create

suspend fun create(name: String? = null, config: ContainerConfig = ContainerConfig()): Result<ContainerCreateResponse, ErrorResponse>

Creates but does not start the container. Pull the image first if absent. A name conflict is returned as an error; existing resources are not replaced.

Return

Created container ID and daemon warnings, or a Docker error. Transport/decoding failures and cancellation throw; success does not mean the container has started.

Parameters

name

New container name; null lets Docker generate one. A conflicting name is an error.

config

Container image, command and environment. Commands are argument lists, not shell expressions: use listOf("sh", "-c", "...") explicitly when shell expansion is needed.

See also

Samples

withTimeout(120_000) {
    docker.images.create("alpine:latest") { progress ->
        println("${progress.id.orEmpty()}: ${progress.status.orEmpty()}")
    }.getOrThrow()
}
val id = docker.containers.create(name = name, config = ContainerCreateRequest(
    image = "alpine:latest",
    cmd = listOf("sh", "-c", "while true; do date; sleep 5; done"),
    env = listOf("APP_MODE=development"),
)).getOrThrow().id
docker.containers.start(id).getOrThrow()
return id

suspend fun create(name: String? = null, config: ContainerCreateRequest): Result<ContainerCreateResponse, ErrorResponse>

Creates but does not start the container. Pull the image first if absent. A name conflict is returned as an error; existing resources are not replaced.

hostConfig configures mounts, published ports and resource limits; networkingConfig selects initial network attachments. Declaring exposedPorts alone does not publish a host port.

Return

Created container ID and daemon warnings, or a Docker error. Transport/decoding failures and cancellation throw; success does not mean the container has started.

Parameters

name

New container name; null lets Docker generate one. A conflicting name is an error.

config

Container image, command and environment. Commands are argument lists, not shell expressions: use listOf("sh", "-c", "...") explicitly when shell expansion is needed.

See also

ContainerCreateRequest
HostConfig

Samples

val volume = docker.volumes.create(VolumeCreateOptions(name = "$name-data")).getOrThrow()
val networkName = "$name-network"
docker.networks.create(NetworkCreateRequest(name = networkName)).getOrThrow()
return docker.containers.create(name = name, config = ContainerCreateRequest(
    image = image,
    hostConfig = HostConfig(
        mounts = listOf(Mount(type = Mount.Type.VOLUME, source = volume.name, target = "/data")),
        portBindings = mapOf("8080/tcp" to listOf(
            PortBinding(hostIp = "127.0.0.1", hostPort = "8080"),
        )),
    ),
    networkingConfig = NetworkingConfig(endpointsConfig = mapOf(
        networkName to EndpointSettings(aliases = listOf("app")),
    )),
)).getOrThrow().id