Cloud & DevOpsTech&Dev&code

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 version
  • docker info – Display system-wide information about Docker
  • docker help – List all available Docker commands

2. Docker Images

  • docker images – List all Docker images on the system
  • docker pull <image> – Download a Docker image from Docker Hub
  • docker build -t <image_name> . – Build an image from a Dockerfile
  • docker rmi <image_id> – Remove a Docker image
  • docker image prune – Remove all unused Docker images
  • docker tag <image> <new_image> – Tag an image with a new name
  • docker history <image> – Show the history of an image

3. Docker Containers

  • docker ps – List running containers
  • docker ps -a – List all containers (running + stopped)
  • docker run <image> – Create and run a container from an image
  • docker run -d <image> – Run a container in detached mode
  • docker stop <container_id> – Stop a running container
  • docker start <container_id> – Start a stopped container
  • docker restart <container_id> – Restart a container
  • docker rm <container_id> – Remove a container
  • docker logs <container_id> – View container logs
  • docker exec -it <container_id> bash – Access a running container via bash shell

4. Docker Volumes

  • docker volume create <volume_name> – Create a volume
  • docker volume ls – List all volumes
  • docker volume inspect <volume_name> – Inspect volume details
  • docker volume rm <volume_name> – Remove a volume
  • docker volume prune – Remove unused volumes

5. Docker Networks

  • docker network ls – List all Docker networks
  • docker network create <network_name> – Create a network
  • docker network inspect <network_name> – Inspect network details
  • docker network connect <network_name> <container_name> – Connect a container to a network
  • docker network disconnect <network_name> <container_name> – Disconnect a container from a network
  • docker network prune – Remove unused networks

6. Docker Compose

  • docker-compose up – Start containers defined in a docker-compose.yml
  • docker-compose down – Stop and remove containers, networks, and volumes
  • docker-compose logs – View logs of services
  • docker-compose ps – List running services
  • docker-compose build – Build or rebuild services

7. Docker System Management

  • docker system df – Show Docker disk usage
  • docker 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 vulnerabilities
  • docker trust inspect <image> – Inspect trust data for an image

9. Docker Stats and Resource Management

  • docker stats – Display live resource usage of containers
  • docker top <container_id> – Show running processes inside a container

10. Docker Export & Import

  • docker export <container_id> > container.tar – Export container filesystem
  • docker 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!

Anshul Pal

Hey there, I'm Anshul Pal, a tech blogger and Computer Science graduate. I'm passionate about exploring tech-related topics and sharing the knowledge I've acquired. Thanks for reading my blog – Happy Learning

Leave a Reply

Your email address will not be published. Required fields are marked *