Docker client written at first for testing purpose, for database migration managed by flyway and for avoiding issues on the creation or modification of tables.
- Clone this project and point a terminal to the root of the project
- in terminal run
sbt assembly - in terminal run
sbt publishLocalThis will copy the jar to your local ivy2 folder (~/.ivy2/local/) - In your project's build.sbt add "com.unrlab" % "docker-scala_2.11" % "0.1.4" to the dependencies
- That's it!
- Scala 2.11+ / sbt 0.13+
- docker installed with version 1.7+
- Stop the docker daemon
sudo systemctl stop docker.service - Create a folder reserved for your docker configuration
sudo mkdir /etc/systemd/system/docker.service.d - Copy the default daemon configuration into the systemctl conf folder
sudo cp /lib/systemd/system/docker.service /etc/systemd/system/docker.service.d/docker.conf - Remove the line starting with ExecStart
- Add
ExecStart=
ExecStart=/usr/bin/dockerd -H fd:// -H tcp://127.0.0.1:2375 -H unix:///var/run/docker.sock
- Reload daemon
sudo systemctl daemon-reload - Start docker daemon
sudo systemctl start docker.service
- Stop the docker daemon
sudo service docker stop - Edit the default docker conf
sudo vim /etc/default/docker - Add the host of your desired daemon on the DOCKER_OPTS line
DOCKER_OPTS="-H tcp://127.0.0.1:2375 -H unix:///var/run/docker.sock" - Start the docker daemon
sudo service docker start
Here is the default configuration
unrlab {
docker {
host = "http://127.0.0.1:55555"
version = "1.25"
}
}
Add these lines to your configuration and feel free to update the host or version in order to adapt them to your needs.
There is a helper for mysql container, but your can start other containers without the need of the helper.
You need to mix the Trait MysqlDockerSupport to your test class.
package com.unrlab.mysql
import com.unrlab.config.DbConfig
import com.unrlab.db.tools.Migrator
import com.unrlab.dockerscala.support.{MysqlDockerDbConfig, MysqlDockerSupport}
import org.scalatest.{BeforeAndAfterAll, FlatSpec, Matchers}
import slick.driver.JdbcDriver
class MySqlMigrationTest
extends FlatSpec
with Matchers
with BeforeAndAfterAll
with MysqlDockerSupport {
"Migrator" should "run against mysql" in {
val conf: MysqlDockerDbConfig = buildMysqlContainer()
val testDbconfig = new DbConfig {
override def username: String = conf.username
override def url: String = conf.url
override def password: String = conf.password
override def jdbcDriver: String = conf.jdbcDriver
override def slickDriver: JdbcDriver = slick.driver.MySQLDriver
}
import scala.concurrent.duration._
var version = 0
try {
val migrator = new Migrator(testDbconfig)
version = migrator.migrateRetry(10 seconds)
} catch {
case e: Throwable =>
logError(s"[ERROR][MIGRATE] : ${e.getMessage}")
} finally {
cleanMysqlContainer(conf.containerName)
version should be (5)
}
}
}buildMysqlContainer() will create a docker container with mysql 5.6 and return a conf case class
case class MysqlDockerDbConfig(url: String, jdbcDriver: String = "com.mysql.jdbc.Driver", username: String = "root", password: String, containerName: String)With this case class, you will be able to recreate your database layer config.
The Api Docker, need a DockerContainer object
class DockerClientMysqlTest extends FlatSpec with Matchers{
"Docker" should "return the container builded from a mysql image" in {
val password: String = "password"
val mysqlContainer = new DockerContainer()
.withEnv(Seq(EnvVariable("MYSQL_ROOT_PASSWORD", password)))
.withPortMapping(Seq(PortMapping(3306, 12345)))
.withName(s"test_mysql")
.withImage("mysql:5.6")
val result = Docker.run(mysqlContainer)
result.statusCode should be (204)
}
"Docker" should "return a container data when making a find request" in {
val password: String = "password"
val dockerContainer = new DockerContainer()
.withName("hello-world")
.withImage("hello-world")
Docker.run(dockerContainer)
Thread.sleep(2000)
Docker.find("hello-world") should be(true)
Docker.stop("hello-world") should be (true)
Docker.remove("hello-world") should be (true)
Docker.find("hello-world") should be(false)
}
}- Docker.run(container: DockerContainer): HttpResponse
with HttpResponse defined as :
case class HttpResponse(statusCode: Int, body: String)The run function will search if the container is up, and will try to stop and remove before running a new one.
- Docker.find(name: String): Boolean Where name is the name or the id of the container
- Docker.stop(name: String): Boolean Where name is the name or the id of the container
- Docker.remove(name: String): Boolean Where name is the name or the id of the container