DEV Community

Cover image for ๐Ÿ› ๏ธ Mastering Docker Commands: Your Daily Toolkit
SAHIL
SAHIL

Posted on

๐Ÿ› ๏ธ Mastering Docker Commands: Your Daily Toolkit

You've learned the what, why, and how behind Docker's architecture and filesystem. Now, let's get deeply practical with the commands you'll use every single day to interact with Docker. This post will be your go-to reference for managing images, containers, and your Docker environment.

We've organized the essential commands into categories for easy lookup:

1. ๐Ÿ” Docker System & Information Commands
These commands provide overall status and management of your Docker installation.

Command Description Example
docker --version Displays the Docker client version. docker --version
docker info Provides detailed system-wide information about your Docker installation. docker info
docker system df Shows disk space usage by Docker objects (images, containers, volumes). docker system df
docker system prune Removes unused Docker data (stopped containers, unused networks, dangling images). docker system prune

2. ๐Ÿ–ผ๏ธ Image Management Commands
Images are the blueprints. These commands help you find, download, list, and remove them.

Command Description Example
docker pull Downloads an image from a registry (default is Docker Hub) to your local machine. docker pull ubuntu:latest
docker images (or docker image ls) Lists all images stored locally on your machine. docker images
docker search Searches Docker Hub for images based on a keyword. docker search nginx
docker inspect Displays detailed JSON configuration information about a Docker object. docker inspect nginx:latest
docker rmi Removes one or more images. docker rmi ubuntu:latest

3. ๐Ÿšข Container Lifecycle Commands
These are the workhorses for creating, starting, stopping, and managing individual containers.

Command Description Example
docker run Creates and starts a new container from an image. docker run -it --name my-alpine alpine sh
docker create Creates a container but does not start it. docker create --name my-db postgres
docker start Starts one or more stopped containers. docker start my-db
docker stop Stops one or more running containers gracefully. docker stop my-web-server
docker restart Restarts one or more containers. docker restart my-db
docker kill Kills one or more running containers forcefully (sends SIGKILL). docker kill my-db
docker ps (or docker container ls) Lists running containers. Add -a or --all to list all containers. docker ps -a
docker rm Removes one or more stopped containers. Use -f to force removal of a running container. docker rm my-alpine

4. โœจ docker run Options (Flags)
The flexibility of docker run comes from its flags. Here are the most important ones and their meanings:

Option Meaning Example Use
-it Interactive Mode. Stands for -i (interactive) and -t (TTY). Essential for shell access. docker run -it alpine sh
-d Detached Mode. Runs the container in the background. docker run -d nginx
-p <host>:<container> Port Mapping. Publishes a container's port to a host port. docker run -p 8080:80 nginx
--name <name> Assigns a memorable name to the container. docker run --name my-app ...
--rm Automatically removes the container when it exits. Great for temporary tasks. docker run --rm alpine ...
-v <source>:<dest> Volume Mount. Mounts a host path or named volume for data persistence. docker run -v my-data:/data ...

5. ๐Ÿ”ฌ Container Interaction & Monitoring Commands
Once a container is running, these commands help you interact with it, monitor performance, and debug issues.

Command Description Example
docker exec Executes a command inside a running container. docker exec -it my-app sh
docker logs Fetches the logs of a container. Use -f to stream logs in real-time. docker logs -f my-web-server
docker top Displays the running processes within a container. docker top my-web-server
docker stats Displays a live stream of resource usage (CPU, memory, I/O) for running containers. docker stats
docker cp Copies files/folders between a container and the local filesystem. docker cp my-app:/app/config.ini .
docker attach Attaches your terminal to a running container's streams (use with caution). docker attach my-alpine

What's Next?
You now have a robust toolkit and reference guide for managing every aspect of a container's life.

In our next post, we will finally use the command-line knowledge to build custom images. We'll dive into the architecture of a Dockerfile and practice writing efficient instructions!

Top comments (0)