0%
Reading Settings
Font Size
18px
Line Height
1.5
Letter Spacing
0.01em
Font Family
Table of contents
    blog cover

    Docker compose basics: Essential commands

    Software Engineer
    Software Engineer
    Docker
    Docker
    published 2024-09-22 18:29:37 +0700 · 3 mins read
    Docker has revolutionized the way we develop, package, and deploy applications. It allows us to containerize our applications, providing a consistent and reproducible environment across different platforms. Docker Compose takes it a step further by enabling us to define and manage multi-container applications with ease. In this guide, we'll explore the basics of Docker Compose and learn how to use its essential commands.

    Installation and Setup

    The easiest and recommended way to get Docker Compose is to install Docker Desktop. Docker Desktop includes Docker Compose along with the Docker Engine and Docker CLI, which are Compose prerequisites.

    You can follow instructions on Docker docs to get started.

    Understanding the Docker Compose YAML File

    Docker Compose uses a YAML file to define the services, networks, and volumes of your application. This file, usually named docker-compose.yml, acts as a blueprint for your containers. It consists of sections that define each service, including its image, ports, environment variables, and more.

    Here's a sample docker-compose.yml file and a breakdown of its common fields.

    // language: yaml
    version: '3.8'
    
    services:
      web:
        build: .
        ports:
          - "8000:8000"
        volumes:
          - .:/app
        depends_on:
          - db
        environment:
          - DB_HOST=db
          - DB_USER=user
          - DB_PASSWORD=secret
          - DB_NAME=myapp_db
    
      db:
        image: postgres:13
        volumes:
          - postgres_data:/var/lib/postgresql/data
        environment:
          - POSTGRES_USER=user
          - POSTGRES_PASSWORD=secret
          - POSTGRES_DB=myapp_db
    
    volumes:
      postgres_data:
    
    Explanation of Fields
    • version: '3.8': Specifies the version of the Compose file format.
      • The version field is now officially considered obsolete and is deprecated. The modern docker compose CLI (Compose V2) uses the Compose Specification by default. It's safe to remove this line from your Compose files.
    • services: The top-level key defining all the containers in your application.
      • web: A service named web.
        • build: .: Tells Docker Compose to build an image from a Dockerfile in the current directory (.).
        • ports: - "8000:8000": Maps port 8000 on the host to port 8000 in the container.
        • volumes: - .:/app: Creates a bind mount, linking the current host directory to the /app directory inside the container.
        • depends_on: - db: Ensures the db service starts before the web service.
        • environment: Sets environment variables for the container.
      • db: A service for our database.
        • image: postgres:13: Pulls the postgres:13 image from Docker Hub.
        • volumes: - postgres_data:/var/lib/postgresql/data: Uses a named volume to persist the database data.
    • volumes: Defines named volumes used by services. postgres_data: creates a volume named postgres_data.

    docker compose vs. docker-compose: A Note on Syntax

    You may see two different commands for Docker Compose: docker-compose and docker compose.
    • docker-compose is the original, standalone command-line tool.
    • docker compose is the newer, integrated command that is part of the Docker CLI. It was introduced with Docker v1.27.0.

    The functionality is identical. The docker compose syntax is the recommended modern approach and is included with Docker Desktop. It simplifies the command structure by making compose a subcommand of docker.

    Essential Docker Compose Commands

    Build Your Services

    • docker compose build: Builds or rebuilds the images for your services. This is necessary when you have made changes to the Dockerfile.

    Start and Stop Your Containers

    • docker compose up: Builds, creates, and starts all services in the docker-compose.yml file.

    • docker compose down: Stops and removes all containers, networks, and volumes created by up.

    • docker compose stop: Stops running containers without removing them. The containers can be restarted later.

    • docker compose start: Starts containers that have been stopped.

    • docker compose restart: Restarts all containers in the project.

    Inspect and Manage Containers

    • docker compose ps: Lists all containers in your project, showing their state, ports, and names.

    • docker compose logs: Displays the logs for all services. You can also specify a service to see its individual logs (e.g. docker compose logs webserver).

    • docker compose exec [service] [command]: Executes a command inside a running container (e.g. docker compose exec webserver /bin/bash).

    Related blogs