Docker has become one of the most widely used tools by developers and system administrators for creating, testing, and managing isolated environments through containers. Its ability to encapsulate applications and their dependencies into containers has transformed how services and applications are managed across different environments.
Whether you’re new to Docker or have some experience, mastering the core Docker commands is essential for efficiently managing containers. In this article, we explore 20 essential Docker commands that will help you maximize your Docker workflow.
What is Docker?
Docker is an open-source platform that enables developers to build, manage, and run applications in containers. Through this technology, developers can package their applications with all necessary dependencies, making them easily portable between different environments such as local servers, development setups, and cloud environments.
Docker allows for more efficient use of system resources since containerized apps generally consume less memory compared to virtual machines. Additionally, Docker makes applications portable, as a container includes everything the application needs to run.
You only need Docker runtime on a host to execute a Docker container seamlessly. Now that you have an understanding of the basics, let’s dive into 20 essential Docker commands.
20 Essential Docker Commands and What They Do
Here are 20 essential Docker commands and their intended functions to help you work more effectively with containers.
1. docker run
This command is used to create and start containers. If the container image is not found, it will create a new one and run it.
Example:
docker run --name nginx -p 8080:80 -d nginx
This runs the nginx
container, mapping port 80 inside the container to port 8080 on the host.
2. docker search
This command is used to search for images from Docker Hub or other registries. It shows you image information, including the name, description, and other specifications.
Example:
docker search ubuntu
Searches for available Ubuntu images on Docker Hub.
3. docker stop
This command stops running containers gracefully. Unlike the docker kill
command, this one allows the container to stop on its own.
Example:
docker stop nginx
Stops the nginx
container.
4. docker rename
This command allows you to rename a container.
Example:
docker rename nginx nginx-container
Renames the nginx
container to nginx-container
.
5. docker restart
If a container is stopped, this command restarts it.
Example:
docker restart nginx-container
6. docker pause / unpause
This command pauses all processes within a container. It can also unpause the container’s processes.
Example:
docker pause nginx-container
docker unpause nginx-container
7. docker kill
This command sends the SIGKILL signal to a container, forcing it to stop immediately. It’s generally used for emergency situations.
Example:
docker kill nginx-container
Use this command only when necessary, as it forcefully terminates the container.
8. docker pull
This command pulls an image from Docker Hub or another registry.
Example:
docker pull node:14
Pulls the official Node.js version 14 image.
9. docker ps
This command lists all running containers. You can use it to see which containers are active.
Example:
docker ps --all
To see even the stopped containers, add the --all
flag.
10. docker login
This command logs you into Docker Hub or another registry. You’ll be prompted for your Docker Hub credentials.
Example:
docker login
11. docker commit
This command saves a container as a new image, or creates one on the local system.
Example:
docker commit nginx-container custom-nginx
Creates an image named custom-nginx
from the nginx-container
container.
12. docker exec
This command runs new commands inside a running container.
Example:
docker run --name ubuntu_1 --rm -i -t ubuntu bash
Creates a new container named ubuntu_1
and opens a Bash session.
13. docker rmi
This command removes an image from the host system, freeing up space.
Example:
docker rmi custom-nginx
This removes the custom-nginx
image from the system.
14. docker cp
This command copies files and directories between the container and the host.
Example:
docker cp test-container:/1/2/random.conf ./test
Copies the random.conf
file from the test-container
to the ./test
directory on the host.
15. docker logs
This command fetches the logs for all Docker containers, making it useful for debugging.
Example:
docker logs nginx-container
16. docker info
This simple command displays detailed information about the Docker host and system.
Example:
docker info
17. docker logout
This command logs you out from the Docker registry.
Example:
docker logout
18. docker inspect
This command provides detailed information about containers or images, such as the checksum, layers, or container’s IP address.
Example:
docker inspect nginx-container
19. docker history
This command shows the history of an image, including any changes or layers made to it.
Example:
docker history ubuntu:latest
20. docker push
This command uploads a Docker image to a registry, such as Docker Hub.
Example:
docker push custom-nginx
Pushes the custom-nginx
image to Docker Hub.
Conclusion: Docker Commands to Boost Your Productivity
These 20 commands represent the essentials of managing Docker containers efficiently. By mastering these commands, you’ll be able to create, manage, and troubleshoot your Docker containers with ease.
Docker allows developers to build environments that are portable, reproducible, and isolated. Whether you’re working on local development, testing, or deploying applications in the cloud, Docker can significantly simplify your workflow.
Which Docker commands do you use the most? Share your favorite commands and how they have helped streamline your container management process in the comments!