Last active
September 2, 2021 00:02
-
-
Save alirezameskin/bc1b34bb022fc34f77772cdaf063d1eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name := "docker-client" | |
version := "0.1" | |
scalaVersion := "2.12.8" | |
libraryDependencies += "org.typelevel" %% "cats-free" % "1.6.0" | |
libraryDependencies += "com.spotify" % "docker-client" % "8.15.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object DockerClientApp extends App { | |
import DockerClientOps._ | |
val client:DockerClient = DefaultDockerClient.fromEnv().build() | |
val interpreter = spotifyInterpreter(client) | |
val program = for { | |
_ <- pull("busybox:latest") | |
cntrId <- run("busybox", "sh", "-c", "while :; do sleep 1; done") | |
res <- exec(cntrId, "date") | |
_ <- kill(cntrId) | |
_ <- remove(cntrId) | |
} yield res | |
val date = program.foldMap(interpreter) | |
println(date) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object DockerClientOps { | |
sealed trait DockerOperation[A] | |
case class Pull(image: String) extends DockerOperation[Unit] | |
case class Run(image: String, command: String*) extends DockerOperation[String] | |
case class Exec(containerId: String, command: String*) extends DockerOperation[String] | |
case class Kill(containerId: String) extends DockerOperation[Unit] | |
case class Remove(containerId: String) extends DockerOperation[Unit] | |
def pull(image: String): Free[DockerOperation, Unit] = Free.liftF(Pull(image)) | |
def run(image: String, command: String*): Free[DockerOperation, String] = Free.liftF(Run(image, command:_*)) | |
def exec(containerId: String, command: String*): Free[DockerOperation, String] = Free.liftF(Exec(containerId, command:_*)) | |
def kill(containerId: String): Free[DockerOperation, Unit] = Free.liftF(Kill(containerId)) | |
def remove(containerId: String): Free[DockerOperation, Unit] = Free.liftF(Remove(containerId)) | |
def spotifyInterpreter(client: DockerClient): DockerOperation ~> Try = new (DockerOperation ~> Try) { | |
override def apply[A](fa: DockerOperation[A]): Try[A] = fa match { | |
case Pull(image: String) => Try { | |
client.pull(image) | |
} | |
case Run(image: String, command@_*) => Try { | |
val config = ContainerConfig.builder().cmd(command:_*).image(image).build() | |
val container:ContainerCreation = client.createContainer(config) | |
client.startContainer(container.id()) | |
container.id() | |
} | |
case Exec(containerId, command@_*) => Try { | |
val params = Seq(ExecCreateParam.tty(true)) | |
val execCreation = client.execCreate(containerId, command.toArray[String], params:_*) | |
val output: LogStream = client.execStart(execCreation.id) | |
output.readFully() | |
} | |
case Kill(containerId) => Try { | |
client.killContainer(containerId) | |
} | |
case Remove(containerId) => Try { | |
client.removeContainer(containerId) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment