Docker Command Cheat Sheet: The Ultimate Guide for Container Enthusiasts
Explore the World of Docker: Pull, Push, and List Images Like a Pro
Table of contents
No headings in the article.
Here's a list of common Docker commands with explanations:
docker run
Description: Run a command in a new container.
Parameters:
-d
or--detach
: Run the container in the background.-it
: Allocate a pseudo-TTY and keep stdin open (interactive mode).--rm
: Automatically remove the container when it exits.-p
or--publish
: Publish a container's port(s) to the host.-v
or--volume
: Bind mount a volume.-e
or--env
: Set environment variables.--name
: Assign a name to the container.
Example:
docker run -d -p 80:80 --name my-web-server nginx
docker build
Description: Build an image from a Dockerfile.
Parameters:
-t
or--tag
: Name and optionally a tag in thename:tag
format.--file
or-f
: Name of the Dockerfile (default is 'Dockerfile')..
: Path to the build context (usually the current directory).
Example:
docker build -t my-image .
docker pull
Description: Pull an image or a repository from a registry.
Parameters:
--all-tags
: Download all tagged images in the repository.
Example:
docker pull ubuntu:latest
docker push
Description: Push an image or a repository to a registry.
Parameters:
--all-tags
: Push all tagged images into the repository.
Example:
docker push my-image:latest
docker images
Description: List images.
Parameters:
-a
or--all
: Show all images (default hides intermediate images).--no-trunc
: Don't truncate output.
Example:
docker images
docker ps
Description: List containers.
Parameters:
-a
or--all
: Show all containers (default shows only running).-q
or--quiet
: Only display container IDs.
Example:
docker ps
docker stop
Description: Stop one or more running containers.
Parameters:
container
: ID or name of the container to stop.
Example:
docker stop my-web-server
docker start
Description: Start one or more stopped containers.
Parameters:
container
: ID or name of the container to start.
Example:
docker start my-web-server
docker rm
Description: Remove one or more containers.
Parameters:
-f
or--force
: Force the removal of a running container.container
: ID or name of the container to remove.
Example:
docker rm my-web-server
docker rmi
Description: Remove one or more images.
Parameters:
-f
or--force
: Force removal of the image.image
: ID or name of the image to remove.
Example:
docker rmi my-image:latest
To generalize and combine commands, you can use shell scripting, command substitution, or pipes. For example, you can stop and remove all running containers using a single command:
docker stop $(docker ps -q) && docker rm $(docker ps -a -q)
In this example, we use command substitution $(...)
to get the list of running container IDs from docker ps -q
and pass them to docker stop
. Then, we use the &&
operator to chain the commands, so after stopping the containers, we use docker ps -a -q
to get the list of all container IDs and pass them to docker rm
.
Another example is removing all dangling images:
docker rmi $(docker images -f "dangling=true" -q)
Here, we use docker images -f "dangling=true" -q
to get the list of dangling image IDs and pass them to docker rmi
using command substitution.
Remember to be cautious when using commands that delete containers or images, especially if you are combining them with other commands. It's a good practice to check the output of the commands you're chaining together before executing them to prevent unintentional deletions.
As you become more familiar with Docker commands and their parameters, you can create more complex sequences and use them in your daily work to automate tasks and increase efficiency.