Introduction
Docker is a set of platform-as-a-service products for developing and deploying containerized applications.
Containers are lightweight, portable virtual environments that developers can share without risking inconsistencies in development. Many organizations have switched from virtual machines to Docker containers to eliminate the resource overhead and streamline the SDLC of their product.
Like any other software, Docker introduces users to new terminology. Users need to get acquainted with concepts such as Dockerfiles, images, containers, and other Docker-specific terms. Once you have mastered the main ideas, the next step is to become familiar with Docker commands. A list of all the commands and options is extensive and it would take time to learn them all by heart.
Whether you are new to Docker or have experience with containerization, it is always good to have a reference point for all the common Docker commands. This tutorial provides a list of the most common Docker commands and a downloadable cheat sheet.
List of Docker Commands
Docker Container Management Commands
This section features the essential commands related to the lifecycle of Docker containers. Learn how to create, manage, and remove containers from your Docker system using the below commands.
See the containers currently running on the system:
docker ps
See all the containers, both running and non-running:
docker ps -a
Create a container (without starting it):
docker create [image]
Create an interactive container with pseudo-TTY:
docker create -it [image]
Rename an existing container:
docker rename [container] [new-name]
Delete a container (if it is not running):
docker rm [container]
Forcefully remove a container, even if it is running:
docker rm -f [container]
View logs for a running container:
docker logs [container]
Retrieve logs created before a specific point in time:
docker logs -f --until=[interval] [container]
Note: Refer to Docker Container Logs: Commands & Best Practices for a more in-depth look at managing Docker container logs.
View real-time events for a container:
docker events [container]
Update the configuration of one or more containers:
docker update [container]
View port mapping for a container:
docker port [container]
Show running processes in a container:
docker top [container]
View live resource usage statistics for a container:
docker stats [container]
Show changes to files or directories on the filesystem:
docker diff [container]
Copy a local file to a directory in a container:
docker cp [file-path] CONTAINER:[path]
Visit our detailed guide on how to update Docker container and image to the latest version for more information.
Running a Container
The following commands show you how to start and stop processes in a container and how to manage container execution.
Run a command in a container based on an image:
docker run [image] [command]
Create, start, and provide a custom name for the container:
docker run --name [container-name] [image]
Establish a connection with a container by mapping a host port to a container port:
docker run -p [host-port]:[container-port] [image]
Run a container and remove it after it stops:
docker run --rm [image]
Run a detached (background) container:
docker run -d [image]
Start an interactive process, such as a shell, in a container:
docker run -it [image]
Start a container:
docker start [container]
Stop a running container:
docker stop [container]
Stop a running container and start it up again:
docker restart [container]
Pause processes in a running container:
docker pause [container]
Resume processes in a running container:
docker unpause [container]
Block a container until others stop (after which it prints their exit codes):
docker wait [container]
Kill a container by sending a SIGKILL to a running container:
docker kill [container]
Attach local standard input, output, and error streams to a running container:
docker attach [container]
Run a shell inside a running container:
docker exec -it [container] [shell]
Note: If you are still unsure of how Docker images and containers differ, check out the article on Images vs Containers.
Docker Image Commands
Below you will find all the necessary commands for working with Docker images.
Create an image from a Dockerfile:
docker build [dockerfile-path]
Build an image from a Dockerfile located in the current directory:
docker build .
Create an image from a Dockerfile and tag it.
docker build -t [name]:[tag] [dockerfile-path]
Specify a file to build from:
docker build -f [file-path]
Pull an image from a registry:
docker pull [image]
Push an image to a registry:
docker push [image]
Create an image from a tarball:
docker import [url/file]
Create an image from a container:
docker commit [container] [new-image]
Tag an image:
docker tag [image] [image]:[tag]
Show all locally stored top-level images:
docker images
Show history for an image:
docker history [image]
Remove an image:
docker rmi [image]
Load an image from a tar archive or stdin:
docker load --image [tar-file]
Save an image to a tar archive file:
docker save [image] > [tar-file]
Remove unused images:
docker image prune
Networking
One of the most valuable features of Docker software is the ability to connect containers to each other and to other non-Docker workloads. This section covers network-related commands.
List available networks:
docker network ls
Remove one or more networks:
docker network rm [network]
Show information on one or more networks:
docker network inspect [network]
Connect a container to a network:
docker network connect [network] [container]
Disconnect a container from a network:
docker network disconnect [network] [container]
Docker General Management Commands
Once you set up your containers, you will need to know how to get all the important information for managing them. The following commands will allow you to obtain general information about the system and connect to remote Docker resources.
Log in to a Docker registry:
docker login
Log out of a Docker registry:
docker logout
List low-level information on Docker objects:
docker inspect [object]
Show the version of the local Docker installation:
docker version
Display information about the system:
docker info
Remove unused images, containers, and networks:
docker system prune
Plugin Management Commands
Docker plugins extend Docker’s functionality and help users connect with other popular services. The commands below let you add, manage, and remove plugins on your system.
Enable a Docker plugin:
docker plugin enable [plugin]
Disable a Docker plugin:
docker plugin disable [plugin]
Create a plugin from config.json and rootfs:
docker plugin create [plugin] [path-to-data]
View details about a plugin:
docker plugin inspect [plugin]
Remove a plugin:
docker plugin rm [plugin]
Docker Cheat Sheet
You can find all the listed commands in a convenient one-page reference sheet below. To save it for future use, you can also download our Docker Command Cheat Sheet.
Conclusion
This list of commonly used Docker commands should help you become more familiar with them. The cheat sheet will be helpful when you want to create and manage containers, images, and networks.