3v-Hosting Blog

Docker-Compose Basics

Administration

7 min read


Introduction to Docker-Compose

 

Docker-Compose is a powerful tool that allows developers and system administrators to define and manage multi-container Docker applications.It simplifies the deployment of complex environments by using a declarative YAML file to specify services, networks, and volumes.Rather than running multiple docker run commands with intricate configurations, Docker-Compose provides a streamlined approach to orchestrating containers efficiently.

Maintaining and deploying applications across different environments is made easier by using a single configuration file (docker-compose.yml), making it a crucial component in modern DevOps workflows.

 

 

 

 

Key Features of Docker-Compose

 

Docker-Compose offers a range of features that make it an essential tool for containerized applications:

    Multi-Container Management: Allows defining and running multiple interdependent containers in a single configuration file.
    Service-Oriented Architecture: Enables a microservices-based approach, where each service runs in its own isolated container.
    Networking: Automatically creates and manages networks between defined services, simplifying communication.
    Volume Management: Defines persistent storage for databases and stateful applications.
    Environment Variables Support: Helps manage different configurations for development, testing, and production environments.
    Scalability: Facilitates horizontal scaling of services when necessary.
    Logging and Debugging: Provides structured logs and debugging tools for better observability.

 

 

 

 


Installing Docker-Compose

 

Before using Docker-Compose, ensure that Docker is installed on your system. Docker-Compose is included in Docker Desktop, but for standalone installations, follow these steps:


Installing on Linux

    sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    sudo chmod +x /usr/local/bin/docker-compose


Verify the installation:

    docker-compose --version

 

 

Installing on Windows and macOS

For Windows and macOS users, Docker-Compose is included in Docker Desktop. Simply download and install Docker Desktop from the official Docker website.

 

 

 

 


Writing a Basic docker-compose.yml File

 

A docker-compose.yml file defines the services, networks, and volumes for a multi-container application. Below is an example of a basic configuration for a web application using Nginx and a PostgreSQL database:

    version: '3.8'
    services:
      web:
        image: nginx:latest
        ports:
          - "80:80"
        volumes:
          - ./html:/usr/share/nginx/html
        depends_on:
          - db

      db:
        image: postgres:latest
        environment:
          POSTGRES_USER: user
          POSTGRES_PASSWORD: password
          POSTGRES_DB: app_db
        volumes:
          - pg_data:/var/lib/postgresql/data

    volumes:
      pg_data:

 

 

 

 

Running and Managing Services with Docker-Compose

 


Starting the Services

Once the docker-compose.yml file is set up, launch the application with:

    docker-compose up -d

The -d flag runs the services in detached mode (background execution).

 


Viewing Running Services

Check the status of running services with:

    docker-compose ps

 


Viewing Logs

Monitor service logs using:

    docker-compose logs -f

 


Stopping Services

To stop the services, run:

    docker-compose down

This command stops and removes all containers, networks, and volumes defined in the docker-compose.yml file.

 

 


 

Other useful articles in our Blog:


    - How To Install and Use Docker on Ubuntu 22.04

    - How to Create Your Own Docker Image

    - How To Remove Docker Images, Containers, and Volumes

    - What is the VPS or VDS

 


 


Defining Networks and Volumes

 

Custom Network Configuration

Docker-Compose allows defining custom networks to isolate services:

    networks:
      frontend:
      backend:

    services:
      app:
        image: myapp:latest
        networks:
          - frontend
          - backend

      db:
        image: mysql:latest
        networks:
          - backend

 

 

Persistent Data Storage with Volumes

Volumes ensure data persistence when containers are restarted:

    volumes:
      mysql_data:

    services:
      database:
        image: mysql:latest
        volumes:
          - mysql_data:/var/lib/mysql

 

 

 

 

Using Environment Variables in Docker-Compose

For better configurability, environment variables can be stored in an .env file and referenced in docker-compose.yml:


.env File:

    POSTGRES_USER=admin
    POSTGRES_PASSWORD=securepassword


docker-compose.yml:

    services:
      db:
        image: postgres:latest
        environment:
          POSTGRES_USER: ${POSTGRES_USER}
          POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}

 

 

 


Scaling Services with Docker-Compose

 

Docker-Compose supports service scaling using the --scale flag:

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

This command starts three instances of the web service, distributing traffic among them.

 

 

 

 

Best Practices for Using Docker-Compose

 

    Keep Services Stateless: Store persistent data in volumes rather than within containers.
    Use .env Files for Configuration: Avoid hardcoding credentials in docker-compose.yml.
    Leverage Named Volumes: Prevent data loss during container updates.
    Define Resource Limits: Specify memory and CPU limits to prevent resource exhaustion.
    Implement Health Checks: Use healthcheck to monitor service health.

        Example of a health check:

        services:
          db:
            image: mysql:latest
            healthcheck:
              test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
              interval: 30s
              retries: 3

 

 

 

 

Conclusion

Docker-Compose is the most effective way to manage multi-container applications. Its structured and declarative configuration approach is unparalleled. Whether you're developing a local application, deploying a microservices architecture, or running a production system, Docker-Compose will enhance your efficiency by streamlining service deployment, networking, and data persistence.

If you're a DevOps engineer, developer or system administrator looking to optimise your containerised environments, then you need to understand the basics of Docker-Compose. Follow best practices and leverage its powerful features to create scalable, maintainable and robust container-based applications.

3v-Hosting Team

Author

3v-Hosting Team

The 3v-Hosting Team is made up of a dedicated group of engineers and operators who are all about building and maintaining the backbone of our services. Every day, we dive into the world of virtual and dedicated servers, handling everything from deployment and monitoring to troubleshooting real-world issues that pop up in production environments. Most of our articles stem from hands-on experience rather than just theory. We share insights on the challenges we face: performance hiccups, configuration missteps, networking intricacies, and architectural choices that impact stability and reliability. Our mission is straightforward – we want to share knowledge that empowers you to manage your projects with fewer surprises and a lot more predictability.

Installing Nginx on AlmaLinux 9
Installing Nginx on AlmaLinux 9

Installing and Configuring Nginx on AlmaLinux 9: Commands, Startup, Firewall, Configurations, Virtual Hosts, and Common Errors. A step-by-step guide for a quick...

8 min