Skip to content

Instantly share code, notes, and snippets.

@afsalthaj
Last active December 8, 2018 01:57
Show Gist options
  • Save afsalthaj/cb7bb31c5ff78970523806a110d2755c to your computer and use it in GitHub Desktop.
Save afsalthaj/cb7bb31c5ff78970523806a110d2755c to your computer and use it in GitHub Desktop.
// Take a look at the snippet. This is just a sample. You might have more/less steps as part of docker compose up
object DockerUtil {
lazy val makeDockerFile = taskKey[Seq[ImageName]]("Make a docker image for the app.")
lazy val makeDockerComposeFile = taskKey[File]("Make docker compose file for the project in resource managed location.")
lazy val dockerComposeUp = taskKey[File]("docker-compose up for the project")
def relativeDockerComposePath(resPath: File) = resPath / "docker" / "bin" / "docker-compose.yml"
def settings = Seq(
makeDockerFile := {
val artifact: File = (assembly in Compile).value
val artifactTargetPath = s"/app/${artifact.name}"
val dockerfile = new Dockerfile {
from("openjdk:10-jre-slim")
add(artifact, artifactTargetPath)
cmd("java", "-jar", artifactTargetPath)
}
val dockerImageNames = Seq(
ImageName(s"afsalthaj/myProject:latest"),
ImageName(s"afsalthaj/myProject:${version.value}")
)
DockerBuild(
dockerfile,
DefaultDockerfileProcessor,
streamImages,
BuildOptions(),
(target in docker).value,
sys.env.get("DOCKER").filter(_.nonEmpty).getOrElse("docker"),
Keys.streams.value.log
)
dockerImageNames
},
makeDockerComposeFile := {
val resourcePath = (resourceManaged in Compile).value
val imageName: ImageName = makeDockerFile.value(0)
val targetFile = relativeDockerComposePath(resourcePath)
// A sample content
val content =
s"""
|version: '2'
|services:
| zookeeper:
| image: confluentinc/cp-zookeeper:5.0.0
| hostname: zookeeper
| ports:
| - '32181:32181'
| ......
| kafka:
| image: confluentinc/cp-enterprise-kafka:5.0.0
| hostname: kafka
| .......
|
| schema-registry:
| image: confluentinc/cp-schema-registry:5.0.0
|
| ....
| custom-application:
| image: ${imageName}
| hostname: my-project-app
| depends_on:
| - kafka
| - schema-registry
| - kafka-create-topics
| ports:
| - '7070:7070'
""".stripMargin
IO.write(targetFile, content)
targetFile
},
dockerComposeUp := {
val file = makeDockerComposeFile.value
DockerComposePlugin.dockerComposeStopInstance("pwxDockerInstance", composePath.getAbsolutePath)
DockerComposePlugin.dockerComposeRemoveContainers("pwxDockerInstance", composePath.getAbsolutePath)
file
},
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment