DEV Community

Cover image for Access Docker Using Rest API
Davinder
Davinder

Posted on • Edited 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 https://devkamboj.in.

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://0.0.0.0:2375

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

There is also another way by using systemd

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

Now you can start using the api

curl -X GET http://ip:4243/containers/json
[{
  "Id":"fff8c7927e19d5a7da280424a09572729641137a8f42e116c3073c72da2978d3",
 "Names":["/rockey"],
  "Image":"busybox",
  ...
}]

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
[{
  "Id":"ae63e8b89a26f01f6b4b2c9a7817c31a1b6196acf560f66586fbc8809ffcd772",
  "Names":["/tender_wing"],
  "Image":"bfirsh/reticulate-splines",
  ...
}]

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

Python

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

Go

package main

import (
    "os"

    "github.com/docker/docker/api/types"
    "github.com/docker/docker/api/types/container"
    "github.com/docker/docker/pkg/stdcopy"
    "github.com/docker/docker/client"
    "golang.org/x/net/context"
)

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

    reader, err := cli.ImagePull(ctx, "docker.io/library/alpine", types.ImagePullOptions{})
    if err != nil {
        panic(err)
    }
    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 {
        panic(err)
    }

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

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

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

    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)