Docker Basics (Part 4) : Docker Compose

Docker Compose, a powerful tool in the Docker ecosystem, simplifies the deployment and management of multi-container applications. By defining your application stack in a single docker-compose.yml file, you can orchestrate containers effortlessly. Let's explore the key features and a practical example.

Full Docker Series : View Docker series

Anatomy of docker-compose.yml

Consider the following example of a docker-compose.yml file:

version: '3'
    image: nginx:latest
      - "80:80"
    image: postgres:latest
      POSTGRES_PASSWORD: mypassword122

In this example:

  • version: '3' specifies the version of Docker Compose.

  • services define individual containers (web and db in this case).

  • image specifies the Docker image to use for each service.

  • ports expose ports from the container to the host.

Docker Compose Commands

Run Container Definitions

docker-compose up

This command deploys the containers defined in the docker-compose.yml file. It creates the specified services, networks, and volumes, bringing your application stack to life.

Stop and Remove Containers

docker-compose down

Use this command to stop and remove the containers defined in the docker-compose.yml file. It cleans up the resources created during the docker-compose up process.

Key Features of Docker Compose

  1. Service Definition: Define services, networks, and volumes in a declarative manner, where each service corresponds to a container.

  2. Orchestration: Docker Compose allows you to define relationships and dependencies between services, orchestrating their interaction.

  3. Environment Variables: Parameterize your configuration using environment variables in the docker-compose.yml file, enabling versatility across different environments.

  4. Scaling: Easily scale services by adjusting the number of containers for a particular service with a single command.

  5. Container Networking: Docker Compose automatically creates a default network for your application, facilitating communication between containers using service names.

  6. Volume Management: Define volumes in the docker-compose.yml file to persist data outside of container instances.

Practical Example: Go Application with Docker Compose

  1. Create a new folder and navigate into it.

  2. Craft a main.go file for your Go application.

  3. Create a Dockerfile to build the Go application.

  4. Formulate a docker-compose.yml file:

version: '3'
      context: .
      dockerfile: Dockerfile
      - "8080:8080"
  1. Run the following commands:
docker-compose up --build

This example demonstrates Docker Compose orchestrating the build and deployment of a Go application with ease.

Embrace the power of Docker Compose for seamless management of your multi-container applications. From defining services to managing dependencies, Docker Compose streamlines the deployment process, empowering you to build robust and scalable containerized solutions.

Did you find this article valuable?

Support Aswin Benny by becoming a sponsor. Any amount is appreciated!