DEV Community

Cover image for Access Docker Using Rest API

Posted on • Updated on

Access Docker Using Rest API

Most of the time we use Docker CLI to manage Docker Objects such as containers, images, volumes, and networks. But we Also can use Rest API to Manage Docker objects.

Originally published at

Docker CLI also uses the Docker rest API to manage Objects. As you can see in the above image. But it is also possible to use the Rest API Directly.

The Docker daemon can listen for Docker Engine API requests via three different types of Socket: unix, tcp, and fd.

By Default, unix socket is enabled. This does not help if you want to access the Docker Rest API remotely. Because unix socket works for exchanging data between processes executing on the same host operating system.
If you need to access the Docker daemon remotely, you need to enable the TCP Socket.

sudo dockerd -H tcp://

You can listen on port 2375 on all network interfaces with -H tcp://

There is also another way by using systemd

cd /lib/systemd/system
vi docker.service
ExecStart=/usr/bin/dockerd -H fd:// -H tcp://
#just add -H tcp:// and restart docker

Now you can start using the api

curl -X GET http://ip:4243/containers/json

But if you do not want to access Rest API remotely then you do not need to enable TCP. Then you can just use unix socket.

List containers that are running same like docker ps

$ curl --unix-socket /var/run/docker.sock http:/v1.24/containers/json

You can use Python and Go as well here is the Example:


import docker
client = docker.from_env()
for container in client.containers.list():


package main

import (


func main() {
    ctx := context.Background()
    cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
    if err != nil {

    reader, err := cli.ImagePull(ctx, "", types.ImagePullOptions{})
    if err != nil {
    io.Copy(os.Stdout, reader)

    resp, err := cli.ContainerCreate(ctx, &container.Config{
        Image: "alpine",
        Cmd:   []string{"echo", "hello world"},
        Tty:   true,
    }, nil, nil, "")
    if err != nil {

    if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {

    statusCh, errCh := cli.ContainerWait(ctx, resp.ID, container.WaitConditionNotRunning)
    select {
    case err := <-errCh:
        if err != nil {
    case <-statusCh:

    out, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{ShowStdout: true})
    if err != nil {

    stdcopy.StdCopy(os.Stdout, os.Stderr, out)

There are a number of libraries available for other languages.

Language Library
C libdocker
C# Docker.DotNet
C++ lasote/docker_client
Dart bwu_docker
Erlang erldocker
Gradle gradle-docker-plugin
Groovy docker-client
Haskell docker-hs
HTML (Web Components) docker-elements
Java docker-client
Java docker-java
Java docker-java-api
NodeJS dockerode
NodeJS harbor-master
Perl Eixo::Docker
PHP Docker-PHP
Ruby docker-api
Rust docker-rust
Rust shiplift
Scala tugboat
Scala reactive-docker
Swift docker-client-swift

Now you can create your own User Interface by using Docker Rest API. This API could help you in automating stuff for your Apps.

Top comments (0)