Docker Top 50ย Must-Know Commands ๐ข๐ณ
Written by Devanshu Agarwal /
In todayโs world, Docker is the go-to tool for containerizing applications. Whether youโre a developer, sysadmin, or someone curious about how modern applications scale and deploy, understanding Docker commands can make a big difference. Weโve gathered the top 50 Docker commands you need to know, broken down into easy-to-understand chunks, so you can navigate Docker like a pro. ๐ช
Why Should You Care About Docker Commands? ๐ค
Docker helps simplify application development by packaging software into containersโa lightweight alternative to virtual machines. With Docker, you can manage environments efficiently, ship faster, and make sure "it works on my machine" is never an excuse. Mastering Docker commands gives you control over your environment and makes you that person others turn to when they need an app running smoothly. ๐
Hereโs a rundown of the 50 most essential Docker commands and what they do. Bookmark this guide as your go-to Docker reference. ๐
1. Docker Basics: The Essentials ๐
Before diving deep, let's go through the foundational commands every Docker user needs:
-
docker --version
๐ Displays the current version of Docker installed. Useful for verifying compatibility. -
docker pull [image]
โฌ๏ธ Fetches a Docker image from Docker Hub. For example,docker pull nginx
gets the latest NGINX container. -
docker run [image]
๐โโ๏ธ Creates and runs a container based on the specified image. -
docker ps
๐ Shows all currently running containers. -
docker ps -a
๐ Displays all containers, even those that are stoppedโideal for tracking down misbehaving services. -
docker stop [container_id]
โ Stops a running container. Handy when you need to halt a misbehaving app. -
docker rm [container_id]
๐๏ธ Removes a stopped container to free up system resources. -
docker rmi [image_id]
๐๏ธ Deletes an image from your local machine. Useful for housekeeping when youโve accumulated too many unused images. -
docker images
๐ผ๏ธ Lists all Docker images available on your machine. -
docker exec -it [container_id] /bin/bash
๐ง Opens an interactive terminal in a running container. Great for troubleshooting.
2. Managing Images and Containers ๐ฆ
-
docker build -t [name] .
๐๏ธ Builds an image from a Dockerfile located in the current directory. -
docker logs [container_id]
๐ Displays the logs of a running or stopped container. Essential for debugging. -
docker start [container_id]
โถ๏ธ Starts a stopped container without recreating it. -
docker restart [container_id]
๐ Stops and then starts a container again. Useful for applying quick fixes. -
docker inspect [container_id]
๐ Provides detailed information about a container, including its configuration and network settings. -
docker commit [container_id] [image_name]
๐ธ Creates a new image from an existing container. Think of it as saving the current state. -
docker rename [old_name] [new_name]
โ๏ธ Renames a container. Useful if your naming conventions change. -
docker top [container_id]
๐ Shows running processes inside a container. Itโs the container equivalent of the Unixtop
command. -
docker diff [container_id]
๐ Lists the changes made to a container's file system. -
docker cp [container_id]:[path] [host_path]
๐ Copies files from a container to the host or vice versa.
3. Networking and Volumes ๐๐พ
-
docker network ls
๐ Lists all Docker networks. Networks manage how containers communicate. -
docker network create [network_name]
๐ ๏ธ Creates a new Docker networkโuseful for separating app environments. -
docker network connect [network] [container_id]
๐ Connects a container to a specific network. -
docker volume create [volume_name]
๐พ Creates a volume for persistent storage, which is crucial for databases. -
docker volume ls
๐ Lists all volumes on your system. -
docker volume rm [volume_name]
๐๏ธ Deletes a volume, freeing up disk space. -
docker network disconnect [network] [container_id]
๐ Disconnects a container from a network. -
docker port [container_id]
๐ Shows the public-facing port of a container and the port it is mapped to internally.
4. Docker Compose Commands ๐ ๏ธ
-
docker-compose up
๐ Starts services defined in adocker-compose.yml
file. Think of it as a fast-track to launch multi-container apps. -
docker-compose down
โฌ๏ธ Stops and removes services, networks, and volumes defined bydocker-compose.yml
. -
docker-compose build
๐๏ธ Builds or rebuilds services defined in the compose file. -
docker-compose logs
๐ Displays logs from all services defined in the Docker Compose setup. -
docker-compose ps
๐ Shows the status of all services in yourdocker-compose.yml
.
5. Advanced Docker Commands ๐ง
-
docker tag [source_image] [target_image:tag]
๐ท๏ธ Tags an image for easy identification. -
docker save [image] > [filename.tar]
๐พ Saves an image to a tar file, which is handy for sharing offline. -
docker load < [filename.tar]
๐ฅ Loads an image from a tar archive. -
docker stats
๐ Displays a live stream of container resource usageโmemory, CPU, etc. -
docker prune
๐๏ธ Removes unused containers, images, networks, and volumes in one sweep to free up space. -
docker system df
๐ Shows disk usage for Docker. It's likedf
, but Docker-specific. -
docker update [container_id] --memory [limit]
โ๏ธ Updates the configuration of a container, such as limiting its memory usage.
6. Security and Best Practices ๐
-
docker scan [image]
๐ Scans the image for vulnerabilities. Always be aware of security issues. -
docker login
๐ Authenticates your Docker client to the Docker Hub. -
docker logout
๐ช Logs out of Docker Hub from the Docker CLI. -
docker secret create [secret_name] [file]
๐ Stores sensitive information, like API keys, securely in Docker Swarm. -
docker secret ls
๐ Lists all secrets currently managed by Docker.
7. Cleanup and Troubleshooting ๐งน๐
-
docker container prune
๐๏ธ Removes all stopped containers. -
docker image prune
๐๏ธ Deletes dangling imagesโthose no longer associated with a running container. -
docker network prune
๐๏ธ Cleans up unused networks to reduce clutter. -
docker volume prune
๐๏ธ Cleans up unused volumes. -
docker system prune -a
โ ๏ธ Wipes out all unused data: containers, images, volumes, and networks. Be cautiousโthis command is thorough.
Conclusion ๐
Understanding Docker commands is crucial for managing your containers effectively. With these top 50 commands, youโre ready to tackle the most common Docker challenges, from starting containers to managing storage and networking. Docker becomes much easier to work with when you know what tools are at your disposal. ๐ ๏ธ
Need More? ๐
This list provides a solid foundation, but Docker is constantly evolving. Stay curious and keep experimenting! Donโt forget to bookmark this guide and revisit it whenever you feel lost in the sea of commands. ๐