I’d like to connect to the Docker Engine API via `...
# ktor
a
I’d like to connect to the Docker Engine API via
docker.sock
using Ktor. I’ve found KTOR-781 which describes this exact case, and the issue was completed in Ktor 2.0, but I can’t find an example. Is connecting to
docker.sock
with Ktor possible? Is there an example out there somewhere? I’ve given it a go (code in 🧵), but I’m not sure how to get it working.
dockerSock_kt.cpp
I’m using Ktor 2.2.2, Kotlin/Native 1.8
a
The following code works for me using JDK 16+:
Copy code
fun main() = runBlocking {
    val manager = SelectorManager(<http://Dispatchers.IO|Dispatchers.IO>)
    val socket = aSocket(manager)
        .tcp()
        .connect(UnixSocketAddress("/var/run/docker.sock"))

    val receiveChannel = socket.openReadChannel()
    val sendChannel = socket.openWriteChannel(autoFlush = true)

    val send = launch(<http://Dispatchers.IO|Dispatchers.IO>) {
        sendChannel.writeStringUtf8("GET /containers/json HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n")
        sendChannel.flush()
    }

    val receive = launch {
        while (true) {
            val line = receiveChannel.readUTF8Line() ?: break
            println(line)
        }
    }

    send.join()
    receive.join()
}
🌟 1
a
YES that works, thanks!