Here, you will find all the important commands, useful one-liners, cleanup commands, machine commands, compose syntax, and instructions to help you interact with containers via Docker. This is the Docker cheat sheet with some commonly used Docker commands:

What is Docker?

Docker is an open-source container manager that is used to develop, ship, and run applications using containers. It enables developers to pack up an application with other essential parts, such as libraries and other dependencies, and deploy it as a single isolated unit.

Unlike Virtual Machines, Docker doesn’t create a full virtual operating system. Instead, it allows applications to use the same Linux kernel as the system that they’re running on. This grants higher speed and a boost in performance along with reducing the size of the applications. Let’s start the Docker Cheat Sheet.

General Commands
  • docker version – Displays detailed information about your Docker CLI and daemon versions.
  • docker system info – Lists data about your Docker environment, including active plugins and the number of containers and images on your system.
  • docker help – View the help index, a reference of all the supported commands.
  • docker <command> --help – View the help information about a particular command, including detailed information on the supported option flags.
  • docker images – List all Docker images.
  • docker pull <image> – Download a Docker image from a registry.
  • docker rmi <image> – Remove one or more images from your system.
  • docker build -t <tag> <path> – Build a Docker image from a Dockerfile.
  • docker tag <image> <tag> – Tag an image to a name (local or registry).
  • docker-compose up– This command builds, (re)creates, starts, and attaches to containers for a service.
  • docker-compose down– If you want to stop and remove containers, networks, images, and volumes, use this command.
  • docker-compose build– This command is used to build or rebuild services.
  • docker ps – List running containers.
  • docker ps -a – List all containers (including stopped ones).
  • docker run <image> – Run a new container, pulling the image if needed and starting the container.
  • docker run <options> <image> – Create and start a container from an image.
  • docker start <container> – Start a stopped container.
  • docker stop <container> – Stop a running container.
  • docker restart <container> – Restart a container.
  • docker exec -it <container> <command> – Execute a command inside a running container.
  • docker rm <container> – Remove a stopped container.
  • docker container prune – Remove all stopped containers.
  • docker network ls – List Docker networks.
  • docker network create <name> – Create a Docker 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 <name> – Remove name-wise network from docker.
  • docker network prune – Remove all unused networks. Unused networks are those that are not referenced by any containers.
  • docker volume ls – List Docker volumes.
  • docker volume create <name> – Create a Docker volume.
  • docker volume rm <volume> – Remove a Docker volume.
Registry and Login:
  • docker login – Log in to a Docker registry.
  • docker logout – Log out from a Docker registry.
  • docker push <image> – Push an image to a registry.
  • docker pull <image> – Pull an image from a registry.
Logs and Info:
  • docker logs <container> – View container logs.
  • docker inspect <container> – Display detailed information about a container.
  • docker stats <container> – Display real-time container resource usage.
System and Cleanup:
  • docker info – Display system-wide information.
  • docker version – Display Docker version information.
  • docker help <command>– command after docker help to get detailed information.
  • docker system df – Show disk usage related to Docker.
  • docker system prune – Remove all unused resources (containers, networks, volumes).

Dockerfile instructions are used to assemble a Docker image. Here are some of the essentials:

  • FROM – This sets the base image for subsequent instructions.
  • RUN – This allows you to execute commands in a new layer on top of the current image and commit the results.
  • CMD – This specifies the command to run when a container is launched.
  • EXPOSE – You can inform Docker that the container listens on the specified network ports at runtime with this instruction.
  • ENV – Set environment variables using this instruction.
  • ADD/COPY – These instructions let you copy new files, directories, or remote file URLs and add them to the image filesystem.
  • ENTRYPOINT – Configure a container that will run as an executable with this instruction.
  • VOLUME – This creates a mount point and marks it as holding externally mounted volumes.
  • USER – This sets the user name or UID used when running the image and for any following RUN, CMD, and ENTRYPOINT instructions.
  • WORKDIR – This sets the working directory for any following RUN, CMD, ENTRYPOINT, COPY, and ADD instructions.

This Docker Cheat Sheet provides a quick reference for some of the most commonly used commands. Remember to replace <image>, <container>, <name>, and <volume> with the appropriate names or IDs in your Docker environment. Use docker --help or refer to the official Docker documentation for more details and options for each command.

Docker Cheat Sheet

Leave a Comment