G: Hey there, fellow developers and tech enthusiasts! 👋 Have you ever felt overwhelmed by the sheer number of Docker commands? You’re not alone! Docker has revolutionized how we build, ship, and run applications, making development workflows smoother and more consistent. But to truly harness its power, you need to master its command-line interface (CLI).
This guide is your one-stop shop for the 30 most essential Docker commands you’ll use daily in real-world scenarios. We’ll break them down, explain their purpose, show practical examples, and even throw in some emojis for good measure! By the end of this read, you’ll be confidently navigating Docker like a pro. Let’s dive in! 🚀
Why Docker Matters (A Quick Refresh) 💡
Before we jump into commands, let’s quickly reiterate why Docker is so crucial:
- Consistency: “It works on my machine” is no longer an excuse. Docker containers package your application and its dependencies, ensuring it runs identically everywhere.
- Isolation: Each container is an isolated environment, preventing conflicts between applications or dependencies.
- Portability: Containers can run on any system that has Docker installed, regardless of the underlying OS.
- Efficiency: Containers are lightweight and start quickly, making them ideal for microservices and CI/CD pipelines.
Now, let’s get to the core: the commands!
I. Getting Started & Basic Information Commands 🚀
These commands help you check your Docker installation and get general information about your Docker daemon.
1. docker version
- What it does: Displays the Docker client and server (daemon) version information. Useful for troubleshooting compatibility issues.
- Syntax:
docker version
- Example:
docker version
- Explanation: Shows details like API version, Go version, Git commit, OS/Arch, etc., for both your Docker client (what you type into) and the Docker Engine daemon (what runs containers).
2. docker info
- What it does: Provides detailed information about your Docker environment, including the number of containers, images, storage driver, and more.
- Syntax:
docker info
- Example:
docker info
- Explanation: Gives you an overview of your Docker setup, helpful for understanding resource usage or debugging system-level issues.
II. Image Management Commands 🖼️
Docker images are the blueprints for your containers. These commands help you download, list, build, and remove them.
3. docker pull
- What it does: Downloads an image from a Docker registry (like Docker Hub) to your local machine.
- Syntax:
docker pull [OPTIONS] NAME[:TAG|@DIGEST]
- Example:
docker pull ubuntu:latest docker pull nginx:1.21.4
- Explanation:
ubuntu:latest
pulls the most recent Ubuntu image. If you don’t specify a tag,latest
is assumed by default. Always good practice to specify a tag for reproducibility!
4. docker images
(or docker image ls
)
- What it does: Lists all Docker images stored on your local machine.
- Syntax:
docker images [OPTIONS] [REPOSITORY[:TAG]]
- Example:
docker images docker images -a # Show all images, including intermediate layers
- Explanation: Shows image ID, repository, tag, size, and when it was created. Essential for seeing what you have locally.
5. docker rmi
(or docker image rm
)
- What it does: Removes one or more Docker images from your local machine.
- Syntax:
docker rmi [OPTIONS] IMAGE [IMAGE...]
- Example:
docker rmi nginx:1.21.4 docker rmi -f my-app:v1 # Force removal of an image
- Explanation: Use the image ID or
REPOSITORY:TAG
to specify which image to remove. The-f
flag is useful if an image is being used by a container (you’ll need to remove the container first or use-f
to force).
6. docker build
- What it does: Builds a new Docker image from a
Dockerfile
and a “context” (a set of files at a specified path). - Syntax:
docker build [OPTIONS] PATH | URL | -
- Example:
Assuming you have a
Dockerfile
in your current directory:docker build -t my-web-app:1.0 .
- Explanation: The
-t
flag tags your image with a name and version (my-web-app:1.0
). The.
at the end specifies the build context (the current directory), where Docker will look for theDockerfile
and other files.
7. docker history
- What it does: Shows the history of a Docker image, layer by layer, including the commands used to create each layer.
- Syntax:
docker history [OPTIONS] IMAGE
- Example:
docker history ubuntu:latest
- Explanation: Great for understanding how an image was constructed and for debugging large image sizes. Each line represents a layer.
8. docker search
- What it does: Searches Docker Hub for images.
- Syntax:
docker search [OPTIONS] TERM
- Example:
docker search nginx
- Explanation: Helps you discover public images available on Docker Hub. It shows image name, description, stars, official status, and automation.
III. Container Life Cycle & Execution Commands ✅
These are the most frequently used commands, allowing you to run, manage, and interact with your containers.
9. docker run
- What it does: Creates and starts a new container from a specified image. This is probably the most used command!
- Syntax:
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
- Key Options & Examples:
- Basic run:
docker run hello-world
Explanation: Downloads
hello-world
image if not present and runs a container that prints a message then exits. - Detached mode (
-d
): Runs the container in the background.docker run -d --name my-nginx -p 80:80 nginx:latest
Explanation: Runs Nginx in the background, maps host port 80 to container port 80, and names the container
my-nginx
. - Interactive TTY (
-it
): Provides an interactive shell inside the container.docker run -it ubuntu:latest bash
Explanation: Starts an Ubuntu container and drops you into a bash shell, allowing you to interact with the container’s environment.
- Port mapping (
-p
): Maps a host port to a container port.docker run -p 8080:80 my-web-app:1.0
Explanation: Access your web app running on port 80 inside the container via port 8080 on your host machine.
- Volume mounting (
-v
): Mounts a host directory or named volume into the container for persistent data.docker run -d -p 80:80 -v /path/on/host:/usr/share/nginx/html nginx:latest docker run -d -p 80:80 -v my-nginx-data:/usr/share/nginx/html nginx:latest
Explanation: The first example mounts a local directory. The second uses a Docker named volume (
my-nginx-data
) for data persistence. - Naming a container (
--name
): Assigns a human-readable name to your container.docker run --name my-database -e POSTGRES_PASSWORD=mysecretpassword postgres:latest
Explanation: Makes it easier to refer to the container later instead of using its ID.
- Basic run:
10. docker ps
- What it does: Lists currently running Docker containers.
- Syntax:
docker ps [OPTIONS]
- Example:
docker ps docker ps -a # Show all containers, including stopped ones docker ps -s # Show total file sizes
- Explanation: Shows container ID, image, command, creation time, status, ports, and name.
-a
is incredibly useful for seeing containers that have exited.
11. docker start
- What it does: Starts one or more stopped containers.
- Syntax:
docker start [OPTIONS] CONTAINER [CONTAINER...]
- Example:
docker start my-nginx docker start c0a1b2c3d4e5
- Explanation: You can use either the container name or its ID.
12. docker stop
- What it does: Gracefully stops one or more running containers.
- Syntax:
docker stop [OPTIONS] CONTAINER [CONTAINER...]
- Example:
docker stop my-nginx
- Explanation: Sends a
SIGTERM
signal to the container’s main process, allowing it to shut down cleanly. After a timeout (default 10 seconds), it sendsSIGKILL
.
13. docker restart
- What it does: Stops and then starts one or more containers.
- Syntax:
docker restart [OPTIONS] CONTAINER [CONTAINER...]
- Example:
docker restart my-app-container
- Explanation: Useful for applying configuration changes or simply resetting a container.
14. docker rm
(or docker container rm
)
- What it does: Removes one or more stopped containers.
- Syntax:
docker rm [OPTIONS] CONTAINER [CONTAINER...]
- Example:
docker rm my-nginx docker rm -f my-app-container # Force remove a running container
- Explanation: You can only remove stopped containers by default. Use
-f
(force) to remove a running container, which will first stop it then remove it.
15. docker exec
- What it does: Runs a command inside a running container.
- Syntax:
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
- Example:
docker exec -it my-nginx bash # Open a bash shell inside a running Nginx container docker exec my-database psql -U user_name -d db_name # Run a database command
- Explanation: This is incredibly powerful for debugging or performing administrative tasks within a running container without stopping it.
-it
is commonly used to get an interactive shell.
16. docker attach
- What it does: Attaches your terminal’s standard input, output, and error streams to a running container.
- Syntax:
docker attach [OPTIONS] CONTAINER
- Example:
docker attach my-app-container
- Explanation: If your container is running in detached mode (
-d
) and producing output to stdout/stderr,attach
allows you to see that output in real-time. To detach without stopping the container, useCTRL+P CTRL+Q
.
17. docker commit
- What it does: Creates a new image from a container’s changes.
- Syntax:
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
- Example:
docker commit my-modified-container my-custom-app:v2
- Explanation: While not recommended for production workflows (use
Dockerfile
instead for reproducibility), it’s handy for quick snapshots or debugging if you’ve made changes directly inside a container and want to save them.
IV. Data Management (Volumes & Files) Commands 📁
Managing persistent data is crucial for stateful applications. Docker volumes are the preferred way to do this.
18. docker volume create
- What it does: Creates a named Docker volume.
- Syntax:
docker volume create [OPTIONS] VOLUME
- Example:
docker volume create my-app-data
- Explanation: Named volumes are managed by Docker and are the best way to persist data generated by and used by Docker containers.
19. docker volume ls
- What it does: Lists all Docker volumes.
- Syntax:
docker volume ls [OPTIONS]
- Example:
docker volume ls
- Explanation: Shows you all the named volumes present on your Docker host.
20. docker volume rm
- What it does: Removes one or more Docker volumes.
- Syntax:
docker volume rm [OPTIONS] VOLUME [VOLUME...]
- Example:
docker volume rm my-app-data
- Explanation: Make sure no containers are using the volume before removing it, or use
-f
to force.
21. docker cp
- What it does: Copies files/folders between a container and the local filesystem.
- Syntax:
docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
ordocker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
- Example:
docker cp my-web-app:/app/logs/access.log . # Copy from container to host docker cp ./my-config.txt my-web-app:/app/config/ # Copy from host to container
- Explanation: Incredibly useful for getting logs out of a container or injecting configuration files without rebuilding an image.
V. Network Management Commands 🌐
Docker allows you to create and manage custom networks for better container communication and isolation.
22. docker network create
- What it does: Creates a new custom Docker network.
- Syntax:
docker network create [OPTIONS] NETWORK
- Example:
docker network create my-app-network
- Explanation: Containers connected to the same custom network can communicate with each other using their container names as hostnames, which simplifies discovery.
23. docker network ls
- What it does: Lists all Docker networks.
- Syntax:
docker network ls [OPTIONS]
- Example:
docker network ls
- Explanation: Shows bridge, host, null, and any custom networks you’ve created.
24. docker network rm
- What it does: Removes one or more Docker networks.
- Syntax:
docker network rm [OPTIONS] NETWORK [NETWORK...]
- Example:
docker network rm my-app-network
- Explanation: You can’t remove a network that has containers connected to it.
VI. Monitoring & Debugging Commands 📊
When things go wrong (and they will!), these commands are your best friends for peering into your containers.
25. docker logs
- What it does: Fetches the logs of a container.
- Syntax:
docker logs [OPTIONS] CONTAINER
- Example:
docker logs my-web-app docker logs -f my-web-app # Follow log output in real-time docker logs --since "20m" my-web-app # Logs from the last 20 minutes
- Explanation: Essential for seeing what’s happening inside your application.
-f
is liketail -f
.
26. docker inspect
- What it does: Returns detailed low-level information about Docker objects (containers, images, volumes, networks).
- Syntax:
docker inspect [OPTIONS] NAME|ID [NAME|ID...]
- Example:
docker inspect my-nginx docker inspect --format='{{.NetworkSettings.IPAddress}}' my-nginx
- Explanation: Outputs a JSON array with comprehensive data. The
--format
flag is incredibly useful for extracting specific pieces of information, like a container’s IP address.
27. docker stats
- What it does: Displays a live stream of resource usage statistics for running containers (CPU, memory, network I/O).
- Syntax:
docker stats [OPTIONS] [CONTAINER...]
- Example:
docker stats docker stats my-app-container db-container
- Explanation: Provides real-time performance monitoring. You’ll see CPU percentage, memory usage, network I/O, etc.
28. docker top
- What it does: Displays the running processes of a container.
- Syntax:
docker top CONTAINER [ps OPTIONS]
- Example:
docker top my-web-app
- Explanation: Similar to the
top
command on a Linux system, but scoped to processes running inside a specific Docker container. Useful for seeing what processes are consuming resources.
VII. System & Registry Operations 🧹🔐
These commands help you manage your Docker system resources and interact with image registries.
29. docker system prune
- What it does: Removes unused Docker objects (stopped containers, unused networks, dangling images, build cache).
- Syntax:
docker system prune [OPTIONS]
- Example:
docker system prune -a # Remove ALL unused images (not just dangling ones) docker system prune --volumes # Also remove unused volumes
- Explanation: This command is a lifesaver for freeing up disk space on your Docker host. Use it regularly, especially
-a
(all) and--volumes
if you’re sure you don’t need the data.
30. docker login
/ docker logout
/ docker push
- What it does:
docker login
: Logs you into a Docker registry (e.g., Docker Hub, Google Container Registry).docker logout
: Logs you out of a Docker registry.docker push
: Pushes an image to a Docker registry.
- Syntax:
docker login [OPTIONS] [SERVER]
docker logout [SERVER]
docker push [OPTIONS] NAME[:TAG]
- Example:
docker login # Enter your username and password docker push my-custom-app:v1 # Image must be tagged with registry/username/imagename:tag docker logout
- Explanation: These are essential for sharing your custom-built images with your team or deploying them to production environments. Before pushing, ensure your image is tagged correctly (e.g.,
docker tag my-app:v1 your-dockerhub-username/my-app:v1
).
Conclusion: You’re Now a Docker Command Maestro! 🎉
Phew, that was a lot, wasn’t it? But congratulations! You’ve just walked through 30 of the most fundamental and frequently used Docker commands. Mastering these commands will significantly boost your productivity and confidence when working with containers.
Remember, practice makes perfect! Fire up your terminal, experiment with these commands, and build a habit of using them. The more you use Docker, the more intuitive these commands will become.
What’s next? Once you’re comfortable with these basics, you might want to explore:
- Docker Compose: For defining and running multi-container Docker applications.
- Docker Swarm / Kubernetes: For orchestrating containers at scale in production.
- Advanced Dockerfile concepts: Multi-stage builds,
.dockerignore
, build arguments.
Happy containerizing! If you found this guide helpful, feel free to share it with your fellow developers! 🚀