Day 16 Task: Docker for DevOps Engineers. Docker
Essential Docker Commands
In today's fast-paced software development world, containerization has become a vital component for deploying applications efficiently. Docker, a popular containerization platform, provides a comprehensive set of commands to manage containers and images effectively. In this guide, we will explore some essential Docker commands that every developer should be familiar with.
Docker run - allows us to start a container based on a specific image. For example, to run an Nginx container, execute the following command in your terminal: In the below command, we use the -d
flag to run the container in the background, -p 8080:80
to map port 8080 on the host machine to port 80 within the container, and --name kp-nginx
to assign the name "kp-nginx" to the container.
docker run -d -p 8080:80 --name kp-nginx nginx
4f46d3a9f9e87c3c1234567890123456789012345678901234567890123456
Docker inspect - this provides detailed output in JSON information about the container, including its ID, creation timestamp, state, configuration, and more.
docker inspect kp-nginx
[ { "Id": "4f46d3a9f9e87c3c1234567890123456789012345678901234567890123456", "Created": "2023-05-18T12:34:56.789Z",
"State": { ... },
"Config": { ... }, ... }]
Docker port -the docker port
command lists the port mappings for a specific container. To check the port mapping for our Nginx container, use the following command:
docker port kp-nginx
80/tcp -> 0.0.0.0:8080
Docker stats- To monitor resource usage of one or more containers, Docker provides the docker stats
command. This command displays real-time statistics on CPU usage, memory consumption, network traffic, and disk I/O. To monitor the Nginx container we created, run the following command:
docker stats kp-nginx
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
4f46d3a9f9e8 my-nginx 0.50% 10MiB / 1GiB 1.00% 5.35kB / 0B 7.09MB / 0B 2
Docker top - Sometimes, it's necessary to view the processes running inside a container. Docker's docker top
command allows you to do just that. By running the following command, you can see the processes running within the container:
docker top kp-nginx
PID USER TIME COMMAND
1 root 0:00 nginx: master process nginx -g daemon off;
7 101 0:00 nginx: worker process
Docker save- provides the ability to save and load images as tar archives. The docker save
command allows you to save an image to a tar archive, while the docker load
command lets you load an image from a tar archive.
To save an image, use the following command:
docker save -o nginx_kptarimage.tar nginx
docker save -o nginx_kptarimage.tar nginx
The docker save
command saves the specified image (nginx
in this example) to a tar archive file named nginx_kptarimage.tar. It doesn't produce any output on the terminal itself, but it creates the tar archive containing the image.
Once the docker save
command is executed, you should see the nginx_kptarimage.tar file in the current directory. This tar archive can be shared, stored, or later loaded into Docker using the docker load
command.
Please note that the output of docker save
command doesn't display any visual output or confirmation message unless there's an error.
More important commands -
docker stop
The docker stop
command is used to stop a running container gracefully. It sends a SIGTERM signal to the container, allowing it to perform any necessary cleanup tasks before stopping.
docker rm
The docker rm
command is used to remove one or more stopped containers from your Docker environment. It frees up resources and allows you to clean up unused containers.
docker rmi
The docker rmi
command is used to remove one or more Docker images from your local image registry. It helps in freeing up disk space and cleaning up unnecessary images.
docker logs
The docker logs
command is used to view the logs generated by a running container. It allows you to troubleshoot issues, debug applications, and monitor the output of containerized processes.
docker exec
The docker exec
command is used to run a command inside a running container. It allows you to execute commands directly within the container's environment.
Thanks for reading #90DayasOfDevops #devops #docker #beginner