logo
eng-flag

Docker Cheatsheet

Table of Contents

  1. Docker Installation
  2. Basic Docker Commands
  3. Working with Docker Images
  4. Managing Docker Containers
  5. Docker Networking
  6. Docker Volumes
  7. Dockerfile Basics
  8. Docker Compose
  9. Docker Swarm
  10. Docker Security Best Practices
  11. Troubleshooting Docker

Docker Installation

Install Docker on Ubuntu

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

Install Docker on macOS

  1. Download Docker Desktop for Mac from the official website
  2. Double-click the downloaded .dmg file and drag Docker to the Applications folder

Install Docker on Windows

  1. Download Docker Desktop for Windows from the official website
  2. Double-click the installer to run it
  3. Follow the installation wizard

Basic Docker Commands

Check Docker version

docker --version

View Docker info

docker info

List Docker CLI commands

docker

Get help on a specific command

docker <command> --help

Working with Docker Images

List Docker images

docker images

Pull an image from Docker Hub

docker pull <image_name>:<tag>

Example:

docker pull nginx:latest

Build an image from a Dockerfile

docker build -t <image_name>:<tag> <path_to_dockerfile>

Example:

docker build -t myapp:1.0 .

Remove an image

docker rmi <image_id_or_name>

Remove all unused images

docker image prune -a

Managing Docker Containers

Run a container

docker run <options> <image_name>:<tag>

Example:

docker run -d -p 80:80 --name my_nginx nginx:latest

List running containers

docker ps

List all containers (including stopped)

docker ps -a

Stop a running container

docker stop <container_id_or_name>

Start a stopped container

docker start <container_id_or_name>

Remove a container

docker rm <container_id_or_name>

Remove all stopped containers

docker container prune

Execute a command in a running container

docker exec -it <container_id_or_name> <command>

Example:

docker exec -it my_nginx /bin/bash

View container logs

docker logs <container_id_or_name>

Docker Networking

List networks

docker network ls

Create a network

docker network create <network_name>

Connect a container to a network

docker network connect <network_name> <container_id_or_name>

Disconnect a container from a network

docker network disconnect <network_name> <container_id_or_name>

Remove a network

docker network rm <network_name>

Docker Volumes

List volumes

docker volume ls

Create a volume

docker volume create <volume_name>

Remove a volume

docker volume rm <volume_name>

Mount a volume to a container

docker run -v <volume_name>:<container_path> <image_name>

Example:

docker run -d -v my_data:/app/data myapp:latest

Dockerfile Basics

Sample Dockerfile

# Use an official Python runtime as the base image
FROM python:3.9-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"]

Common Dockerfile instructions

  • FROM: Sets the base image
  • WORKDIR: Sets the working directory
  • COPY: Copies files from host to container
  • ADD: Copies files and can also download from URLs and extract archives
  • RUN: Executes commands in a new layer
  • ENV: Sets environment variables
  • EXPOSE: Informs Docker that the container listens on specified ports
  • CMD: Provides defaults for an executing container
  • ENTRYPOINT: Configures a container to run as an executable

Docker Compose

Sample docker-compose.yml

version: '3'
services:
  web:
    build: .
    ports:
      - "5000:5000"
  redis:
    image: "redis:alpine"

Start services defined in docker-compose.yml

docker-compose up

Stop services defined in docker-compose.yml

docker-compose down

View logs of services

docker-compose logs

Scale a service

docker-compose up -d --scale <service_name>=<num_instances>

Example:

docker-compose up -d --scale web=3

Docker Swarm

Initialize a swarm

docker swarm init

Join a swarm as a worker

docker swarm join --token <worker_token> <manager_ip>:<manager_port>

List nodes in the swarm

docker node ls

Deploy a stack to the swarm

docker stack deploy -c <docker-compose-file> <stack_name>

List stacks

docker stack ls

Remove a stack

docker stack rm <stack_name>

Docker Security Best Practices

  1. Use official base images
  2. Regularly update and patch your images
  3. Scan images for vulnerabilities
  4. Use multi-stage builds to reduce image size
  5. Don't run containers as root
  6. Use secrets management for sensitive data
  7. Limit container resources
  8. Use read-only file systems when possible
  9. Implement network segmentation
  10. Enable Docker Content Trust for image signing and verification

Troubleshooting Docker

View container resource usage

docker stats

Inspect container details

docker inspect <container_id_or_name>

View Docker events

docker events

Check Docker disk usage

docker system df

Clean up Docker system

docker system prune

View container processes

docker top <container_id_or_name>

Debug a running container

docker exec -it <container_id_or_name> /bin/sh

2024 © All rights reserved - buraxta.com