Docker Cheat Sheet: 100 Essential Commands with Examples

Saurabh NamdeoSaurabh Namdeo
9 min read

Docker 100 Essential Commands with Examples

1. Basic Commands

  1. docker --version: Check Docker version.

    • Example:

        docker --version
      

      Output: Docker version 24.0.5, build 1234567

  2. docker info: Display system-wide information.

    • Example:

        docker info
      
  3. docker help: Show help for Docker commands.

    • Example:

        docker help run
      
  4. docker login: Log in to a Docker registry.

    • Example:

        docker login -u myusername -p mypassword
      
  5. docker logout: Log out from a Docker registry.

    • Example:

        docker logout
      
  6. docker version: Display client and server version details.

    • Example:

        docker version
      

2. Container Management

  1. docker run <image>: Create and start a new container.

    • Example:

        docker run -d -p 80:80 nginx
      

      Creates a detached container running the Nginx image and maps port 80.

  2. docker ps: List running containers.

    • Example:

        docker ps
      
  3. docker ps -a: List all containers (including stopped ones).

    • Example:

        docker ps -a
      
  4. docker stop <container>: Stop a running container.

    • Example:

        docker stop my-container
      
  5. docker start <container>: Start a stopped container.

    • Example:

        docker start my-container
      
  6. docker restart <container>: Restart a container.

    • Example:

        docker restart my-container
      
  7. docker rm <container>: Remove a stopped container.

    • Example:

        docker rm my-container
      
  8. docker kill <container>: Forcefully stop a container.

    • Example:

        docker kill my-container
      
  9. docker pause <container>: Pause a container.

    • Example:

        docker pause my-container
      
  10. docker unpause <container>: Unpause a container.

    • Example:

        docker unpause my-container
      
  11. docker rename <old-name> <new-name>: Rename a container.

    • Example:

        docker rename my-container my-new-container
      
  12. docker inspect <container>: View detailed container information.

    • Example:

        docker inspect my-container
      

3. Image Management

  1. docker images: List all images.

    • Example:

        docker images
      
  2. docker rmi <image>: Remove an image.

    • Example:

        docker rmi my-image
      
  3. docker tag <source-image> <target-image>: Tag an image.

    • Example:

        docker tag my-image my-repo/my-image:latest
      
  4. docker build -t <image-name> .: Build an image from a Dockerfile.

    • Example:

        docker build -t my-image .
      
  5. docker pull <image>: Pull an image from a registry.

    • Example:

        docker pull nginx
      
  6. docker push <image>: Push an image to a registry.

    • Example:

        docker push my-repo/my-image
      
  7. docker history <image>: Show the history of an image.

    • Example:

        docker history my-image
      
  8. docker save -o <file.tar> <image>: Save an image to a tarball.

    • Example:

        docker save -o my-image.tar my-image
      
  9. docker load -i <file.tar>: Load an image from a tarball.

    • Example:

        docker load -i my-image.tar
      
  10. docker export <container>: Export a container's filesystem as a tar archive.

    • Example:

        docker export my-container > my-container.tar
      
  11. docker import <file.tar>: Import a tar archive to create an image.

    • Example:

        docker import my-container.tar my-imported-image
      

4. Container Interaction

  1. docker exec -it <container> <command>: Run a command inside a running container.

    • Example:

        docker exec -it my-container bash
      
  2. docker attach <container>: Attach to a running container.

    • Example:

        docker attach my-container
      
  3. docker logs <container>: View logs of a container.

    • Example:

        docker logs my-container
      
  4. docker exec <container> pwd: Print working directory in a container.

    • Example:

        docker exec my-container pwd
      
  5. docker exec <container> ls: List files in a container.

    • Example:

        docker exec my-container ls
      
  6. docker top <container>: Display running processes in a container.

    • Example:

        docker top my-container
      

5. Networking Commands

  1. docker network ls: List all networks.

    • Example:

        docker network ls
      
  2. docker network create <network-name>: Create a custom network.

    • Example:

        docker network create my-network
      
  3. docker network inspect <network>: Inspect a network.

    • Example:

        docker network inspect my-network
      
  4. docker network rm <network>: Remove a network.

    • Example:

        docker network rm my-network
      
  5. docker run --network=<network-name>: Run a container on a specific network.

    • Example:

        docker run -d --network=my-network nginx
      
  6. docker network connect <network> <container>: Connect a container to a network.

    • Example:

        docker network connect my-network my-container
      
  7. docker network disconnect <network> <container>: Disconnect a container from a network.

    • Example:

        docker network disconnect my-network my-container
      

6. Docker Compose Commands

  1. docker-compose up: Start services defined in docker-compose.yml.

    • Example:

        docker-compose up -d
      
  2. docker-compose up -d: Start services in detached mode.

    • Example:

        docker-compose up -d
      
  3. docker-compose down: Stop and remove containers and networks.

    • Example:

        docker-compose down
      
  4. docker-compose ps: List running containers managed by Docker Compose.

    • Example:

        docker-compose ps
      
  5. docker-compose logs: View logs for services.

    • Example:

        docker-compose logs -f
      
  6. docker-compose exec <service> <command>: Run a command in a running service.

    • Example:

        docker-compose exec web /bin/bash
      
  7. docker-compose build: Build services from a Dockerfile.

    • Example:

        docker-compose build
      
  8. docker-compose stop: Stop running services.

    • Example:

        docker-compose stop
      
  9. docker-compose start: Start stopped services.

    • Example:

        docker-compose start
      
  10. docker-compose restart: Restart services.

    • Example:

        docker-compose restart
      
  11. docker-compose pull: Pull images for services.

    • Example:

        docker-compose pull
      

7. Volume Commands

  1. docker volume ls: List all volumes.

    • Example:

        docker volume ls
      
  2. docker volume create <volume-name>: Create a volume.

    • Example:

        docker volume create my-volume
      
  3. docker volume inspect <volume>: Inspect a volume.

    • Example:

        docker volume inspect my-volume
      
  4. docker volume rm <volume>: Remove a volume.

    • Example:

        docker volume rm my-volume
      
  5. docker run -v <volume-name>:<path>: Mount a volume to a container.

    • Example:

        docker run -d -v my-volume:/data my-container
      
  6. docker run -v <host-path>:<container-path>: Bind-mount a host directory to a container.

    • Example:

        docker run -d -v /host/data:/container/data my-container
      

8. Docker System Commands

  1. docker system df: Display disk usage.

    • Example:

        docker system df
      
  2. docker system prune: Remove unused data.

    • Example:

        docker system prune
      
  3. docker system prune -a: Remove all unused images, containers, and networks.

    • Example:

        docker system prune -a
      
  4. docker system events: Display real-time events from the Docker daemon.

    • Example:

        docker system events
      
  5. docker info --format: Format the output of docker info.

    • Example:

        docker info --format '{{.Containers}} containers are running'
      
  6. docker update <container>: Update a container's configuration.

    • Example:

        docker update --memory 512m my-container
      

9. Advanced Docker Commands

  1. docker cp <container>:<path> <host-path>: Copy files from a container to the host.

    • Example:

        docker cp my-container:/app/data.txt /host/data.txt
      
  2. docker cp <host-path> <container>:<path>: Copy files from the host to a container.

    • Example:

        docker cp /host/data.txt my-container:/app/data.txt
      
  3. docker stats: Display real-time statistics of running containers.

    • Example:

        docker stats
      
  4. docker diff <container>: Show changes made to a container’s filesystem.

    • Example:

        docker diff my-container
      
  5. docker export <container>: Export the filesystem of a container.

    • Example:

        docker export my-container > my-container.tar
      
  6. docker import <file.tar>: Import an image from a tar archive.

    • Example:

        docker import my-container.tar my-imported-image
      
  7. docker commit <container> <new-image>: Create an image from a container's changes.

    • Example:

        docker commit my-container my-custom-image
      
  8. docker update <container>: Update the resource configuration of a container.

    • Example:

        docker update --memory 512m my-container
      
  9. docker wait <container>: Block until a container stops, then return its exit code.

    • Example:

        docker wait my-container
      
  10. docker attach <container>: Attach to a running container’s standard input/output.

    • Example:

        docker attach my-container
      
  11. docker container prune: Remove all stopped containers.

    • Example:

        docker container prune
      

10. Security and Permissions

  1. docker run --user <UID>: Run a container as a specific user.

    • Example:

        docker run --user 1001 my-container
      
  2. docker run --privileged: Run a container in privileged mode.

    • Example:

        docker run --privileged my-container
      
  3. docker login <registry>: Log in to a specific Docker registry.

    • Example:

        docker login my-registry.com
      
  4. docker trust sign <image>: Sign an image for content trust.

    • Example:

        docker trust sign my-image
      
  5. docker content trust: Enable or disable Docker Content Trust.

    • Example:

        export DOCKER_CONTENT_TRUST=1
      
  6. docker scan <image>: Scan an image for vulnerabilities.

    • Example:

        docker scan my-image
      

11. Troubleshooting Commands

  1. docker inspect <container>: Inspect detailed container configuration.

    • Example:

        docker inspect my-container
      
  2. docker logs --tail <number> <container>: Show the last <number> of lines from a container's logs.

    • Example:

        docker logs --tail 10 my-container
      
  3. docker logs -f <container>: Stream logs of a container in real-time.

    • Example:

        docker logs -f my-container
      
  4. docker events: Monitor real-time events from the Docker daemon.

    • Example:

        docker events
      
  5. docker check: Check system configuration (not an actual command, might be docker system info).

    • Example:

        docker system info
      
  6. docker container top <container>: Show processes running in a container.

    • Example:

        edocker container top my-container
      
  7. docker network disconnect: Disconnect a container from a network.

    • Example:

        docker network disconnect my-network my-container
      
  8. docker exec -it <container> /bin/bash: Start a bash shell in a container.

    • Example:

        docker exec -it my-container /bin/bash
      
  9. docker run --rm: Automatically remove a container when it exits.

    • Example:

        docker run --rm my-container
      

12. Docker Swarm Commands

  1. docker swarm init: Initialize a Docker swarm.

    • Example:

        docker swarm init --advertise-addr 192.168.1.1
      
  2. docker swarm join: Join a Docker swarm as a worker or manager.

    • Example:

        docker swarm join --token <token> 192.168.1.1:2377
      
  3. docker swarm leave: Leave the Docker swarm.

    • Example:

        docker swarm leave --force
      
  4. docker service create: Create a new service.

    • Example:

        docker service create --name my-service --replicas 3 -p 80:80 nginx
      
  5. docker service ls: List all services in the swarm.

    • Example:

        docker service ls
      
  6. docker service ps <service>: List tasks of a service.

    • Example:

        docker service ps my-service
      
  7. docker service update: Update a service.

    • Example:

        docker service update --image nginx:latest my-service
      
  8. docker stack deploy -c <file> <stack>: Deploy a stack using a Compose file.

    • Example:

        docker stack deploy -c docker-compose.yml my-stack
      
  9. docker stack rm <stack>: Remove a Docker stack. - Example:

    docker stack rm my-stack
    

These commands cover a wide range of Docker functionalities to help manage containers, images, and networking effectively.

0
Subscribe to my newsletter

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

Written by

Saurabh Namdeo
Saurabh Namdeo