Docker Cursor

Loading Docker Cheatsheet...

Docker Cheatsheet

Your Handy Guide to Containerized Commands

Basic Image Management

docker images

Lists downloaded images.

docker pull [image]

Downloads an image from a registry (like Docker Hub).

docker build -t [name] .

Builds an image from a Dockerfile in the current directory.

docker rmi [image]

Removes one or more images.

Advanced Image Management

docker tag [source] [target]

Creates a tag TARGET_IMAGE that refers to SOURCE_IMAGE.

docker push [repository/image]

Pushes an image to a registry (like Docker Hub).

docker inspect [image]

Returns detailed information about an image.

Container Lifecycle

docker run [image]

Creates and starts a new container from an image.

docker stop [container]

Stops one or more running containers.

docker rm [container]

Removes one or more stopped containers.

Use -f to force remove a running container.

Container Interaction

docker ps

Lists running containers.

docker ps -a

Lists all containers (running and stopped).

docker logs [container]

Fetches the logs of a container.

Use -f to follow log output.

Networking

docker network ls

Lists networks.

docker network create [NETWORK_NAME]

Creates a new network.

docker network connect [NETWORK_NAME] [CONTAINER_NAME]

Connects a container to a network.

Volumes

docker volume ls

Lists volumes.

docker volume create [VOLUME_NAME]

Creates a new volume.

docker run -v [VOLUME_NAME]:[CONTAINER_PATH] ...

Mounts a volume into a container.

Dockerfile Examples

# Use specific base image with version FROM ubuntu:20.04 # Set working directory WORKDIR /app # Use multi-stage builds # Install only necessary dependencies RUN apt-get update && \ apt-get install -y --no-install-recommends python3 && \ rm -rf /var/lib/apt/lists/* # Copy only necessary files COPY requirements.txt . RUN pip3 install -r requirements.txt # Copy application code COPY . . # Set environment variables ENV APP_ENV=production # Expose port EXPOSE 8080 # Use non-root user USER appuser # Define startup command CMD ["python3", "app.py"]

A sample Dockerfile for a Python application.

Basic Compose Commands

docker-compose up

Creates and starts containers defined in docker-compose.yml.

docker-compose down

Stops and removes containers, networks, volumes, and images created by up.

docker-compose ps

Lists containers managed by docker-compose.

docker-compose logs

Displays log output from services.

Advanced Compose Commands

docker-compose build

Builds or rebuilds services defined in docker-compose.yml.

docker-compose up -d

Creates and starts containers in detached mode (background).

docker-compose restart

Restarts all stopped and running services.

Sample docker-compose.yml

# Specifies the Compose file format version version: '3.8' # Defines the containers to be created services: # First service (application container) web: # Builds image from Dockerfile in current directory build: . # Maps container ports to host # Host port 5000 maps to container port 5000 ports: - "5000:5000" # Mounts host directory into container # Current directory mounted to /app in container volumes: - .:/app # Sets environment variables # Configures Flask to run in dev mode environment: - FLASK_ENV=development # Second service (database container) database: # Uses official MySQL 8 image image: mysql:8 # Configures database settings environment: - MYSQL_DATABASE=myapp - MYSQL_ROOT_PASSWORD=secret - MYSQL_USER=myapp - MYSQL_PASSWORD=secret # Persistent data storage # Preserves database data between container restarts volumes: - mysql_data:/var/lib/mysql # Maps MySQL port to host ports: - "3306:3306" # Defines named volumes for persistent data volumes: mysql_data:

A sample docker-compose.yml file that defines a web application with a MySQL database.

Docker Security

docker scan [image]

Scan images for vulnerabilities using Docker Scan

Swarm Management

docker swarm init

Initialize a Docker Swarm cluster

Docker Best Practices

Use .dockerignore to exclude unnecessary files.

Always specify explicit image tags.

Minimize layer count in Dockerfile.

Use multi-stage builds to reduce image size.

Avoid running containers as root.