Skip to main content

Docker And Docker Compose Cheatsheet

· 4 min read
Sivabharathy
### Docker and Docker Compose Cheatsheet

Docker Commands

  1. Docker Basics

    • docker --version: Check Docker version.
    • docker info: Display system-wide information.
  2. Images

    • docker images: List all images.
    • docker pull <image>: Download an image from a registry.
    • docker build -t <name>:<tag> .: Build an image from a Dockerfile in the current directory.
    • docker rmi <image>: Remove an image.
  1. Containers

    • docker ps: List running containers.
    • docker ps -a: List all containers.
    • docker run -it <image>: Run a container interactively.
    • docker run -d <image>: Run a container in detached mode.
    • docker run -p <host_port>:<container_port> <image>: Run a container with port mapping.
    • docker stop <container>: Stop a running container.
    • docker start <container>: Start a stopped container.
    • docker restart <container>: Restart a container.
    • docker rm <container>: Remove a stopped container.
    • docker logs <container>: View container logs.
    • docker exec -it <container> <command>: Execute a command inside a running container.
  2. Volumes

    • docker volume create <volume>: Create a volume.
    • docker volume ls: List all volumes.
    • docker volume rm <volume>: Remove a volume.
    • docker run -v <volume>:/path/in/container <image>: Run a container with a volume.
  3. Networks

    • docker network create <network>: Create a network.
    • docker network ls: List all networks.
    • docker network rm <network>: Remove a network.
    • docker run --network <network> <image>: Run a container on a network.

Dockerfile Instructions

  1. Basic Instructions

    • FROM <image>: Set the base image.
    • RUN <command>: Execute a command during the build process.
    • COPY <src> <dest>: Copy files/directories to the image.
    • ADD <src> <dest>: Similar to COPY, but can also extract TAR files and download URLs.
    • WORKDIR <path>: Set the working directory.
    • CMD ["executable", "param1", "param2"]: Set the default command to run.
    • ENTRYPOINT ["executable", "param1", "param2"]: Configure a container to run as an executable.
    • EXPOSE <port>: Expose a port.
    • ENV <key>=<value>: Set an environment variable.
  2. Advanced Instructions

    • ARG <name>: Define a build-time variable.
    • LABEL <key>=<value>: Add metadata to the image.
    • ONBUILD <instruction>: Add a trigger instruction for the next build stage.
    • HEALTHCHECK <options> CMD <command>: Check container health.

Docker Compose Commands

  1. Basics

    • docker-compose --version: Check Docker Compose version.
    • docker-compose up: Create and start containers.
    • docker-compose up -d: Start containers in detached mode.
    • docker-compose down: Stop and remove containers, networks, images, and volumes.
    • docker-compose ps: List containers.
    • docker-compose logs: View logs of all containers.
    • docker-compose logs <service>: View logs of a specific service.
    • docker-compose exec <service> <command>: Execute a command in a running container.
    • docker-compose build: Build or rebuild services.
    • docker-compose stop: Stop running containers.
    • docker-compose start: Start existing containers.
    • docker-compose restart: Restart containers.
  2. Docker Compose File (docker-compose.yml) Structure

    version: '3.8'

    services:
    app:
    image: node:14
    build:
    context: .
    dockerfile: Dockerfile
    ports:
    - "3000:3000"
    environment:
    - NODE_ENV=development
    volumes:
    - .:/usr/src/app
    - /usr/src/app/node_modules
    depends_on:
    - mongo

    mongo:
    image: mongo:4.4
    ports:
    - "27017:27017"
    volumes:
    - mongo-data:/data/db

    volumes:
    mongo-data:
  3. Common Service Configuration Options

    • build: Configuration options for the build (context, dockerfile, args).
    • image: Specify an image to use.
    • command: Override the default command.
    • ports: Map ports from the host to the container.
    • volumes: Mount host paths or named volumes.
    • environment: Set environment variables.
    • depends_on: Specify service dependencies.
    • networks: Connect services to specific networks.
    • restart: Define the restart policy (no, always, on-failure, unless-stopped).

Examples

1. Simple Node.js Application with MongoDB

Dockerfile:

# Dockerfile
FROM node:14

WORKDIR /usr/src/app

COPY package*.json ./
RUN npm install

COPY . .

EXPOSE 3000
CMD ["npx", "babel-node", "src/index.js"]

docker-compose.yml:

version: '3.8'

services:
app:
build: .
ports:
- "3000:3000"
env_file:
- .env
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
depends_on:
- mongo

mongo:
image: mongo:4.4
ports:
- "27017:27017"
volumes:
- mongo-data:/data/db

volumes:
mongo-data:

.env:

PORT=3000
MONGODB_URL=mongodb://mongo:27017/mydatabase

Conclusion

This cheatsheet provides a quick reference for essential Docker and Docker Compose commands and configurations. Whether you're building, running, or managing containers, these commands and instructions will help streamline your development and deployment workflows.