The Docker Engine API is the central part of the Docker ecosystem, it is the interface through which developers and system administrators are able to work with the Docker daemon programmatically. This is an API that allows one to manage containers, images, networks, and volumes through making HTTP requests against a Docker daemon. The Docker Engine API can be used for automation of tasks, integrating Docker with any other tools, and tailor-making solutions for the job at hand.
It is a RESTful API, so it uses the standard HTTP methods GET, POST, PUT, and DELETE to do operations against Docker resources. Whether it is container creation, image pulling, or network inspection, the Docker Engine API provides the flexibility and control to do these tasks either remotely or locally.
In this article, we will discuss with you the most important parts of the Docker Engine API: key terminology, how to guide on its usage, and practical examples. Moreover, we will show you how to secure your API usage and answer many common questions in the FAQ section.
Primary Terminologies
Docker Daemon: The Docker daemon is a service that runs in the background on the host machine and manages Docker objects such as containers, images, networks, and volumes. The Docker Engine API interacts with this daemon.
JSON Payload: The Docker Engine API talks in JSON (JavaScript Object Notation). This means requests and responses both contain data that are formatted using JSON; it is both human-readable and, at the same time, super easy to parse and generate with different programming languages.
HTTP Methods: The Docker Engine API uses standard HTTP methods like GET, POST, DELETE, and PUT to interact with the Docker daemon:
- GET: Obtain information (e.g., list containers).
- POST: Create or initiate something (e.g., initiate a container).
- DELETE: Remove something (e.g., delete an image).
- PUT: Update or replace something.
API Endpoint: An endpoint refers to a specific URL to which all requests for a web service are directed. In the Docker Engine API, endpoints correspond to operations like creating a container, starting a container, or pulling an image.
Unix Socket: By default, the Docker Engine API is accessed through a Unix socket, /var/run/docker.sock, which communicates locally between the client and daemon, if it is desired for other machines to communicate with the API, this can be modified so it listens over a TCP socket.
TLS (Transport Layer Security): TLS is a protocol that ensures safe communication over a computer network, accesses the Docker Engine API remotely through a TCP socket, uses encryption of the connection via TLS, and help guard it against unauthorized access.
Container ID: The Container ID is the unique identifier of any Docker container. It is required in all API requests to define which container it operates against. For instance, for starting a container, the request should be made at /containers/{id}/start.
Image: A Docker image is a lightweight, stand-alone, executable package that includes everything needed to run a piece of software. The Docker Engine API enables you to pull, inspect, and manage images programmatically.
What Is Docker Engine API?
The Docker Engine API is a RESTful API exposed by Docker for interaction programmatically with the Docker daemon. It's the service within Docker responsible for the core functionality of managing Docker containers, images, networks, and volumes on a host system, this allows developers and system administrators to use Docker for automating operations in such a way that resources are managed most efficiently.
This API can enable a variety of actions, including:
- Container Management: Creation, starting, stopping, and removal of containers.
- Image Management: Pull, build, tag, and push Docker images.
- System Information: Obtain system-level details related to the Docker environment, like running containers, available images, and system resources.
- Network and Volume Management: Create and manage networks and storage volumes for containers.
In its most basic form, Docker's CLI is the user of the Docker Engine API. It actually can be used directly by sending HTTP requests, and this turns it into a really powerful tool for using Docker integration inside custom applications, automation scripts, or CI/CD pipelines. It is versioned to be compatible with different versions of Docker. The API may be secured over TLS so that communications between clients and the Docker daemon are protected.
How to Use Docker Engine API: Step-by-Step Guide
Step 1: Install Docker
sudo yum -y install docker
- Now start and enable docker engine by using following commands
sudo systemctl start docker
sudo systemctl enable docker
sudo systemctl status docker
Step 2: Listing Containers
- By using following command we can list out containers
curl --unix-socket /var/run/docker.sock http://localhost/containers/json
The command curl --unix-socket /var/run/docker.sock http://localhost/containers/json returned an empty array []. This means that there are no running containers on your Docker instance at the moment.
Step 3: Creating a Container:
- To create a container, you use the POST method with curl and provide the necessary JSON data.
Pull the Image:
docker pull nginx:latest
Create the Container:
#bash
curl -X POST -H "Content-Type: application/json" \
-d '{"Image": "nginx", "Cmd": ["nginx", "-g", "daemon off;"]}' \
--unix-socket /var/run/docker.sock http://localhost/containers/create
Step 4: Start the Container
Example:
#bash
curl -X POST --unix-socket /var/run/docker.sock \
http://localhost/containers/<container_id>/start
curl -X POST --unix-socket /var/run/docker.sock \
http://localhost/containers/824f455287516591815effbef7c
Check the Container's Status
To verify that your container is running:
curl --unix-socket /var/run/docker.sock http://localhost/containers/824f455287516591815effbef7c49db38e42f0d8c932213f673b8f8432c208a6/json
View Container Logs
To view the logs generated by your container:
curl --unix-socket /var/run/docker.sock \
http://localhost/containers/824f455287516591815effbef7c49db38e42f0d8c932213f673b8f8432c208a6/logs?stdout=1&stderr=1
Step 5: Stop the Container
To stop the container when you no longer need it:
curl -X POST --unix-socket /var/run/docker.sock \
http://localhost/containers/824f455287516591815effbef7c49db38e42f0d8c932213f673b8f8432c208a6/stop
We can check container list it is stopped or not
docker ps -a
Step 6: Remove the Container
To remove the container from your system:
curl -X DELETE --unix-socket /var/run/docker.sock \
http://localhost/containers/824f455287516591815effbef7c49db38e42f0d8c932213f673b8f8432c208a6
Conclusion
The Docker Engine API is important for those looking to automate and integrate Docker capabilities into their applications or workflows. Exposing a RESTful interface to the Docker daemon, it makes Docker's operations programmable in ways that give a developer flexibility in managing containers, images, networks, and more with great flexibility and precision. Advanced management and automation of Docker can be achieved either directly through the Docker Engine API or through higher-level tools like the Docker CLI.
Docker Engine API - FAQs
What is the Docker Engine API?
The Docker Engine API is a RESTful interface to which you can interact with a Docker daemon programmatically, it will help you manage containers, images, networks, and other resources of Docker through HTTP requests.
How do I get to the Docker Engine API?
The API endpoint of the Docker daemon is a means to reach the Docker Engine API by making HTTP requests toward it. This can be achieved using any HTTP client, such as curl, Postman, and programming languages such as Python or Go.
What kind of operations can I do with the Docker Engine API?
The Docker Engine API allows you to do a lot of things, some of which are listed here:
- Creating, starting, stopping, and killing containers.
- Pulling and pushing images.
- Managing networks and volumes.
- Watching Docker events.
- Viewing the system information for the Docker environment.
Is the Docker Engine API secure?
Yes, the Docker Engine API can be secured using TLS encryption and client authentication. The preferred configuration should be to make the API listen only on a local socket by default, but exposing the API over a network should require TLS.
May I use the Docker Engine API to manage remote Docker daemons?
Yes, the Docker Engine API can interact with both local and remote Docker daemons, keep in mind, though, while managing remote daemons, secure the API using TLS to avoid unauthorized access.