Debug School

Cover image for Docker Commands Cheat Sheet
Suyash Sambhare
Suyash Sambhare

Posted on

Docker Commands Cheat Sheet

Images

  • Build an image from a Dockerfile:
  docker build -t <image_name> .
Enter fullscreen mode Exit fullscreen mode
  • List local images:
  docker images
Enter fullscreen mode Exit fullscreen mode
  • Delete an image:
  docker rmi <image_name>
Enter fullscreen mode Exit fullscreen mode
  • Remove all unused images:
  docker image prune
Enter fullscreen mode Exit fullscreen mode

Containers

  • Create and run a container from an image:
  docker run --name <container_name> <image_name>
Enter fullscreen mode Exit fullscreen mode
  • Run a container in the background:
  docker run -d <image_name>
Enter fullscreen mode Exit fullscreen mode
  • Start or stop an existing container:
  docker start|stop <container_name>
Enter fullscreen mode Exit fullscreen mode
  • Remove a stopped container:
  docker rm <container_name>
Enter fullscreen mode Exit fullscreen mode
  • Open a shell inside a running container:
  docker exec -it <container_name> sh
Enter fullscreen mode Exit fullscreen mode
  • Fetch and follow the logs of a container:
  docker logs -f <container_name>
Enter fullscreen mode Exit fullscreen mode

General Commands

  • Start the Docker daemon:
  docker -d
Enter fullscreen mode Exit fullscreen mode
  • Display system-wide information:
  docker info
Enter fullscreen mode Exit fullscreen mode
  • Get help with Docker:
  docker --help
Enter fullscreen mode Exit fullscreen mode

Docker Hub

  • Login to Docker Hub:
  docker login -u <username>
Enter fullscreen mode Exit fullscreen mode
  • Publish an image to Docker Hub:
  docker push <username>/<image_name>
Enter fullscreen mode Exit fullscreen mode
  • Pull an image from Docker Hub:
  docker pull <image_name>
Enter fullscreen mode Exit fullscreen mode

Docker

Advanced Docker CLI Commands

Networking

  • Create a new network:
  docker network create <network_name>
Enter fullscreen mode Exit fullscreen mode
  • List all networks:
  docker network ls
Enter fullscreen mode Exit fullscreen mode
  • Connect a container to a network:
  docker network connect <network_name> <container_name>
Enter fullscreen mode Exit fullscreen mode
  • Disconnect a container from a network:
  docker network disconnect <network_name> <container_name>
Enter fullscreen mode Exit fullscreen mode

Volumes

  • Create a volume:
  docker volume create <volume_name>
Enter fullscreen mode Exit fullscreen mode
  • List all volumes:
  docker volume ls
Enter fullscreen mode Exit fullscreen mode
  • Remove a volume:
  docker volume rm <volume_name>
Enter fullscreen mode Exit fullscreen mode
  • Inspect a volume:
  docker volume inspect <volume_name>
Enter fullscreen mode Exit fullscreen mode

Compose

  • Start services defined in a docker-compose.yml file:
  docker-compose up
Enter fullscreen mode Exit fullscreen mode
  • Stop services defined in a docker-compose.yml file:
  docker-compose down
Enter fullscreen mode Exit fullscreen mode
  • Run a specific service:
  docker-compose run <service_name>
Enter fullscreen mode Exit fullscreen mode

Swarm

  • Initialize a new swarm:
  docker swarm init
Enter fullscreen mode Exit fullscreen mode
  • Join an existing swarm:
  docker swarm join --token <token> <manager_ip>:<port>
Enter fullscreen mode Exit fullscreen mode
  • List nodes in the swarm:
  docker node ls
Enter fullscreen mode Exit fullscreen mode
  • Deploy a stack in swarm mode:
  docker stack deploy -c <compose_file> <stack_name>
Enter fullscreen mode Exit fullscreen mode

Security

  • Scan an image for vulnerabilities:
  docker scan <image_name>
Enter fullscreen mode Exit fullscreen mode
  • Sign an image:
  docker trust sign <image_name>
Enter fullscreen mode Exit fullscreen mode
  • Enable content trust:
  export DOCKER_CONTENT_TRUST=1
Enter fullscreen mode Exit fullscreen mode

Resource Management

  • Limit container memory usage:
  docker run -m <memory_limit> <image_name>
Enter fullscreen mode Exit fullscreen mode
  • Limit container CPU usage:
  docker run --cpus=<cpu_limit> <image_name>
Enter fullscreen mode Exit fullscreen mode

Ref: Docker CLI Cheat Sheet

Top comments (0)