Hello, fellow developers and tech enthusiasts! 👋
In today’s fast-paced software world, Docker has become an indispensable tool for building, shipping, and running applications. It allows you to package your applications and their dependencies into lightweight, portable, and self-sufficient units called containers. But understanding how to interact with these containers is key to truly leveraging Docker’s power.
This blog post is your ultimate guide to mastering 30 essential Docker commands specifically related to container management. We’ll dive deep into each command, providing clear explanations, practical examples, and even some emojis to make learning fun! By the end of this guide, you’ll be able to manage your Docker containers like a seasoned pro. Let’s get started! 🚀
Understanding Docker Commands: A Quick Note
Most Docker commands follow a similar structure: docker [command] [subcommand] [options] [arguments]
. For example, docker container ls
lists containers, where container
is the subcommand and ls
is the specific action. For simplicity, we’ll often just refer to docker ls
or docker run
as the main command, but keep in mind that docker container
is often implied or explicitly used for clarity.
Category 1: Lifecycle Management 🔄
These commands allow you to create, start, stop, restart, and remove your containers.
-
docker run
- Description: Creates a new container from an image and runs a command in it. This is often the first command you’ll use.
- Usage:
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
- Example:
docker run -d -p 80:80 --name my-nginx nginx:latest
-d
: Detached mode (run in background)-p 80:80
: Publish port 80 of the container to port 80 on the host--name my-nginx
: Assign a custom name to the containernginx:latest
: The image to use
- Output: (A long container ID)
-
docker start
- Description: Starts one or more stopped containers.
- Usage:
docker start [OPTIONS] CONTAINER [CONTAINER...]
- Example:
docker start my-nginx
- Output:
my-nginx
-
docker stop
- Description: Stops one or more running containers gracefully. It sends a
SIGTERM
signal, allowing the container to shut down properly. - Usage:
docker stop [OPTIONS] CONTAINER [CONTAINER...]
- Example:
docker stop my-nginx
- Output:
my-nginx
- Description: Stops one or more running containers gracefully. It sends a
-
docker restart
- Description: Restarts one or more containers. It’s essentially a
stop
followed by astart
. - Usage:
docker restart [OPTIONS] CONTAINER [CONTAINER...]
- Example:
docker restart my-nginx
- Output:
my-nginx
- Description: Restarts one or more containers. It’s essentially a
-
docker kill
- Description: Kills one or more running containers forcefully. It sends a
SIGKILL
signal, which immediately terminates the process. Use this whenstop
doesn’t work. - Usage:
docker kill [OPTIONS] CONTAINER [CONTAINER...]
- Example:
docker kill my-nginx
- Output:
my-nginx
- Description: Kills one or more running containers forcefully. It sends a
-
docker rm
- Description: Removes one or more stopped containers. You cannot remove a running container unless you use the
-f
(force) option. - Usage:
docker rm [OPTIONS] CONTAINER [CONTAINER...]
- Example:
docker rm my-nginx
- Output:
my-nginx
- Description: Removes one or more stopped containers. You cannot remove a running container unless you use the
-
docker pause
- Description: Pauses all processes within one or more running containers. This freezes the container’s state without stopping it.
- Usage:
docker pause CONTAINER [CONTAINER...]
- Example:
docker pause my-nginx
- Output:
my-nginx
-
docker unpause
- Description: Unpauses all processes within one or more paused containers.
- Usage:
docker unpause CONTAINER [CONTAINER...]
- Example:
docker unpause my-nginx
- Output:
my-nginx
Category 2: Inspection & Monitoring 👀
These commands help you get information about your containers and monitor their activity.
-
docker ps
- Description: Lists running containers. It’s one of the most frequently used commands.
- Usage:
docker ps [OPTIONS]
- Example:
docker ps -a
(shows all containers, including stopped ones) - Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a1b2c3d4e5f6 nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 0.0.0.0:80->80/tcp my-nginx
-
docker logs
- Description: Fetches the logs of a container. Useful for debugging and monitoring application output.
- Usage:
docker logs [OPTIONS] CONTAINER
- Example:
docker logs -f my-nginx
(-f
follows the log output) - Output: (Stream of Nginx access and error logs)
-
docker inspect
- Description: Returns low-level information about Docker objects (containers, images, volumes, networks) in JSON format.
- Usage:
docker inspect [OPTIONS] NAME|ID [NAME|ID...]
- Example:
docker inspect my-nginx
- Output: (Large JSON output with detailed container configuration, network settings, etc.)
-
docker stats
- Description: Displays a live stream of container resource usage statistics (CPU, memory, network I/O, block I/O).
- Usage:
docker stats [OPTIONS] [CONTAINER...]
- Example:
docker stats my-nginx
- Output:
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS a1b2c3d4e5f6 my-nginx 0.00% 4.156MiB / 7.795GiB 0.05% 750B / 0B 0B / 0B 1
-
docker top
- Description: Displays the running processes of a container. Similar to the
top
command in Linux, but for a specific container. - Usage:
docker top CONTAINER [OPTIONS]
- Example:
docker top my-nginx
- Output:
UID PID PPID C STIME TTY TIME CMD root 1 0 0 10:30 ? 00:00:00 nginx: master process nginx -g daemon off; 101 7 1 0 10:30 ? 00:00:00 nginx: worker process
- Description: Displays the running processes of a container. Similar to the
-
docker port
- Description: Lists port mappings or a specific mapping for the container.
- Usage:
docker port CONTAINER [PRIVATE_PORT[/PROTO]]
- Example:
docker port my-nginx 80/tcp
- Output:
0.0.0.0:80
-
docker diff
- Description: Inspects changes on files or directories in a container’s filesystem. Useful for seeing what files have been added (A), deleted (D), or modified (C).
- Usage:
docker diff CONTAINER
- Example:
docker diff my-nginx
- Output:
C /var/cache/nginx A /var/cache/nginx/client_temp A /var/cache/nginx/fastcgi_temp ...
Category 3: Interaction & Debugging 🛠️
These commands allow you to interact with a running container’s shell or copy files.
-
docker exec
- Description: Executes a command inside a running container. This is extremely useful for debugging, inspecting, or running one-off tasks.
- Usage:
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
- Example:
docker exec -it my-nginx bash
(opens an interactive bash shell inside the container)-i
: Interactive (keep STDIN open)-t
: Allocate a pseudo-TTY
- Output: (Bash prompt inside the container)
-
docker attach
- Description: Attaches your local standard input, output, and error streams to a running container’s main process. Useful if you want to see the output of the main process or interact with it.
- Usage:
docker attach [OPTIONS] CONTAINER
- Example:
docker attach my-nginx
(You’ll see Nginx logs and potentially lose control if it’s not designed for interactive input) - Note: Often
docker exec
is preferred for interactive shells as it doesn’t attach to the main process, allowing the container’s primary process to continue running normally.
-
docker cp
- Description: Copies files/folders between a container and the local filesystem.
- Usage:
docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH
ordocker cp [OPTIONS] SRC_PATH CONTAINER:DEST_PATH
- Example:
docker cp my-nginx:/etc/nginx/nginx.conf .
(Copiesnginx.conf
from container to current directory)docker cp ./my_new_config.conf my-nginx:/etc/nginx/conf.d/
(Copies local file to container)
- Output: (No output on success)
Category 4: Data & Configuration 💾
Commands for modifying container properties or network connectivity.
-
docker rename
- Description: Renames an existing container.
- Usage:
docker rename OLD_NAME NEW_NAME
- Example:
docker rename my-nginx web-server-prod
- Output: (No output on success)
-
docker update
- Description: Updates the configuration of one or more running containers. This includes resource constraints like CPU shares, memory, etc.
- Usage:
docker update [OPTIONS] CONTAINER [CONTAINER...]
- Example:
docker update --memory 512m my-nginx
(Sets container memory limit to 512MB) - Output:
my-nginx
-
docker network connect
- Description: Connects a container to a network. Containers can be connected to multiple networks.
- Usage:
docker network connect [OPTIONS] NETWORK CONTAINER
- Example:
docker network connect my-app-network my-nginx
- Output: (No output on success)
-
docker network disconnect
- Description: Disconnects a container from a network.
- Usage:
docker network disconnect [OPTIONS] NETWORK CONTAINER
- Example:
docker network disconnect my-app-network my-nginx
- Output: (No output on success)
Category 5: Advanced Operations & Image Management (Relevant to Containers) 🚀
These commands deal with more advanced scenarios, including saving container states to new images.
-
docker commit
- Description: Creates a new image from a container’s changes. Useful for saving the state of a container after making modifications.
- Usage:
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
- Example:
docker commit -m "Added custom config" my-nginx my-custom-nginx:v1.0
-m
: Commit message
- Output: (New image ID)
-
docker export
- Description: Exports a container’s filesystem as a tar archive. This is different from
docker commit
as it exports the entire filesystem, not just the changes, and doesn’t create an image directly. - Usage:
docker export [OPTIONS] CONTAINER
- Example:
docker export my-nginx > my-nginx-fs.tar
- Output: (Tar archive created)
- Description: Exports a container’s filesystem as a tar archive. This is different from
-
docker import
- Description: Imports the contents from a tarball to create a filesystem image. This is the reverse of
docker export
. - Usage:
docker import [OPTIONS] FILE|URL|- [REPOSITORY[:TAG]]
- Example:
cat my-nginx-fs.tar | docker import - my-nginx-imported:latest
- Output: (New image ID)
- Description: Imports the contents from a tarball to create a filesystem image. This is the reverse of
-
docker wait
- Description: Blocks until one or more containers stop, then prints their exit codes. Useful in scripting.
- Usage:
docker wait CONTAINER [CONTAINER...]
- Example:
docker wait my-nginx
(This command will wait untilmy-nginx
stops, then print its exit code) - Output:
(if successful exit)
-
docker events
- Description: Get real-time events from the Docker daemon. This includes container, image, volume, and network events.
- Usage:
docker events [OPTIONS]
- Example:
docker events --filter 'type=container'
(Shows only container-related events) - Output: (Continuous stream of events like
container create
,container start
,container stop
)
Category 6: Cleanup & Maintenance 🧹
Commands for cleaning up unused resources and getting system information.
-
docker container prune
- Description: Removes all stopped containers. A quick way to clean up your Docker environment.
- Usage:
docker container prune [OPTIONS]
- Example:
docker container prune -f
(-f
or--force
to skip confirmation prompt) - Output:
Total reclaimed space: 123MB
-
docker system prune
- Description: Removes all unused Docker objects (containers, images, networks, build cache, and optionally volumes). This is a more comprehensive cleanup.
- Usage:
docker system prune [OPTIONS]
- Example:
docker system prune -a --volumes
(Removes all unused images and volumes) - Output:
WARNING! This will remove: - all stopped containers - all networks not used by at least one container - all dangling images - all build cache Are you sure you want to continue? [y/N] y ... Total reclaimed space: 1.5GB
-
docker info
- Description: Displays system-wide information about the Docker installation, including the number of containers, images, volumes, and networks. Useful for quick health checks.
- Usage:
docker info [OPTIONS]
- Example:
docker info
-
Output:
Client: Context: default Debug Mode: false Server: Containers: 3 Running: 1 Paused: 0 Stopped: 2 Images: 15 Server Version: 20.10.12 Storage Driver: overlay2 ... (much more detail)
Conclusion ✨
Phew! That was a lot of commands, but each one serves a unique purpose in managing your Docker containers effectively. From bringing your applications to life with docker run
to peeking inside with docker exec
and keeping your environment clean with docker prune
, you now have a powerful arsenal at your fingertips.
The best way to master these commands is to practice. Spin up some containers, experiment with the commands, and observe their effects. Docker’s command-line interface is incredibly robust and intuitive once you get the hang of it.
Happy containerizing! If you have any favorite Docker commands or tips, feel free to share them in the comments below. 👇 G