Hey friend! Docker is an incredible tool that has revolutionized application development and deployment. As a fellow tech geek, I‘m excited to share this comprehensive guide on 15 must-know Docker commands to manage containers.
With the rise of microservices and containerization, Docker has become a critical skill for developers and sysadmins alike. But Docker‘s power comes from mastering its CLI toolset.
![15 Docker Commands to Manage Containers [List, Stop, Start, Remove and More]](https://i0.wp.com/www.toptensocialmedia.com/wp-content/uploads/2021/08/docker-commands-containers-1200x675.jpg.webp?w=662&ssl=1)
Let‘s quickly recap what containers and Docker images are:
-
Containers – these are running instances of Docker images. You can think of them like lightweight virtual machines.
-
Images – these are read-only templates used to create containers. Images are composed of file system snapshots and startup commands.
According to Datadog‘s 2022 Container Report, containers adoption has grown significantly:
With over 79% of companies now running containers in production, it‘s clear these skills are in high demand.
So in this guide, I‘ll share my top 15 Docker commands I use daily as a DevOps engineer to manage containers like a pro. I‘ll cover everything from running and stopping containers to monitoring, debugging and copying files.
Let‘s dive in!
1. docker run
The docker run
command is used to launch new containers from images. It‘s one of the most common Docker commands.
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Here are some handy flags:
-d
– Run the container in detached (background) mode--name
– Name the container-p
– Publish container ports to the host
Let‘s run a new Nginx container:
docker run -d --name my-nginx -p 80:80 nginx
This launches an Nginx container named "my-nginx" in the background and exposes port 80.
Pro tip: The docker run
command will automatically pull the image if it‘s not present on the host.
2. docker ps
To view running containers, use the docker ps
command:
docker ps [OPTIONS]
For example:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a87ecb4cdf4d nginx "nginx" 5 seconds ago Up 3 seconds 80/tcp my-nginx
Pass the -a
flag to view stopped containers too.
3. docker stop
To stop a running container, use docker stop
:
docker stop [OPTIONS] CONTAINER [CONTAINER...]
You can pass a container‘s ID or name:
docker stop my-nginx
This gracefully stops the container by sending a SIGTERM. To forcefully kill it, use docker kill
.
4. docker rm
Once containers have stopped, they won‘t be cleaned up automatically. Use docker rm
to remove them manually:
docker rm [OPTIONS] CONTAINER [CONTAINER...]
For example:
docker rm my-nginx
This will free up disk space by deleting stopped containers.
5. docker logs
To view container logs, use the docker logs
command:
docker logs [OPTIONS] CONTAINER
For example, to follow the live logs for a container:
docker logs -f my-nginx
This is super helpful for debugging issues or monitoring container activity.
6. docker exec
To execute a command inside a running container, use docker exec
:
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
For example, to open a Bash shell inside your Nginx container:
docker exec -it my-nginx bash
The -it
provides an interactive terminal. This allows you to explore and troubleshoot containers.
7. docker cp
The docker cp
command lets you copy files between containers and the local filesystem:
docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
For example, to copy your Nginx config from the container to your current directory:
docker cp my-nginx:/etc/nginx/nginx.conf ./nginx.conf
This bidirectional copy works great for backing up files or pushing config changes.
8. docker diff
Want to see filesystem changes within a container? Use docker diff
:
docker diff [OPTIONS] CONTAINER
This shows changed files and directories since the container was started from the image. Here‘s sample output:
A /tmp/new_file
C /var
D /usr/local/bin
Useful for inspecting container state and verifying config changes.
9. docker top
To see running processes inside a container, use docker top
:
docker top [OPTIONS] CONTAINER [ps OPTIONS]
For example:
docker top my-nginx
UID PID PPID C STIME TTY TIME CMD
root 132 0 0 02:34 ? 00:00:00 nginx: master process nginx -g daemon off;
101 158 132 0 02:34 ? 00:00:00 nginx: worker process
root 172 0 0 02:34 ? 00:00:00 nginx: cache manager process
This provides insight into how containers function and resource usage.
10. docker stats
To monitor real-time container resource usage statistics, use docker stats
:
docker stats [OPTIONS] [CONTAINER...]
For example:
CONTAINER CPU % MEM USAGE / LIMIT MEM %
my-nginx 0.00% 24.4MiB / 1.945GiB 1.24%
This gives you metrics on CPU, RAM utilization, disk I/O and network traffic. Helpful for performance tuning and bottleneck identification.
11. docker inspect
The docker inspect
command provides detailed information about a container:
docker inspect [OPTIONS] NAME|ID [NAME|ID...]
This includes the container config, runtime details, network settings, mounted volumes, and more.
Here‘s a snippet:
"Config": {
"Hostname": "a87ecb4cdf4d",
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.21.3"
],
"ExposedPorts": {
"80/tcp": {}
}
},
"Name": "/my-nginx",
"RestartCount": 0,
"State": {
"Status": "running",
"Running": true,
},
"NetworkSettings": {
"Ports": {
"80/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "80"
}
]
}
}
Great for debugging and understanding what‘s happening under the hood.
12. docker rename
You can rename a container with the docker rename
command:
docker rename CONTAINER NEW_NAME
For example:
docker rename my-nginx my-new-nginx
Simple but useful for keeping your environment organized when managing multiple containers.
13. docker export
To export a container‘s filesystem into a tar archive, use docker export
:
docker export [OPTIONS] CONTAINER
For example:
docker export my-nginx -o my_nginx.tar
This exports the container into a tarball that can be shared or reimported.
14. docker kill
To forcefully kill a running container, use docker kill
:
docker kill [OPTIONS] CONTAINER [CONTAINER...]
This instantly sends a SIGKILL signal, terminating the container immediately. Compare this to docker stop
which gracefully stops the process.
15. docker system
Finally, docker system
provides system-wide management capabilities:
docker system COMMAND
For example, docker system prune
allows you to clean up unused containers, images, volumes, and networks.
Other subcommands let you manage disk usage, check Docker health, view real-time events, and more.
Key Takeaways
There you have it – my top 15 Docker commands for managing containers like a pro!
Here are some key takeaways:
docker run
launches new containers from imagesdocker ps
anddocker logs
allow monitoring and debuggingdocker stop
anddocker kill
stop running containersdocker exec
runs commands inside containersdocker cp
copies files between containers and the hostdocker stats
anddocker top
give resource usage metricsdocker inspect
provides detailed container informationdocker system
handles system-wide tasks like prune
With robust container management capabilities, Docker enables you to build, deploy, monitor, and maintain applications easily. These commands are essential knowledge for developers and sysadmins working with containerized environments.
For further learning, I suggest checking out Docker‘s official command line reference. And let me know if you have any other favorite container management tips!
Hope this guide helps you become a Docker pro. Now go forth and containerize all the things!