Goglides Dev 🌱

Goglides Dev 🌱 is a community of amazing users

We are working on this space so that IT professionals can grow together.

Create account Log in
Cover image for Docker Cheat Sheet
Balkrishna Pandey
Balkrishna Pandey

Posted on

Docker Cheat Sheet

Docker is a powerful tool that can help you packaging, deploying and running your applications. This cheat sheet provides necessary commands and concepts related to Docker.

Basic Information

# Display system-wide information about Docker 
docker info

# Show the Docker version information 
docker version

# List all running (and stopped) containers
docker ps 
docker ps -a
docker ps -qa #(only IDs)
Enter fullscreen mode Exit fullscreen mode

Docker Image Operations

# List all images that are locally stored with the Docker engine  
docker images

# Pull an image or a repository from a registry  
docker pull NAME

# Upload an image to the Docker Hub Registry  
docker push NAME

# Build an image from a Dockerfile  
docker build -t IMAGE_NAME . 

# Create a new image from a container's changes  
docker commit CONTAINER_ID 

# Save one or more images to a tar archive  
docker save IMAGE1 IMAGE2 ... > my_images.tar

# Load an image from a tar archive  
cat my_images.tar | docker load

# Remove one or more images  
docker rmi IMAGE_ID1 IMAGE_ID2 ... IMAGE_IDN 

Enter fullscreen mode Exit fullscreen mode

Docker run

# Run a command in a new container  
docker run IMAGE COMMAND 

# Stop one or more running containers  
docker stop CONTAINER_ID

# Rename a container   

# Remove one or more containers  

# Run a command in a running container  
docker exec -it CONTAINER_ID /bin/bash

# Export a container's file system as a tar archive  
docker export CONTAINER_ID > mycontainer.tar 
Enter fullscreen mode Exit fullscreen mode

Logging/Monitoring and Stats

# Fetch the logs of a container  
docker logs --tail 50 --follow --timestamps CONTAINER 

# Inspect a running container  
docker inspect CONTAINER_ID 

# List all running processes in a container  
docker top CONTAINER_ID 

# Display the history of an image   
docker history IMAGE_ID

# Display total disk usage of one or more images  
docker system df

# Get real time events from the server  
docker events  --since '2019-07-01'  --until '2023-12-31'

# Monitor Docker's resource usage statistics    
docker stats $(docker ps --format={{.Names}}) 

# Show all modified files in a container
docker diff CONTAINER_ID

# Show map ports of a container
docker port CONTAINER_ID
Enter fullscreen mode Exit fullscreen mode

Docker Volumes

# List all available volumes  
docker volume ls

# Remove one or more volumes  
docker volume rm VOLUME_NAME1 VOLUME_NAME2 ...
Enter fullscreen mode Exit fullscreen mode

Docker Network

# List all available network commands  
docker network --help 

# Look up the Subnet, Gateway that a container uses on a specific network  
docker network inspect demo --format '{{(index .IPAM).Config}}'

#  Remove one or more networks  
docker network rm NETWORK_ID1 NETWORK_ID2 ... NETWORK_IDN 
Enter fullscreen mode Exit fullscreen mode

Bulk Cleanup

# Delete everything (containers, volumes, images, networks)  
docker system prune --all --force 

# Delete all containers  
docker rm $(docker ps -qa)  --force 

# Delete all images  
docker rmi $(docker images -a -q)  --force 

# Delete all unused images (just dangling images)  
docker rmi $(docker images -f "dangling=true" -q) 

# Delete all unused networks 
docker network prune 

# Remove all networks that are created more than 1 hours ago
docker network prune --filter "until=1h"

# Delete bridge network
docker network rm $(docker network ls | grep "bridge" | awk '/ / { print $1 }')
Enter fullscreen mode Exit fullscreen mode

Docker repository

# Authenticate with a Docker registry  

# List all tags from a repository in the Docker Hub Registry  
curl https://hub.docker.com/v2/repositories/$REPOSITORY/
curl -s https://hub.docker.com/v2/repositories/bkpandey | jq .

curl -X GET -i "http://localhost:5000/v2/_catalog"
Enter fullscreen mode Exit fullscreen mode

Dockerfile commands

FROM       β€” Sets the Base Image for subsequent instructions.
MAINTAINER β€” Sets the Author field of the generated images.
RUN        β€” Executes any commands in a new layer on top of the current image and commits the results. The resulting committed image will be used for the next step in the Dockerfile.
CMD        β€” Provides default values for an executing container.
EXPOSE    β€” Informs Docker that the container listens on the specified network ports at runtime. NOTE: does not actually make ports accessible.
ENV        β€” Sets environment variables.
ADD        β€” Copies new files, directories or remote file to the filesystem of the image.
COPY       β€” Copies new files or directories to the filesystem of the image.
ENTRYPOINT β€” Configures a container that will run as an executable.
VOLUME     β€” Creates a mount point with the specified name and marks it as holding externally mounted volumes from native host or other containers.
WORKDIR    β€” Sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile.
USER       β€” Sets the username or UID to use when running the image and for any RUN, CMD and ENTRYPOINT instructions that follow it in the Dockerfile.
ARG        β€” Defines a variable that users can pass at build-time to the builder with the docker build command using the --build-arg <varname>=<value> flag. If a user specifies a build argument that was not defined in the Dockerfile, the build outputs a warning.
ONBUILD    β€” Adds to the image a trigger instruction to be executed at a later time, when the image is used as the base for another build. The trigger will be executed in the context of the downstream Dockerfile.
STOPSIGNAL β€” Specifies a system call signal that overrides the default STOPSIGNAL value set by the ENTRYPOINT instruction.
HEALTHCHECK β€” Specifies how the container should be tested for health – this can be a command or a script.
SHELL      β€” Sets the default shell to use for commands (e.g., /bin/bash -c).
Enter fullscreen mode Exit fullscreen mode

Discussion (0)