Cloud Native 6 min read

Master Docker Compose: Quick Setup, YAML Basics, and Advanced Tips

Docker Compose, Docker’s official orchestration tool, lets you define and run multi‑container applications via a simple YAML file, covering its history, installation, service definitions, essential CLI commands, and advanced features like environment variables, scaling, and network configuration to streamline development, testing, and production workflows.

Code Mala Tang
Code Mala Tang
Code Mala Tang
Master Docker Compose: Quick Setup, YAML Basics, and Advanced Tips

Docker Compose is Docker’s official orchestration tool, first released in June 2014. It originated from the open‑source project fig created by Piston Cloud Computing, which Docker later integrated into its ecosystem. The tool enables defining and managing multi‑container applications with a single YAML file, ensuring consistency across development, testing, and production environments.

Version History

1.x : Initial release with basic orchestration.

2.x : Added support for the Docker Engine API.

3.x : Introduced service dependency ordering.

4.x : Added environment variables and command‑line options.

5.x : Added Docker Stacks support for Swarm mode.

How to Use Docker Compose

Installation

On most systems Docker Compose can be installed as a Python package.

<code># Install pip (Python package manager)
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py

# Install Docker Compose with pip
pip install docker-compose</code>

Defining Services

Create a docker-compose.yml file. A basic example defines a web service using the nginx:alpine image and a database service using postgres:latest :

<code>version: '3'
services:
  web:
    image: "nginx:alpine"
    ports:
      - "80:80"
    volumes:
      - "web-data:/var/www"
  database:
    image: "postgres:latest"
    volumes:
      - "db-data:/var/lib/postgresql/data"

volumes:
  web-data:
  db-data:</code>

Common CLI Commands

Start services: <code>docker-compose up</code>

Run services in background: <code>docker-compose up -d</code>

Stop services: <code>docker-compose down</code>

Show service status: <code>docker-compose ps</code>

Rebuild services: <code>docker-compose up --build</code>

View logs: <code>docker-compose logs</code>

Enter a container: <code>docker-compose exec [service-name] /bin/bash</code>

Stop a single service: <code>docker-compose stop [service-name]</code>

Start a single service: <code>docker-compose start [service-name]</code>

Restart services: <code>docker-compose restart</code>

Advanced Usage

Docker Compose also supports environment files, scaling, and custom networks.

Environment variables:

<code>version: '3'
services:
  web:
    image: "nginx:alpine"
    env_file:
      - web.env</code>

Scaling (replicas):

<code>version: '3'
services:
  web:
    image: "nginx:alpine"
    deploy:
      replicas: 3</code>

Network configuration:

<code>version: '3'
services:
  web:
    image: "nginx:alpine"
    networks:
      - webnet
networks:
  webnet:</code>

Conclusion

Docker Compose is a powerful tool for defining and running multi‑container Docker applications, simplifying deployment and management while boosting development efficiency.

DockerYAMLContainer OrchestrationDocker ComposeCLI commands
Code Mala Tang
Written by

Code Mala Tang

Read source code together, write articles together, and enjoy spicy hot pot together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.