Docker Quick Commands Cheat Sheet
This guide provides a quick reference for the most common commands used in the Docker and Docker Compose workflows.
Managing Containers (Lifecycle)
| Command | Description |
|---|---|
docker run [OPTIONS] IMAGE [COMMAND] |
Creates and starts a new container from an image. |
docker ps |
Lists all running containers. |
docker ps -a |
Lists all containers, including stopped ones. |
docker start <container_id_or_name> |
Starts one or more stopped containers. |
docker stop <container_id_or_name> |
Stops one or more running containers gracefully. |
docker kill <container_id_or_name> |
Forcibly stops one or more running containers. |
docker rm <container_id_or_name> |
Removes one or more stopped containers. |
docker rm -f <container_id_or_name> |
Forcibly removes a container (even if it's running). |
docker prune containers |
Removes all stopped containers. |
Common docker run Flags
| Flag | Description |
|---|---|
-d |
Detached mode: runs the container in the background. |
-it |
Interactive mode: connects your terminal to the container's terminal. |
--rm |
Automatically removes the container when it exits. |
-p <host_port>:<container_port> |
Maps a port on the host to a port in the container. |
--name <my_container_name> |
Assigns a custom name to the container. |
-v <host_path>:<container_path> |
Mounts a host directory or volume into the container. |
-w /path/to/workdir |
Sets the working directory inside the container. |
--network <network_name> |
Connects the container to a specific network. |
Inspecting and Debugging
| Command | Description |
|---|---|
docker logs <container_id_or_name> |
Fetches the logs of a container. |
docker logs -f <container_id_or_name> |
Follows the log output in real-time. |
docker exec -it <container_id_or_name> bash |
Executes a command (like bash) inside a running container. |
docker inspect <container_id_or_name> |
Displays low-level information on Docker objects. |
Managing Images
| Command | Description |
|---|---|
docker images or docker image ls |
Lists all images on the local machine. |
docker pull <image_name>:<tag> |
Downloads an image from a registry (like Docker Hub). |
docker build -t <tag_name> . |
Builds an image from a Dockerfile in the current directory. |
docker rmi <image_id_or_name> |
Removes one or more images. |
docker login / docker logout |
Logs in to or out of a container registry. |
docker push <username>/<image_name>:<tag> |
Uploads an image to a container registry. |
Building Images (Dockerfile)
A Dockerfile is a text file with instructions for building a Docker image.
Example myprog.py:
print("Hello from my Docker container!")
Example Dockerfile:
# Start from an official base image
FROM python:3.9-slim
# Set the working directory inside the container
WORKDIR /app
# Copy the application code into the container
COPY myprog.py .
# Define the command to run when the container starts
CMD ["python", "myprog.py"]
Build Command:
# Build the image and tag it as 'my-python-app'
docker build -t my-python-app .
Run Command:
docker run --rm my-python-app
Managing Multi-Container Applications (Docker Compose)
Docker Compose is a tool for defining and running multi-container applications using a YAML file.
Example docker-compose.yml:
This file builds the image from the Dockerfile above and mounts the current directory into the container's /app directory, allowing for live code changes without rebuilding.
version: "3.8"
services:
my_app:
build: .
volumes:
- ./:/app
Common Docker Compose Commands
| Command | Description |
|---|---|
docker-compose up |
Creates and starts containers defined in the YAML file. |
docker-compose up -d |
Creates and starts containers in detached mode. |
docker-compose down |
Stops and removes containers, networks, and volumes. |
docker-compose ps |
Lists the containers for the current project. |
docker-compose logs |
Displays logs from the services. |
docker-compose exec <service_name> bash |
Executes a command in a running service. |
docker-compose build |
Builds or rebuilds the images for the services. |