Introduction đ
- Docker has rapidly become the de facto standard for containerizing applications. As a developer or DevOps Engineer/SysAdmin, getting familiar with Docker is crucial for deploying modern, portable applications efficiently.
- While Docker boasts numerous advanced features and commands, there are certain essential ones youâll find yourself using consistently on a daily basis.
- In this post, we will go through the top 20+ essential Docker commands and their use cases everyone should know.
Installation đ¨
- To install Docker on your machine, follow this document (Supported various platforms)
- To get hands-on experience and understand Docker better, you could visit this repository
Table of Contents đ
Docker General Commands
1. docker info
docker info
displays system-wide information
- Syntax:
docker info
2. docker âhelp
docker --help
gets help with Docker. Can also use âhelp on all subcommands
- Syntax:
docker <subcommands> --help
Docker Registry
3. docker login
docker login
is used to log in to a Docker registry. If no server is specified, the default is defined by the daemon.
- Syntax:
docker login <options> <registry>
- Use cases:
ID |
Command |
Description |
1 |
docker login |
Log in to the default Docker registry |
2 |
docker login myRegistry -u username |
Log in to a specified registry with the specified username |
4. docker logout
- To log out from a Docker registry, use
docker logout
. This command is used when you want to remove the credentials used to authenticate with a registry.
- Syntax:
docker logout <registry_url>
- Use cases:
ID |
Command |
Description |
1 |
docker logout |
Log out from the default Docker registry |
2 |
docker logout myRegistry |
Logout from a specified registry |
Docker Images
5. docker build
docker build
is used to build custom Docker images from a Dockerfile.
- Syntax:
docker build -t <your_image_name> <options> <dockerfile_path>
- Use cases:
ID |
Command |
Description |
1 |
docker build -t myImage . |
Build an image using a Dockerfile in the current directory |
2 |
docker build -t myImage:v0.1.0 . |
Build a Docker image from a Dockerfile with specified tag |
3 |
docker build -t myImage -f demo/Dockerfile |
Build an image using a Dockerfile in the demo directory |
6. docker tag
docker tag
allows you to create a new tag for an existing Docker image.
- Syntax:
docker tag <source_image> <target_image>
- Use cases:
ID |
Command |
Description |
1 |
docker tag myImage:latest myImage:v2 |
Create a new tag âv2â for the Docker image âmyImageâ with tag âlatestâ |
2 |
docker tag myImage:latest myRegistry/myImage |
Tag the Docker image âmyImageâ with tag âlatestâ to a registry image tag latest |
3 |
docker tag myImage:latest myRegistry/myImage:1.0.0 |
Tag the Docker image âmyImageâ with tag âlatestâ to a registry image tag 1.0.0 |
7. docker images
- The
docker images
command lists all Docker images pulled and built on your system. Youâll use this frequently to view images before running containers or cleaning up.
- Syntax:
docker images <options>
- Use cases:
ID |
Command |
Description |
1 |
docker images |
List all Docker images on the system |
2 |
docker images -a |
List all Docker images, including intermediate ones |
8. docker pull
- To download an image from a registry like Docker Hub, use
docker pull
. For example, docker pull nginx
fetches the latest nginx image.
- Syntax:
docker pull <image_name>
- Use cases:
ID |
Command |
Description |
1 |
docker pull nginx |
Pull the latest nginx image from Docker Hub |
2 |
docker pull nginx:latest |
Pull the latest nginx image from Docker Hub explicitly |
3 |
docker pull myRegistry/myImage:tag |
Pull a specific image from a private registry |
9. docker push
docker push
is used to upload Docker images to a registry.
- Syntax:
docker push <image_name>
- Use cases:
ID |
Command |
Description |
1 |
docker push myImage |
Push the âmyImageâ image to the default registry |
2 |
docker push myRegistry/myImage:tag |
Push a specific tagged version of the âmyImageâ image to a private registry |
10. docker save
- To save a Docker image to a .tar file, use
docker save
. This command allows you to export an image from your local Docker environment into a portable format.
- Syntax:
docker save -o <output_file> <image_name>
- Use cases:
ID |
Command |
Description |
1 |
docker save -o myImage.tar myImage |
Save a Docker image to a local .tar file |
2 |
docker save -o /path/to/output.tar myImage |
Save an image to a specific location on your system |
11. docker load
- To load an image from a saved archive, use
docker load
. This command is handy when you have an image saved as a .tar file and want to import it into your local Docker environment.
- Syntax:
docker load -i <path_to_image_archive>
- Use cases:
ID |
Command |
Description |
1 |
docker load -i myImage.tar |
Load a Docker image from a local .tar file |
2 |
docker load -i /path/to/image_archive.tar |
Load an image from a specific location on your system |
12. docker rmi
docker rmi
removes one or more Docker images. Make sure there are no stopped containers based on an image before removing it.
- Syntax:
docker rmi <image_name>
- Use cases:
ID |
Command |
Description |
1 |
docker rmi myImage |
Remove the Docker image named âmyImageâ |
2 |
docker rmi myImage:tag |
Remove a specific tagged version of the âmyImageâ image |
Docker Container
13. docker run
docker run
creates and starts a new container from an image. You can pass various options to configure the containerâs networking, set environment variables, map volumes, and more.
- Syntax:
docker run <options> <image>
- Use cases:
ID |
Command |
Description |
1 |
docker run -d --name mycontainer nginx |
Run nginx image in detached mode with container name âmycontainerâ |
2 |
docker run -p 8080:80 --name mycontainer nginx |
Run nginx image with port mapping from host 8080 to container 80 |
3 |
docker run -e MYSQL_ROOT_PASSWORD=password -v /mydata:/var/lib/mysql mysql |
Run MySQL image with setting root password |
4 |
docker run -v /mydata:/var/lib/mysql mysql |
Run MySQL image and mounting a host directory to container |
5 |
docker run --network=host myImage |
Run a container using the host network |
6 |
docker run --privileged myImage |
Run a container with extended privileges using the Docker image âmyImageâ |
7 |
docker run -it myImage /bin/bash |
Run an image and open a bash shell inside a container |
14. docker ps
- Once you have containers running, youâll need
docker ps
to list them. The basic docker ps
shows just running containers. Use docker ps -a
to include stopped containers as well.
- Syntax:
docker ps <options>
- Use cases:
ID |
Command |
Description |
1 |
docker ps |
List running containers |
2 |
docker ps -a |
List all containers, including stopped ones |
15. docker stop/start/restart
- These commands allow you to stop, start, or restart one or more running containers. You reference containers by name or ID.
- Syntax:
docker stop/start/restart <container_name>
- Use cases:
ID |
Command |
Description |
1 |
docker stop mycontainer |
Stop a running container named âmycontainerâ |
2 |
docker restart mycontainer |
Restart a container named âmycontainerâ |
3 |
docker start mycontainer |
Start a stopped container named âmycontainerâ |
16. docker rm
- Once youâve stopped a container,
docker rm
removes it entirely from your system. Use docker rm -f
to force-remove running containers.
- Syntax:
docker rm <options> <container>
- Use cases:
ID |
Command |
Description |
1 |
docker rm mycontainer |
Remove a stopped container named âmycontainerâ |
2 |
docker rm -f mycontainer |
Force-remove a running container named âmycontainerâ |
17. docker logs
- When a containerized application is not behaving correctly,
docker logs
retrieves the logs from a specified container to help troubleshoot.
- Syntax:
docker logs <options> <container>
- Use cases:
ID |
Command |
Description |
1 |
docker logs mycontainer |
Retrieve logs from a container named âmycontainerâ |
2 |
docker logs --tail 100 mycontainer |
Retrieve last 100 lines of logs from âmycontainerâ |
18. docker exec
docker exec
allows you to run a new command inside an already-running container. For example, starting a Bash shell with docker exec -it <container> /bin/bash
.
- Syntax:
docker exec <options> <container> <command>
- Use cases:
ID |
Command |
Description |
1 |
docker exec -it mycontainer /bin/bash |
Start an interactive Bash shell in âmycontainerâ |
2 |
docker exec mycontainer ls -l /app |
List files in directory â/appâ in âmycontainerâ |
19. docker cp
docker cp
allows you to copy files and directories between a container and the local filesystem.
- Syntax:
docker cp <container_id_or_name>:<source_path> <destination_path>
- Use cases:
ID |
Command |
Description |
1 |
docker cp mycontainer:/app/logs/log.txt ./local_dir/ |
Copy the file âlog.txtâ from the container âmycontainerâ to a local directory |
2 |
docker cp ./local_file.txt mycontainer:/app/data/ |
Copy the file âlocal_file.txtâ from the local filesystem to the container âmycontainerâ |
Docker cleanup
20. docker system prune
docker system prune
allows you to clean up unused data in your Docker environment.
- Syntax:
docker system prune <options>
- Use cases:
ID |
Command |
Description |
1 |
docker system prune |
Remove all stopped containers, dangling images, and unused networks |
2 |
docker system prune -a |
Remove all stopped containers, all unused images, and all unused networks |
Whatâs next?
- For the full list of docker commands, visit: https://docs.docker.com/reference/cli/docker/
- You could the most comprehensive and up-to-date content on this topic, please visit this [repo] (https://github.com/tungbq/devops-basics/blob/main/topics/docker/basic/top-docker-cmd.md) âď¸.
Which Docker command do you find yourself using the most? Let us know in the comments below. Your feedback and suggestions are highly appreciated. Thank you, and happy coding! đ