Docker Cheat Sheet

Whether you’re just getting started or need a quick reference, this Docker cheat sheet covers essential commands, Dockerfile instructions, Docker Compose configurations, and registry interactions. Keep this guide handy to navigate Docker with ease and efficiency.

Docker Basics

Images

  • List all images:
docker images
  • Pull an image from Docker Hub:
docker pull <image_name>
  • Remove an image:
docker rmi <image_id>

Containers

  • List all running containers:
docker ps
  • List all containers (including stopped ones):
docker ps -a
  • Start a container:
docker start <container_id>
  • Stop a running container:
docker stop <container_id>
  • Remove a container:
docker rm <container_id>

Dockerfile

  • Dockerfile example:

# Use an official Python runtime as a parent image

FROM python:3.8-slim

# Set the working directory in the container

WORKDIR /app

# Copy the current directory contents into the container at /app

COPY . /app

# Install any needed packages specified in requirements.txt

RUN pip install --no-cache-dir -r requirements.txt

# Make port 80 available to the world outside this container

EXPOSE 80

# Define environment variable

ENV NAME World

# Run app.py when the container launches

CMD ["python", "app.py"]

Docker Compose

  • Docker Compose example (docker-compose.yml):
version: '3'
services:
web:
build: .
ports: - "5000:5000"
volumes: - .:/code
environment:
FLASK_ENV: development

Docker Commands

  • Build an image from the Dockerfile in the current directory:
docker build -t <image_name> .
  • Run a command in a running container:
docker exec -it <container_id> <command>
  • View logs from a container:
docker logs <container_id>
  • Clean up unused Docker resources:
docker system prune

Docker Registry

  • Login to a Docker registry:
docker login <registry_url>
  • Push an image to a Docker registry:
docker push <image_name>
  • Pull an image from a Docker registry:
docker pull <image_name>

This cheat sheet covers essential Docker commands and configurations. For more details, refer to the official Docker documentation.