1. Docker Basics Docker Daemon: The background service that manages Docker objects. Docker Client: The command-line tool ($ docker) that interacts with the daemon. Images: Read-only templates with instructions for creating a Docker container. Containers: Runnable instances of an image. They are isolated environments. Registries: Stores Docker images (e.g., Docker Hub). Volumes: Persistent data storage for containers. Networks: Connect containers to each other and to the outside world. 2. Image Management Pulling Images Pull an image from Docker Hub: docker pull [image_name]:[tag] (e.g., $ docker pull ubuntu:latest) Listing Images List all local images: docker images List image IDs only: docker images -q Removing Images Remove a specific image: docker rmi [image_id_or_name] Remove all unused images: docker image prune Force remove an image (even if used by a container): docker rmi -f [image_id_or_name] Building Images (Dockerfile) Build an image from a Dockerfile in the current directory: docker build -t [image_name]:[tag] . Specify a different Dockerfile path: docker build -f /path/to/Dockerfile -t [image_name]:[tag] . 3. Container Management Running Containers Run a container in detached mode (background): docker run -d --name [container_name] [image_name] Run a container in interactive mode (foreground): docker run -it --name [container_name] [image_name] /bin/bash Run with port mapping (host:container): docker run -p 8080:80 --name mynginx nginx Run with volume mapping (host:container): docker run -v /host/path:/container/path --name mydata ubuntu Run with environment variables: docker run -e "MY_VAR=value" ubuntu Listing Containers List all running containers: docker ps List all containers (running and stopped): docker ps -a List container IDs only: docker ps -q Stopping/Starting Containers Stop a running container: docker stop [container_id_or_name] Start a stopped container: docker start [container_id_or_name] Restart a container: docker restart [container_id_or_name] Kill a container (force stop): docker kill [container_id_or_name] Removing Containers Remove a stopped container: docker rm [container_id_or_name] Force remove a running container: docker rm -f [container_id_or_name] Remove all stopped containers: docker container prune Executing Commands in a Running Container Execute a command in a running container: docker exec -it [container_id_or_name] /bin/bash Viewing Container Logs View logs of a container: docker logs [container_id_or_name] Follow logs in real-time: docker logs -f [container_id_or_name] Inspecting Containers/Images Get detailed information about a container or image: docker inspect [id_or_name] 4. Dockerfile Instructions FROM: Base image for the build (e.g., FROM ubuntu:latest). Must be the first instruction. RUN: Executes commands during image build (e.g., RUN apt-get update && apt-get install -y git). CMD: Default command to execute when the container starts. Only one CMD per Dockerfile. Can be overridden. ENTRYPOINT: Configures a container that will run as an executable. Often used with CMD as default arguments. COPY: Copies files/directories from host to image (e.g., COPY . /app). ADD: Similar to COPY, but can also extract tar files and fetch remote URLs. WORKDIR: Sets the working directory for subsequent RUN, CMD, ENTRYPOINT, COPY, ADD instructions. EXPOSE: Informs Docker that the container listens on the specified network ports at runtime. Does not actually publish the port. ENV: Sets environment variables (e.g., ENV APP_COLOR=blue). ARG: Defines build-time variables (e.g., ARG VERSION=1.0). VOLUME: Creates a mount point for external volumes. USER: Sets the user name or UID to use when running the image (e.g., USER root). 5. Docker Compose Define and run multi-container Docker applications. Basic Commands Start services defined in docker-compose.yml : docker-compose up Start services in detached mode: docker-compose up -d Stop and remove containers, networks, and volumes: docker-compose down Build or rebuild services: docker-compose build List services: docker-compose ps View logs of services: docker-compose logs Execute a command in a service container: docker-compose exec [service_name] [command] docker-compose.yml Structure version: '3.8' services: web: build: . ports: - "5000:5000" volumes: - .:/code environment: FLASK_ENV: development depends_on: - redis redis: image: "redis:alpine" networks: default: driver: bridge version: Specify Compose file format version. services: Define application services. build: Path to the Dockerfile or context. image: Specify an existing image. ports: Port mapping (HOST:CONTAINER). volumes: Volume mapping (HOST:CONTAINER). environment: Environment variables for the service. depends_on: Define service dependencies (does not wait for service to be "ready"). networks: Define custom networks. 6. Volumes & Networks Volumes Create a named volume: docker volume create [volume_name] List volumes: docker volume ls Inspect a volume: docker volume inspect [volume_name] Remove a volume: docker volume rm [volume_name] Remove all unused local volumes: docker volume prune Networks List networks: docker network ls Create a custom bridge network: docker network create --driver bridge [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] Inspect a network: docker network inspect [network_name] Remove a network: docker network rm [network_name] Remove all unused networks: docker network prune 7. Docker Swarm (Basic) Orchestrate a cluster of Docker engines. Initialize Swarm mode: docker swarm init --advertise-addr [manager_ip] Join a worker to the Swarm: docker swarm join --token [token] [manager_ip]:[port] Deploy a stack (using a Compose file): docker stack deploy -c [compose_file.yml] [stack_name] List services in a stack: docker stack services [stack_name] List running stacks: docker stack ls Remove a stack: docker stack rm [stack_name] List Swarm nodes: docker node ls Leave the Swarm: docker swarm leave 8. Cleaning Up Remove all unused Docker objects (images, containers, networks, volumes): docker system prune Remove all unused Docker objects, including dangling images and build cache: docker system prune -a