Cheat Sheet #day15 - Docker Commands

Cloud TunedCloud Tuned
3 min read

Docker Commands Cheat Sheet

Docker is a platform for developing, shipping, and running applications in containers. Here is a cheat sheet with the most commonly used Docker commands.

Basic Docker Commands

  • Check Docker Version:

      docker --version
    
  • Get System-wide Information:

      docker info
    

Working with Containers

  • List Running Containers:

      docker ps
    
  • List All Containers (including stopped):

      docker ps -a
    
  • Start a Container:

      docker start CONTAINER_ID
    
  • Stop a Container:

      docker stop CONTAINER_ID
    
  • Remove a Container:

      docker rm CONTAINER_ID
    
  • Remove All Stopped Containers:

      docker container prune
    

Working with Images

  • List Images:

      docker images
    
  • Pull an Image from Docker Hub:

      docker pull IMAGE_NAME
    
  • Remove an Image:

      docker rmi IMAGE_NAME
    
  • Build an Image from a Dockerfile:

      docker build -t IMAGE_NAME PATH_TO_DOCKERFILE
    
  • Tag an Image:

      docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
    

Running Containers

  • Run a Container (from an image):

      docker run IMAGE_NAME
    
  • Run a Container in Detached Mode:

      docker run -d IMAGE_NAME
    
  • Run a Container with a Specific Name:

      docker run --name CONTAINER_NAME IMAGE_NAME
    
  • Run a Container and Map Ports:

      docker run -p HOST_PORT:CONTAINER_PORT IMAGE_NAME
    
  • Run a Container and Mount a Volume:

      docker run -v /host/path:/container/path IMAGE_NAME
    

Inspecting Containers and Images

  • Inspect a Container:

      docker inspect CONTAINER_ID
    
  • Inspect an Image:

      docker inspect IMAGE_NAME
    

Managing Docker Networks

  • List Docker Networks:

      docker network ls
    
  • Create a Network:

      docker network create NETWORK_NAME
    
  • Remove a Network:

      docker network rm NETWORK_NAME
    
  • Connect a Container to a Network:

      docker network connect NETWORK_NAME CONTAINER_NAME
    
  • Disconnect a Container from a Network:

      docker network disconnect NETWORK_NAME CONTAINER_NAME
    

Docker Compose Commands

  • Start Services Defined in docker-compose.yml:

      docker-compose up
    
  • Start Services in the Background:

      docker-compose up -d
    
  • Stop Services:

      docker-compose down
    
  • Build or Rebuild Services:

      docker-compose build
    
  • View Compose Logs:

      docker-compose logs
    
  • Scale Services:

      docker-compose scale SERVICE=NUM
    

Miscellaneous Commands

  • View Container Logs:

      docker logs CONTAINER_ID
    
  • Execute a Command in a Running Container:

      docker exec -it CONTAINER_ID COMMAND
    
  • Follow Log Output:

      docker logs -f CONTAINER_ID
    
  • Restart a Container:

      docker restart CONTAINER_ID
    

Cleaning Up

  • Remove All Unused Images:

      docker image prune -a
    
  • Remove All Unused Volumes:

      docker volume prune
    
  • Remove All Unused Networks:

      docker network prune
    

Conclusion

This cheat sheet provides a quick reference to the most commonly used Docker commands, helping you manage containers, images, networks, and more efficiently. For more detailed information, refer to the official Docker documentation.

0
Subscribe to my newsletter

Read articles from Cloud Tuned directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Cloud Tuned
Cloud Tuned