Docker Cheat Sheet ๐Ÿณ

Rudraksh LaddhaRudraksh Laddha
5 min read

๐Ÿ”น Docker Basics

CommandDescription
docker --versionCheck Docker version
docker infoDisplay system-wide information
docker helpShow help for Docker commands

๐Ÿ”น Working with Containers

CommandDescription
docker run <image>Run a container from an image
docker run -d <image>Run a container in detached mode (background)
docker run -it <image> bashRun a container interactively with a shell
docker psList running containers
docker ps -aList all containers (including stopped ones)
docker start <container>Start a stopped container
docker stop <container>Stop a running container
docker restart <container>Restart a container
docker kill <container>Kill a running container immediately
docker rm <container>Remove a stopped container
docker rm -f <container>Force remove a running container
docker logs <container>Show logs of a container
docker exec -it <container> bashAccess a running container's shell
docker inspect <container>Get detailed information about a container
docker statsShow resource usage of running containers
docker rename <old> <new>Rename a container
docker pause <container>Pause a running container
docker unpause <container>Unpause a paused container

๐Ÿ”น Working with Images

CommandDescription
docker imagesList all local images
docker pull <image>Download an image from Docker Hub
docker push <image>Upload an image to Docker Hub
docker rmi <image>Remove an image
docker tag <image> <new_image>Rename/tag an image
docker history <image>Show history of an image
docker save -o image.tar <image>Save an image to a tar file
docker load -i image.tarLoad an image from a tar file
docker commit <container> <new_image>Create an image from a container

๐Ÿ”น Docker Volumes (Storage Management)

CommandDescription
docker volume create <volume>Create a new volume
docker volume lsList all volumes
docker volume inspect <volume>Get details of a volume
docker volume rm <volume>Remove a volume
docker run -v <volume>:/path <image>Mount a volume inside a container

๐Ÿ”น Docker Networks

CommandDescription
docker network lsList all networks
docker network create <network>Create a new network
docker network inspect <network>Get details of a network
docker network connect <network> <container>Connect a container to a network
docker network disconnect <network> <container>Disconnect a container from a network
docker network rm <network>Remove a network

๐Ÿ”น Building Custom Images (Dockerfile)

CommandDescription
docker build -t <image_name> .Build an image from a Dockerfile in the current directory
docker build -t <image_name> -f <Dockerfile>Build an image using a specific Dockerfile
docker run --rm <image>Run a container and remove it after exiting

Basic Dockerfile Example:

# Use an official image as a base
FROM node:16

# Set the working directory
WORKDIR /app

# Copy files and install dependencies
COPY package.json .
RUN npm install

# Copy the rest of the app
COPY . .

# Expose a port and run the app
EXPOSE 3000
CMD ["node", "server.js"]

๐Ÿ”น Docker Compose

CommandDescription
docker-compose upStart services in the docker-compose.yml file
docker-compose up -dStart services in detached mode
docker-compose downStop and remove all containers from docker-compose.yml
docker-compose psList running services
docker-compose restartRestart all services
docker-compose logsView logs of all services
docker-compose exec <service> bashOpen a shell inside a service container

Basic docker-compose.yml Example:

version: '3'
services:
  web:
    image: nginx
    ports:
      - "8080:80"
    volumes:
      - ./html:/usr/share/nginx/html
  db:
    image: mysql
    environment:
      MYSQL_ROOT_PASSWORD: secret

๐Ÿ”น Managing Docker System Resources

CommandDescription
docker system dfShow Docker disk usage
docker system pruneRemove unused data (stopped containers, networks, images)
docker container pruneRemove all stopped containers
docker image pruneRemove unused images
docker volume pruneRemove unused volumes
docker network pruneRemove unused networks

๐Ÿ”น Security & User Management

CommandDescription
docker loginLog in to Docker Hub
docker logoutLog out from Docker Hub
docker secret create <name> <file>Create a secret from a file
docker secret lsList all secrets
docker secret rm <name>Remove a secret

๐Ÿ”น Docker Swarm (Orchestration)

CommandDescription
docker swarm initInitialize a Swarm cluster
docker swarm join --token <token> <manager-IP>Join a Swarm cluster
docker swarm leaveLeave a Swarm cluster
docker service create --name web -p 80:80 nginxCreate a service in Swarm
docker service lsList all services
docker service rm <service>Remove a service
docker node lsList all nodes in the Swarm

๐Ÿ”น Kubernetes with Docker

CommandDescription
kubectl get podsList all running pods
kubectl get servicesList all services
kubectl logs <pod>View logs of a pod
kubectl exec -it <pod> -- bashAccess a podโ€™s shell
kubectl apply -f <file>.yamlApply a Kubernetes configuration

Linux Cheatsheet

0
Subscribe to my newsletter

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

Written by

Rudraksh Laddha
Rudraksh Laddha

DevOps Engineer || Technical Writer || Content Creator || | Adventurer chasing dreams, capturing life's kaleidoscope. || ๐ŸŽ“UCET '24 || Dm for Collabs๐Ÿ“ฅ||