Day 20: Docker Cheatsheet ๐ŸŽŠ

Day 20: Docker Cheatsheet ๐ŸŽŠ

ยท

6 min read

Today is the 20th day of #90daysofdevops and now is the time to share all the learning with all of you. so today I will provide you all with a cheat sheet of Docker so you can easily understand the commands and what the command does : )

Running a new Container

1. Search for an Image:

docker search <image_name>

2. Pull an Image:

docker pull <image_name>:<tag>
# Example:
docker pull ubuntu:latest

3. Run a Container:

docker run [options] <image_name>
# Example:
docker run -it ubuntu:latest
  • -it: Interactive mode with a pseudo-TTY.

  • Use -d to run in detached mode (in the background).

4. Name the Container:

docker run --name <container_name> <image_name>
# Example:
docker run --name my_container -it ubuntu:latest

5. Map Ports:

docker run -p <host_port>:<container_port> <image_name>
# Example:
docker run -p 8080:80 nginx:latest

6. Map Volumes:

docker run -v <host_path>:<container_path> <image_name>
# Example:
docker run -v /host/path:/container/path ubuntu:latest

7. Environment Variables:

docker run -e <variable_name>=<value> <image_name>
# Example:
docker run -e MYSQL_ROOT_PASSWORD=my-secret-pw mysql:latest

8. View Running Containers:

docker ps
  • Use -a to show all containers.

9. Access Container Shell:

docker exec -it <container_id_or_name> /bin/bash

10. Stop a Running Container:

docker stop <container_id_or_name>

11. Remove a Container:

docker rm <container_id_or_name>
  • Use -f to force removal.

12. Remove an Image:

docker rmi <image_id_or_name>
  • Use -f to force removal.

13. Show Container Logs:

docker logs <container_id_or_name>

14. Inspect Container Details:

docker inspect <container_id_or_name>

15. Clean Up (Remove All Stopped Containers):

docker container prune

16. Clean Up (Remove All Unused Images):

docker image prune

17. Clean Up (Remove All Unused Resources):

docker system prune

18. Build an Image from Dockerfile:

docker build -t <image_name>:<tag> <path_to_dockerfile>
# Example:
docker build -t custom_image:latest .

Feel free to customize these commands based on your specific requirements and Docker setup.

Docker Cheat sheet to Manage Containers

Basic Commands:

  • Run a Container:

      docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
    

    Example:

      docker run -d -p 8080:80 --name my-container nginx
    
  • List Running Containers:

      docker ps
    
  • List All Containers (including stopped):

      docker ps -a
    
  • Stop a Running Container:

      docker stop CONTAINER_ID or CONTAINER_NAME
    
  • Start a Stopped Container:

      docker start CONTAINER_ID or CONTAINER_NAME
    
  • Remove a Container:

      docker rm CONTAINER_ID or CONTAINER_NAME
    

Image Management:

  • List Images:

      docker images
    
  • Pull an Image from Docker Hub:

      docker pull IMAGE_NAME[:TAG]
    

    Example:

      docker pull ubuntu:20.04
    
  • Build an Image from Dockerfile:

      docker build -t IMAGE_NAME[:TAG] PATH_TO_DOCKERFILE
    

    Example:

      docker build -t my-custom-image:1.0 .
    
  • Remove an Image:

      docker rmi IMAGE_ID or IMAGE_NAME[:TAG]
    

Container Networking:

  • Expose Ports:

      docker run -p HOST_PORT:CONTAINER_PORT IMAGE_NAME
    

    Example:

      docker run -p 8080:80 nginx
    
  • Inspect Container Network:

      docker inspect --format '{{ .NetworkSettings.IPAddress }}' CONTAINER_ID or CONTAINER_NAME
    

Container Logs:

  • View Container Logs:

      docker logs CONTAINER_ID or CONTAINER_NAME
    
  • Tail Container Logs:

      docker logs -f CONTAINER_ID or CONTAINER_NAME
    

Executing Commands Inside Containers:

  • Execute a Command Inside a Running Container:

      docker exec -it CONTAINER_ID or CONTAINER_NAME COMMAND [ARG...]
    

    Example:

      docker exec -it my-container bash
    

Volume Management:

  • Create a Volume:

      docker volume create VOLUME_NAME
    
  • List Volumes:

      docker volume ls
    
  • Mount a Volume in a Container:

      docker run -v VOLUME_NAME:CONTAINER_PATH IMAGE_NAME
    

    Example:

      docker run -v my-volume:/app/data my-app
    

Docker Compose:

  • Run Compose:

      docker-compose up -d
    
  • Stop Compose:

      docker-compose down
    

This cheat sheet covers some common Docker commands for container management. Feel free to customize and add more commands based on your specific use cases and requirements.

Docker Images Cheat sheet

Building Images:

  1. Build an image from a Dockerfile:

     docker build -t image_name:tag path/to/Dockerfile
    
  2. Build an image with a specific build context:

     docker build -t image_name:tag -f path/to/Dockerfile path/to/build/context
    
  3. Build an image with build arguments:

     docker build --build-arg key=value -t image_name:tag path/to/Dockerfile
    

Listing Images:

  1. List all images:

     docker images
    
  2. List images with additional details:

     docker images -a
    

Removing Images:

  1. Remove a specific image:

     docker rmi image_name:tag
    
  2. Remove all unused images:

     docker image prune
    
  3. Forcefully remove an image:

     docker rmi -f image_name:tag
    

Pulling/Pushing Images:

  1. Pull an image from Docker Hub:

     docker pull image_name:tag
    
  2. Push an image to Docker Hub:

     docker push image_name:tag
    

Tagging Images:

  1. Tag an image:

     docker tag source_image:source_tag new_image:new_tag
    

Inspecting Images:

  1. Inspect image details:

     docker inspect image_name:tag
    

Image History:

  1. Display the history of an image:

     docker history image_name:tag
    

Saving/Loading Images:

  1. Save an image to a tarball:

     docker save -o image_name.tar image_name:tag
    
  2. Load an image from a tarball:

     docker load -i image_name.tar
    

Misc:

  1. Search for an image on Docker Hub:

     docker search image_name
    
  2. Display detailed information about system usage:

     docker system df
    
  3. Clean up unused resources (images, containers, networks):

     docker system prune
    

Remember to replace "image_name" and "tag" with your actual image name and tag. Adjust the commands based on your specific use case and requirements.

Docker Info and Stats Cheat sheet

General Information

  1. Docker Version:

     docker version
    
  2. Docker System Information:

     docker info
    
  3. Docker Disk Usage:

     docker system df
    

Container Information

  1. List Running Containers:

     docker ps
    
  2. List All Containers (including stopped ones):

     docker ps -a
    
  3. Container Statistics:

     docker stats [container_name or container_id]
    
  4. Inspect Container Details:

     docker inspect [container_name or container_id]
    

Image Information

  1. List Downloaded Images:

     docker images
    
  2. Inspect Image Details:

     docker image inspect [image_name or image_id]
    
  3. Display Image History:

     docker history [image_name or image_id]
    

Network Information

  1. List Networks:

     docker network ls
    
  2. Inspect Network Details:

     docker network inspect [network_name or network_id]
    

Volume Information

  1. List Volumes:

     docker volume ls
    
  2. Inspect Volume Details:

     docker volume inspect [volume_name or volume_id]
    

Resource Usage

  1. Show Resource Usage by Containers:

     docker stats
    
  2. Display CPU Usage in Container:

     docker exec -it [container_name or container_id] top
    

Docker Compose

  1. Docker Compose Version:

     docker-compose version
    
  2. Docker Compose Build:

     docker-compose build
    
  3. Docker Compose Up (Start Services):

     docker-compose up -d
    
  4. Docker Compose Down (Stop Services):

     docker-compose down
    

Cleanup

  1. Remove Stopped Containers:

     docker container prune
    
  2. Remove Unused Images:

     docker image prune
    
  3. Remove Unused Volumes:

     docker volume prune
    
  4. Remove Unused Networks:

     docker network prune
    

Remember to replace [container_name or container_id], [image_name or image_id], [network_name or network_id], and [volume_name or volume_id] with the actual names or IDs.

Feel free to customize the commands based on your specific needs and preferences.

Follow me on my Socials and stay tuned with more such amazing content : )

Socials:

Github: https://github.com/dipen006
LinkedIN: https://www.linkedin.com/in/dipenr06/
Twitter: https://twitter.com/dipenr06

~Dipen : )

Did you find this article valuable?

Support Dipen's Blog by becoming a sponsor. Any amount is appreciated!

ย