Do You Prefer Docker Compose Over Docker CLI? Here’s Why You Should!

JeevanJeevan
4 min read

Introduction:

Do you prefer using Docker Compose over Docker CLI commands? If you’re working with multi-container applications, you’ve likely needed a more structured and manageable way to run, link, and configure containers.

I prefer Docker Compose for exactly that reason. Docker Compose provides a declarative syntax with YAML files, making it easy to define and control multiple containers, their networks, volumes, and environment variables. Rather than using complex Docker commands, you can manage everything in a single, concise configuration file.

Let’s dive into why Docker Compose is more advantageous than Docker CLI commands, especially for applications with multiple services, and see some examples to illustrate these benefits.

Why Choose Docker Compose Over Docker CLI?

  1. Simplified Multi-Container Management: With Docker CLI, managing multiple containers requires chaining or running commands in sequence. Docker Compose allows you to define and manage all containers in one docker-compose.yml file.

  2. Declarative Configuration: Compose uses a YAML file to configure application services, which is more readable, maintainable, and version-controlled than a set of individual CLI commands.

  3. Environment Variable Management: Docker Compose lets you easily define environment variables in one place, making it ideal for applications that need different configurations in various environments.

  4. Service Dependencies: With Docker Compose, you can specify dependencies between services, so they start up in the correct order. Docker CLI doesn’t have this built-in.

  5. Reusable and Scalable: The Compose file can be reused across different environments without modification, making it ideal for development, staging, and production.

Example: Running a Multi-Container Application with Docker CLI

Suppose you want to run a web application with the following components:

  • Nginx as the web server

  • Redis for caching

  • PostgreSQL as the database

Here’s how you might accomplish this using Docker CLI, specifying volumes, environment variables, and networking manually:

# Create a custom network
docker network create my_app_network

# Run PostgreSQL container
docker run -d --name my_postgres \
  --network my_app_network \
  -v pgdata:/var/lib/postgresql/data \
  -e POSTGRES_DB=mydb \
  -e POSTGRES_USER=myuser \
  -e POSTGRES_PASSWORD=mypassword \
  postgres:13

# Run Redis container
docker run -d --name my_redis \
  --network my_app_network \
  redis:6.2

# Run Nginx container with port binding
docker run -d --name my_nginx \
  --network my_app_network \
  -p 8080:80 \
  -v nginxdata:/usr/share/nginx/html \
  -e REDIS_HOST=my_redis \
  -e POSTGRES_HOST=my_postgres \
  nginx:latest

Here, we:

  • Created a custom network.

  • Manually defined each container’s environment variables.

  • Specified volumes and network configuration.

  • Linked each container by explicitly assigning them to the network.

While this approach works, it’s not very maintainable for larger setups. Each command is separate, making it complex to update and restart containers if the configuration changes.

The Docker Compose Way: A Cleaner Approach

Now, let’s see how this setup could look with Docker Compose. With Compose, you define the entire configuration in a single docker-compose.yml file, which significantly simplifies your setup.

Here’s how you would define the same application stack in Docker Compose:

version: '3.8'

services:
  postgres:
    image: postgres:13
    container_name: my_postgres
    environment:
      POSTGRES_DB: mydb
      POSTGRES_USER: myuser
      POSTGRES_PASSWORD: mypassword
    volumes:
      - pgdata:/var/lib/postgresql/data
    networks:
      - my_app_network

  redis:
    image: redis:6.2
    container_name: my_redis
    networks:
      - my_app_network

  nginx:
    image: nginx:latest
    container_name: my_nginx
    ports:
      - "8080:80"
    environment:
      REDIS_HOST: my_redis
      POSTGRES_HOST: my_postgres
    volumes:
      - nginxdata:/usr/share/nginx/html
    networks:
      - my_app_network
    depends_on:
      - postgres
      - redis

volumes:
  pgdata:
  nginxdata:

networks:
  my_app_network:

Explanation of Key Features:

  • Declarative Syntax: With Docker Compose, all services, networks, and volumes are defined in a single file, making it more readable and maintainable.

  • Service Dependencies: The depends_on option ensures that Nginx only starts after Redis and PostgreSQL services are up.

  • Environment Variables and Volumes: Instead of separate CLI commands, environment variables, and volumes are configured directly within each service.

  • Network: Docker Compose automatically creates a custom network and assigns all services to it, reducing the need for manual networking commands.

How to Run the Docker Compose File

With Docker Compose, starting up all your services is as simple as:

docker-compose up -d

To stop all services, use:

docker-compose down

This command not only stops the containers but also removes networks and temporary resources, cleaning up the environment effortlessly.

Advantages of Docker Compose Over Docker CLI for Multi-Container Applications

FeatureDocker CLIDocker Compose
ConfigurationImperative commandsDeclarative YAML configuration
Multi-Container SetupRequires multiple commandsManaged in a single file
Service DependenciesNot directly supportedSupported with depends_on
Environment VariablesSet manually per containerDefined in YAML for each service
Scaling ServicesRequires manual commandsEasily managed with scale

Conclusion: Why Docker Compose is the Better Choice for Complex Applications

Using Docker Compose over Docker CLI for multi-container applications provides a cleaner, more maintainable, and scalable approach to managing complex stacks. With Compose, you can:

  • Easily manage dependencies between services.

  • Configure environment variables, volumes, and networking in a single file.

  • Scale services up or down with minimal effort.

Docker Compose offers a powerful solution for anyone working with Docker in development, testing, or production environments. By choosing Docker Compose, you’re opting for a simpler, more efficient, and easily reproducible setup, making it the preferred choice for multi-service applications.

Happy Learning 😊

0
Subscribe to my newsletter

Read articles from Jeevan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jeevan
Jeevan