👨🏽💻Docker Cheat Sheet🐬
As this is a cheat sheet I already assume you understand the basics of Docker and what it does.
3 min readFeb 6, 2023
Index
- Docker CLI Commands
- Docker Compose CLI Commands
- Docker CLI Flags
Docker CLI Commands
Docker Images
docker images
- List all imagesdocker pull <image_name>
- Pull an image from a registrydocker build -t <image_name> .
- Build an image from a Dockerfile in the current directorydocker push <image_name>
- Push an image to a registry
Docker Containers
docker ps
- List all running containersdocker run <image_name>
- Start a new container from an imagedocker run -it <image_name> bash
- Start a new container in interactive mode and open a bash shelldocker stop <container_id>
- Stop a running containerdocker 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 useWORKDIR
- Sets the working directory for commands that followCOPY
- Copies files from the host to the imageRUN
- Runs a command in the imageEXPOSE
- Exposes a port for the container to listen onCMD
- 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 thedocker-compose.yml
filedocker-compose down
- Stop and remove all containers, networks, and volumes defined in thedocker-compose.yml
filedocker-compose ps
- List the containers created bydocker-compose
docker-compose logs
- Show the logs of the containers defined in thedocker-compose.yml
filedocker-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 thedocker-compose.yml
filedocker-compose down
- Stop and remove all containers, networks, and volumes defined in thedocker-compose.yml
filedocker-compose ps
- List the containers created bydocker-compose
docker-compose logs
- Show the logs of the containers defined in thedocker-compose.yml
filedocker-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 formatservices
- Defines the services for your applicationbuild
- Specifies the directory containing the Dockerfile for the serviceports
- Maps a port on the host to a port in the containervolumes
- Mounts a host path to a path in the containernetworks
- Defines the networks for your applicationdriver
- 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