👨🏽‍💻Docker Cheat Sheet🐬

As this is a cheat sheet I already assume you understand the basics of Docker and what it does.

SurajSarkar
3 min readFeb 6, 2023

Index

  • Docker CLI Commands
  • Docker Compose CLI Commands
  • Docker CLI Flags
docker image by SurajSarkar

Docker CLI Commands

Docker Images

  • docker images - List all images
  • docker pull <image_name> - Pull an image from a registry
  • docker build -t <image_name> . - Build an image from a Dockerfile in the current directory
  • docker push <image_name> - Push an image to a registry

Docker Containers

  • docker ps - List all running containers
  • docker run <image_name> - Start a new container from an image
  • docker run -it <image_name> bash - Start a new container in interactive mode and open a bash shell
  • docker stop <container_id> - Stop a running container
  • docker rm <container_id> - Remove a container

Dockerfile

A Dockerfile is a script that contains instructions for building an image.

FROM <base_image>

WORKDIR /app

COPY . .

RUN <command>

EXPOSE <port>

CMD ["<command>", "<arg1>", "<arg2>", ...]
  • FROM - Specifies the base image to use
  • WORKDIR - Sets the working directory for commands that follow
  • COPY - Copies files from the host to the image
  • RUN - Runs a command in the image
  • EXPOSE - Exposes a port for the container to listen on
  • CMD - Specifies the command to run when the container starts

Docker Compose CLI Commands

Docker Compose CLI Commands

  • docker-compose up - Start the services defined in the docker-compose.yml file
  • docker-compose down - Stop and remove all containers, networks, and volumes defined in the docker-compose.yml file
  • docker-compose ps - List the containers created by docker-compose
  • docker-compose logs - Show the logs of the containers defined in the docker-compose.yml file
  • docker-compose exec <service_name> <command> - Run a command in a running service container

Docker Compose File

Docker Compose CLI Commands

  • docker-compose up - Start the services defined in the docker-compose.yml file
  • docker-compose down - Stop and remove all containers, networks, and volumes defined in the docker-compose.yml file
  • docker-compose ps - List the containers created by docker-compose
  • docker-compose logs - Show the logs of the containers defined in the docker-compose.yml file
  • docker-compose exec <service_name> <command> - Run a command in a running service container

Docker Compose File

The docker-compose.yml file defines the services, networks, and volumes for your application.

version: '3'

services:
<service_name>:
build: .
ports:
- "<host_port>:<container_port>"
volumes:
- "<host_path>:<container_path>"

networks:
<network_name>:
driver: bridge

volumes:
<volume_name>:
driver: local
  • version - Specifies the version of the Compose file format
  • services - Defines the services for your application
  • build - Specifies the directory containing the Dockerfile for the service
  • ports - Maps a port on the host to a port in the container
  • volumes - Mounts a host path to a path in the container
  • networks - Defines the networks for your application
  • driver - Specifies the driver to use for the network or volume

Docker CLI Flags

docker run Flags

  • -d - Run the container in the background
  • -p <host_port>:<container_port> - Map a port on the host to a port in the container
  • -v <host_path>:<container_path> - Mount a host path to a path in the container
  • -e <variable_name>=<value> - Set an environment variable for the container
  • -it - Start the container in interactive mode and open a shell
  • --name <container_name> - Assign a name to the container
  • --rm - Automatically remove the container when it stops

docker build Flags

  • -t <image_name> - Assign a name and a tag to the image being built
  • --no-cache - Build the image without using the cache
  • --pull - Always force Docker to pull the latest version of the base image

docker images Flags

  • -a - List all images, including intermediate images
  • --filter <filter_expression> - Filter the list of images based on a condition
  • --format <output_format> - Specify the format of the output

docker ps Flags

  • -a - List all containers, including stopped containers
  • -q - Only display the container IDs
  • --filter <filter_expression> - Filter the list of containers based on a condition
  • --format <output_format> - Specify the format of the output

--

--

No responses yet