Enabling & Developing With Docker Engine API

Enabling & Developing With Docker Engine API

Overview

Docker allows your applications to be shipped quickly to any platform running a Docker Engine, and run these applications in an isolated environment called a container.

Docker Engine is a client-server application, which provides three main components:

  1. A dockerd daemon process
  2. REST API which talks to the daemon
  3. Docker CLI

engine-components-flow.png

Docker Engine API is a RESTful API, accessible over HTTP, which can be used to develop automation scripts and Docker solutions. Docker Engine API is also available as SDKs for Go and Python programming language.

In this tutorial, you will enable the Docker Engine API and do some Docker operations, like, running a container, listing the logs of the container etc.

Pre-Requisites

To follow the tutorial, you need to have:

  • Docker installed on a Linux system and managed by systemd

%[https://elasticskills.dev/install-docker-docker-compose-ansible-ubuntu-2004]

Step 1: Enabling the Docker Engine API

Start by creating a systemd unit file to override the default docker.service unit file.

1$ sudo systemctl edit --full docker.service

Provide dockerd daemon with an additional parameter to expose the API on port 4243

1[...]
2ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock -H tcp://0.0.0.0:4243
3[...]

Reload the systemd daemon to get the new unit file and restart the Docker service.

1$ sudo systemctl daemon-reload
2$ sudo systemctl restart docker.service

Step 2: Access the Docker Engine API

Check the version of Docker Engine installed on the system.

1$ curl localhost:4243/v1.41/version
 1[...]
 2{
 3    "Platform": {
 4        "Name": "Docker Engine - Community"
 5    },
 6    "Components": [
 7        {
 8            "Name": "Engine",
 9            "Version": "20.10.2",
10    [...]
11}

Now our Docker Engine API is enabled to listen to HTTP requests. HTTP clients curl or wget can be used to interact with the API.

Step 3: Running A Container

First, pull the alpine image.

1$ curl -X POST "http://localhost:4243/v1.41/images/create?fromImage=alpine:latest"
1# Output truncated
2{"status":"Pulling from library/alpine","id":"latest"}
3...
4{"status":"Status: Downloaded newer image for alpine:latest"}

Create the container and grab the container Id. It will be used for the rest of the REST calls.

1curl -H "Content-Type: application/json" \
2  -d '{"Image": "alpine:latest", "Cmd": ["echo", "hello world"]}' \
3  -X POST http://localhost:4243/v1.41/containers/create
1{"Id":"d4ac7504007","Warnings":[]}

Start the container

1curl -X POST http://localhost:4243/v1.41/containers/d4ac7504007/start

See the logs to verify the echo statement printed hello world on the stdout.

1curl -o - "http://localhost:4243/v1.41/containers/d4ac7504007/logs?stdout=1"
1hello world

Remove the container

1curl -X DELETE "http://localhost:4243/v1.41/containers/d4ac7504007"

Summary

Learned how Docker Engine API can be accessed using HTTP clients by providing the additional parameters to the dockerd command in the systemd docker.service unit file. Also created a container which runs the latest alpine image and prints hello world on the standard output.