Essential Docker Commands: A Full Cheat Sheet
Docker is a platform that allows developers to automate the deployment, scaling and management of applications using containerization. Containers are lightweight, portable and self-sufficient environments that package software along with its dependencies. Docker simplifies the process of creating, deploying and running applications in any environment, whether it’s on a local machine, cloud infrastructure or hybrid environments.
Why is Docker Important?
Docker has revolutionized the way software is developed and deployed. Its importance stems from several key benefits:
- Portability: Docker containers can run consistently across different environments without needing modifications.
- Scalability: Docker makes it easier to scale applications horizontally by creating multiple container instances.
- Efficiency: Containers use fewer resources than traditional virtual machines, leading to faster performance and lower costs.
- Isolation: Each container runs independently, ensuring that applications and their dependencies do not interfere with each other.
- Consistency: Developers can build and test applications in the same environment that will be used in production, minimizing compatibility issues.
Why Use Docker?
Docker is widely used for several reasons:
- Simplifies the development and deployment process.
- Improves collaboration between development and operations teams.
- Enhances the CI/CD pipeline by making builds and deployments more predictable.
- Provides a reliable environment for testing and debugging.
- Offers a vast ecosystem with pre-built images from Docker Hub.
Essential Docker Commands that Every Developer Should Know
Let’s see a detailed guide of top docker commands:
1. Docker Installation & Version
docker --version
– Check Docker versiondocker info
– Display system-wide information about Dockerdocker help
– List all available Docker commands
2. Docker Images
docker images
– List all Docker images on the systemdocker pull <image>
– Download a Docker image from Docker Hubdocker build -t <image_name> .
– Build an image from a Dockerfiledocker rmi <image_id>
– Remove a Docker imagedocker image prune
– Remove all unused Docker imagesdocker tag <image> <new_image>
– Tag an image with a new namedocker history <image>
– Show the history of an image
3. Docker Containers
docker ps
– List running containersdocker ps -a
– List all containers (running + stopped)docker run <image>
– Create and run a container from an imagedocker run -d <image>
– Run a container in detached modedocker stop <container_id>
– Stop a running containerdocker start <container_id>
– Start a stopped containerdocker restart <container_id>
– Restart a containerdocker rm <container_id>
– Remove a containerdocker logs <container_id>
– View container logsdocker exec -it <container_id> bash
– Access a running container via bash shell
4. Docker Volumes
docker volume create <volume_name>
– Create a volumedocker volume ls
– List all volumesdocker volume inspect <volume_name>
– Inspect volume detailsdocker volume rm <volume_name>
– Remove a volumedocker volume prune
– Remove unused volumes
5. Docker Networks
docker network ls
– List all Docker networksdocker network create <network_name>
– Create a networkdocker network inspect <network_name>
– Inspect network detailsdocker network connect <network_name> <container_name>
– Connect a container to a networkdocker network disconnect <network_name> <container_name>
– Disconnect a container from a networkdocker network prune
– Remove unused networks
6. Docker Compose
docker-compose up
– Start containers defined in a docker-compose.ymldocker-compose down
– Stop and remove containers, networks, and volumesdocker-compose logs
– View logs of servicesdocker-compose ps
– List running servicesdocker-compose build
– Build or rebuild services
7. Docker System Management
docker system df
– Show Docker disk usagedocker system prune
– Remove unused data (images, containers, volumes, networks)docker system info
– Display system-wide information
8. Docker Security
docker scan <image>
– Scan an image for vulnerabilitiesdocker trust inspect <image>
– Inspect trust data for an image
9. Docker Stats and Resource Management
docker stats
– Display live resource usage of containersdocker top <container_id>
– Show running processes inside a container
10. Docker Export & Import
docker export <container_id> > container.tar
– Export container filesystemdocker import container.tar
– Import container filesystem as an image
Conclusion
In conclusion, Docker is an essential tool for modern application development. These 50 Docker commands are essential for managing Docker containers, images, networks, and volumes. Whether you are a beginner or an advanced developer, understanding these commands will help you efficiently work with Docker environments.
Stay updated with Docker documentation for more advanced commands and best practices!