Goglides Dev 🌱

Goglides Dev 🌱 is a community of amazing users

Shaping the future of IT, one connection at a time.

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   
docker rename CONTAINER_ID NEW_NAME

# Remove one or more containers  
docker rm CONTAINER_ID1 CONTAINER_ID2 ... CONTAINER_IDN

# Run a command in a running container  
docker exec CONTAINER_ID COMMAND 
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  
docker login REGISTRY_SERVER -u USERNAME -p PASSWORD

# 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)