Table of contents
    blog cover

    Docker compose basics: Essential commands

    Software Engineer
    Software Engineer
    Docker
    Docker
    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

    Before diving into Docker Compose, ensure that you have Docker installed on your machine. You can follow instructions on Docker docs

    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.

    Essential Docker Compose Commands

    // language: bash
    docker-compose up
    The docker-compose up command is used to start the containers defined in your docker-compose.yml file. It creates and starts the containers, pulling the necessary images from Docker Hub if they don't exist locally. It also establishes the networking between the containers.

    // language: bash
    docker-compose down
    The docker-compose down command stops and removes the containers defined in your docker-compose.yml file. It also removes the associated networks and volumes, freeing up system resources.

    // language: bash
    docker-compose ps
    The docker-compose ps command lists the running containers that are part of your Docker Compose project. It provides information such as container names, status, and exposed ports.

    // language: bash
    docker-compose logs
    The docker-compose logs command displays the logs of the containers defined in your docker-compose.yml file. It allows you to monitor the output and troubleshoot any issues that may arise.

    // language: bash
    docker-compose exec
    The docker-compose exec command lets you execute a command inside a running container. For example, docker-compose exec webserver ls -l would run the ls -l command inside the container named "webserver."

    // language: bash
    docker-compose build
    The docker-compose build command builds or rebuilds the images defined in your docker-compose.yml file. It's useful when you make changes to your application's code or dependencies and need to update the containers accordingly.

    // language: bash
    docker-compose restart
    The docker-compose restart command restarts the containers defined in your docker-compose.yml file without rebuilding them. It can be handy when you want to apply configuration changes without going through a complete rebuild process.

    // language: bash
    docker-compose scale
    The docker-compose scale command allows you to scale the number of containers for a specific service. For example, docker-compose scale web=3
    It's important to note that scaling is applicable only to services defined as "replicated" or "scale" services in the docker-compose.yml file. These services need to have a specified deploy section with the replicas option set. Here's an example of how to define a replicated service in the docker-compose.yml file:
    // language: yaml
    services:  
      web:    
        image: myapp:latest    
        deploy:      
          replicas: 1
    Created at 2023-05-22 18:56:28 +0700

    Related blogs