How Docker Works: A Comprehensive Guide to Understanding the What? Why? and How?
Docker has been a buzzword in the software industry for a while, growing in popularity daily. In this article, we’ll get a basic understanding of what docker is, why we use it, how it works, and the commands that are essential to know while using Docker.
Because of the Agile trend and the expansion of cloud infrastructure, companies favor microservices over monolithic applications, which means instead of building a huge and complicated software, we split it down into smaller parts/services that can be delivered independently. Managing the configuration of microservices and delivering them to production is difficult, which is where Docker comes in to help develop, ship and deliver the product with minimal effort using containerization
What is Docker:
Docker is a freemium platform-as-a-service software for building, shipping, and running applications. Docker employs OS-level virtualization to distribute applications in containers. By utilizing Docker’s methodology for fast shipping, testing, and deploying code, you could considerably reduce the amount of time between developing software and deploying it in production.
Why Docker:
- Docker is cost-saving as most services provided along with docker are either open-source or freemium.
- Docker containers might save a significant amount of money over cloud computing as they are efficient because they share many of their resources with the host system and require very low RAM and CPU time.
- Once configured, docker images may be launched on a wide range of environments, eliminating the ‘works on my computer’ issues that affect DevOps teams.
- When deploying microservices, virtual machines are quite expensive in terms of performance when compared to docker because they require the installation of an entire new OS for each virtual machine required, which increases the resource consumption and load on the Host infrastructure, whereas once the Docker engine is installed on the Host infrastructure, we can run as many containers as needed with only a few commands.
How Docker works:
To understand how Docker works, we should first understand basic docker terminologies and docker architecture, as shown following.
- Docker Client:
Users can communicate with Docker using docker client. When we use commands like docker run on CLI, docker client sends this data to the docker daemon using API request. - Docker Server/Host:
The Docker Server/Host is a machine that has a docker engine installed along with docker daemon (dockerd) which controls Docker objects such as images, containers, networks, and volumes by listening to docker Requests. - Docker image registry:
It is a way of storing and distributing docker images. Primarily it is a collection of several repositories containing different images with multiple versions identified by their tags
When we run a command on docker client, it is transmitted to the docker daemon using the docker API. Docker daemon then understands this command and searches the local repository for the required image; if it is not found, it sends a request to the image registry. If an image is available, it is pulled from the image registry and a container is immediately created to deploy that image.
Essential Docker commands you NEED to know
**Please use CTRL + F to search specific command**
1. Pull an image or a repository from the image registry
> docker image pull ubuntu:latest
2. Remove the docker image which is not running in container
> docker image remove [image_id]
> docker image rm [image_id]
3. Remove all unused images
> docker image prune -a
4. See all available local images
> docker images
> docker image ls
5. View logs of image
> docker image history [image_id]
6. View metadata/details related to image
> docker image inspect [image_id]
7. Running docker container using image
> docker run -p 5000:5000 ubuntu:latest
8. To run container in detached mode, i.e. logs will not be followed
> docker run -d -p 5001:5000 ubuntu:latest
9. See all running containers
> docker container ls
10. See all containers ( running + stopped )
> docker container ls -a
11. To view the logs of container
> docker logs [container_id]
12. To view logs in follow-up mode
> docker logs -f [container_id]
13. To inspect and show metadata related to container
> docker container inspect [container_id]
14. Removing exited containers
> docker container rm [container_id]
15. Pause running container
> docker container pause [container_id]
16. Start again paused container
> docker container unpause [container_id]
17. To stop container gracefully (give time to disconnect services and then stops container)
> docker container stop [container_id]
18. Stop container immediately (Unlike stop it doesn’t care about disconnecting services instead immediately stops container once command received)
> docker container kill [container_id]
19. Start again stopped containers
> docker container start [container_id]
20. Remove all stopped containers
> docker container prune
21. View disk-space used by docker daemon
> docker system df
> docker system df -v #shows detailed space usage for images and containers
22. To get real-time events from docker objects (e.g. start, stop, pull, run)
> docker system events
23. View detailed information about system
> docker system info
24. Remove all unused data (containers, images, networks, and build cache)
> docker system prune -a
25. View CPU and Memory details related to container
> docker stats [container_id]
26. Allocate customized memory and CPU to container
# memory:- -m 512will assign 512 mb space to container
# CPU quota:- We defaultly have 100000 cpu quota which can be assigned accordingly to container using --cpu-quota flag
> docker container run -p 5000:5000 d -m 512m --cpu-quota=50000 ursmaheshj/blogpost:0.0.1
27. Building image using Dockerfile
> docker build -t repo_name/tag_name:release_version .
# the dot(.) at last is required it specifies the context
# repo_name not required if you dont want to push image to image registry
28. Pushing image to Docker Hub
> docker push repo_name/tag_name:release_version
29. View available networks
> docker network ls
30. Create new custom network
> docker network create [network_name]
31. View details related to specific network
> docker network inspect [netwrok_id]