photo by david-knox on unsplash.com

Docker Engine API — A Quick Set-up

Ikeh
3 min readApr 25, 2021

--

Quick summary: In this tutorial, I will show on how to easily setup Docker Engine on your Linux machine.

Introduction

The documentation at Docker Engine API explains it an open source containerization technology for building and containerizing your applications. Docker Engine acts as a client-server application with:

  • A server with a long-running daemon process dockerd.
  • APIs which specify interfaces that programs can use to talk to and instruct the Docker daemon.
  • A command line interface (CLI) client docker.

The CLI uses Docker APIs to control or interact with the Docker daemon through scripting or direct CLI commands. Many other Docker applications use the underlying API and CLI. The daemon creates and manage Docker objects, such as images, containers, networks, and volumes.

Okay. This in simpler terms means with Docker Engine, a client-server application, 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. And the three main components are

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

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

Prerequisites

  • Docker installed on a Linux system and managed by systemd
  • Basic knowledge of the terminal commands

Let’s Get Started

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

The first step is enabling the Docker Engine API by creating a systemd unit file to override the default docker.service unit file.

sudo systemctl edit --full docker.service

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

ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock -H tcp://0.0.0.0:4243

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

sudo systemctl daemon-reload
sudo systemctl restart docker.service

The next step is to access the Docker Engine API. Check the version of Docker Engine installed on the system by running the command:

curl localhost:4243/v1.41/version# Output truncated
{
"Platform": {
"Name": "Docker Engine - Community"
},
"Components": [
{
"Name": "Engine",
"Version": "20.10.2",
...
}

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

Now to running a Container, first I pull the alpine image.

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

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

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

Start the container

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

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

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

Remove the container

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

Summary

You learnt 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.

Related Resources

  1. Docker Engine API
  2. Dockerd
  3. Docker
  4. Docker APIs
  5. Install Docker Engine

--

--

Ikeh

Software Engineer | Technical Writer | I love bunnies