narma / zio-docker   2.0.1-M1

Apache License 2.0 GitHub

High-level zio interface to docker-java

Scala versions: 3.x 2.13

zio-docker

CI ZIO2 CI ZIO1 Latest release
CI Badge ZIO2 CI Badge ZIO1 Release Artifacts

High-level zio interface to docker-java

zio-docker is currently available for Scala 2.13 and 3.1.1-RC1 both for ZIO 1.x and 2.x

How to use

  1. Add zio-docker

For ZIO 2.x (current for ZIO 2.0.0-M6-2)

"st.alzo" %% "zio-docker" % "2.0.1-M1"

For ZIO 1.x

"st.alzo" %% "zio-docker" % "1.0.1"
  1. Choose docker-java transport from official documentation and add this transport dependency to your project:

For example apache http transport:

"com.github.docker-java" % "docker-java-transport-httpclient5" % dockerJavaVersion

Create a ZDocker instance

For example with apache httpclient transport

import com.github.dockerjava.api.DockerClient
import com.github.dockerjava.core.{DefaultDockerClientConfig, DockerClientImpl}
import com.github.dockerjava.httpclient5.ApacheDockerHttpClient
import st.alzo.zdocker.{ZDocker, ZDockerLive}
import zio.*

def makeClient(): TaskManaged[ZDocker] = {

  ZDocker.fromDockerHttpClient(
    DefaultDockerClientConfig.createDefaultConfigBuilder().build()
  )(config => Task {
    new ApacheDockerHttpClient.Builder()
      .connectionTimeout(2.seconds)
      .responseTimeout(5.seconds)
      .dockerHost(config.getDockerHost)
      .build()
  })
}

Usage

import zio.*
import st.alzo.zdocker.*
import zio.Runtime.default as rt


def helloWorld(): Task[Unit] =
  ImageName("hello-world").flatMap { img =>
    ZDocker.startManagedContainer(img).use { containerId =>
      ZDocker.collectLogs(containerId)
        .map(_.value)
        .tap(s => ZIO.debug(s))
        .runDrain
    }
  }.provideSome(makeClient().toLayer)

rt.unsafeRun(helloWorld())