Docker API Services: Unlocking the Power of Containerization
Learn about docker API services, this is fun and interesting feature of docker. So any one can integrate their project with docker with APIs. happy containerization.
DOCKER
- Luminari
7/16/20243 min read
As the world continues to evolve towards containerization, Docker has emerged as the industry leader in providing a robust platform for deploying, managing, and orchestrating containers. One of the most powerful features of Docker is its comprehensive API (Application Programming Interface) that allows developers to automate, manage, and integrate their containerized applications seamlessly. In this blog post, we will delve into the world of Docker API services, exploring what they are, how they work, and the numerous benefits they offer.
What are Docker API Services?
Docker API is a RESTful (Representational State of Resource) interface that provides a programmatic way to interact with your containerized applications. The API allows you to perform various operations on containers, such as creating, starting, stopping, and removing them, all without having to manually execute commands through the command-line interface or Docker Compose.
Key Features of Docker API Services
1. Container Management: Docker API enables you to create, start, stop, pause, and remove containers, as well as retrieve information about their status, ports, and environment variables.
2. Image Management: You can pull, push, tag, and delete images from your container registry, making it easy to manage and distribute your containerized applications.
3. Network Management: Docker API allows you to create, inspect, and remove networks, as well as attach and detach containers to them.
4. Volume Management: You can create, inspect, and remove volumes, which enables you to persist data even after a container is deleted or restarted.
How Do Docker API Services Work?
To use the Docker API, you need to send HTTP requests to the Docker daemon, which is responsible for managing your containers. The Docker daemon listens on a Unix socket or TCP port by default and responds with JSON-formatted data.
Expose your docker API service
There are multiple ways we can do this, By enabling remote access to machine can also accept the request from out side of local machine. I will leave how use can use it to your imagination.
There are multiple way to do this as mentioned in docker documentation : https://docs.docker.com/config/daemon/remote-access/ , we felt configure through systemd is easy
Configuring Remote access via systemd
Use the command sudo systemctl edit docker.service to open an override file for docker.service in a text editor.
Add or modify the following lines, substituting your own values.
save the file
Reload the systemctl configuration. -> "sudo systemctl daemon-reload"
Restart Docker. $-> "sudo systemctl restart docker.service"
Verify that the change has gone through.
Identify Docker API version
There is a better way to find, But easy way to get is run any docker api request as shown below. docker will show its supported version.
In my case I got an error as : {"message":"client version 1.44 is too new. Maximum supported API version is 1.43"}
So changed above request to v1.43, then it worked.
Simple script using python
Lets see how we utilize this power with python
With this simple get request ,i was able to access docker Api service
output:
[{'Id': '5c9c738fc994360ce85a294ec05c2dedaa08ff218898ca5b0e1a3d0e2c5f63f7', 'Names': ['/open-webui'], 'Image': 'ghcr.io/open-webui/open-webui:main', 'ImageID': 'sha256:5cd3b777b8d87b85b52bccfd1a35e97717242fc19a809553c0c13d6db3d9bcaa', 'Command': 'bash start.sh', 'Created': 1719382198, 'Ports': [{'IP': '0.0.0.0', 'PrivatePort': 8080, 'PublicPort': 3000, 'Type': 'tcp'}, {'IP': '::', 'PrivatePort': 8080, 'PublicPort': 3000, 'Type': 'tcp'}], 'Labels': {'org.opencontainers.image.created': '2024-06-17T05:53:17.029Z', 'org.opencontainers.image.description': 'User-friendly WebUI for LLMs (Formerly Ollama WebUI)', 'org.opencontainers.image.licenses': 'MIT', 'org.opencontainers.image.revision': '9e4dd4b86f77653b59dc1fa97fe3f72e8252b359', 'org.opencontainers.image.source': 'https://github.com/open-webui/open-webui', 'org.opencontainers.image.title': 'open-webui', 'org.opencontainers.image.url': 'https://github.com/open-webui/open-webui', 'org.opencontainers.image.version': 'main'}, 'State': 'running', 'Status': 'Up 2 hours (healthy)', 'HostConfig': {'NetworkMode': 'default'}, 'NetworkSettings': {'Networks': {'bridge': {'IPAMConfig': None, 'Links': None, 'Aliases': None, 'NetworkID': 'f3a57db4b413a44c59cc52ceae9aa7cb04b5f9cdbf7f4e7e4f3982db93172c64', 'EndpointID': '15dcb9ae577a92e9d6cce90f5aa745f5a0959cdd860b9e14f0ca2b56f268d10a', 'Gateway': '172.17.0.1', 'IPAddress': '172.17.0.2', 'IPPrefixLen': 16, 'IPv6Gateway': '', 'GlobalIPv6Address': '', 'GlobalIPv6PrefixLen': 0, 'MacAddress': '02:42:ac:11:00:02', 'DriverOpts': None}}}, 'Mounts': [{'Type': 'volume', 'Name': 'open-webui', 'Source': '/var/lib/docker/volumes/open-webui/_data', 'Destination': '/app/backend/data', 'Driver': 'local', 'Mode': 'z', 'RW': True, 'Propagation': ''}]}]
What next ?
Its fun rite?, If you wanna utilize api service to the fullest checkout the docker doc : https://docs.docker.com/engine/api/ , Remember to follow the version of your docker under "API reference by version"
New to docker checkout learn docker for free,
Preparing for interview? checkout Interview preparation
Contents will be updated time to time, subscribe for updates
My interests
As a techie + proud Hindhu i love to know/write about technology, spiritual knowledge.
Hey!, I am not living library. But if there is topic if you want me to cover, I will do my research and write about it, if it is unfamiliar to me. Its fun to learn and grow together.
Contact ID
Contact
author@luminari.info
© 2024. All rights reserved.
Well usually everyone goes with explaining containers and pod, like workloads but we feel it's better to know architectures first with those questions in the head. we will explain about work loads but now lets jump in with architecture and components.
Well usually everyone goes with explaining containers and pod, like workloads but we feel it's better to know architectures first with those questions in the head. we will explain about work loads but now lets jump in with architecture and components.