Docker compose

svg viewer

What is Docker compose?

Docker compose orchestrates an environment to run multi-container Docker applications. In layman’s language, it enables you to bring multiple containers together to make an application.

Docker compose makes use of a yaml file to define services that are later used to start and run the application with a single command.

Initially, you will have to make the respective Dockerfiles and set up the environment according to the application’s need. Then, download all the dependencies and install all the packages.

A typical docker-compose.yml file looks like this:

services:
  web:
    build: .
    ports:
    - "5000:5000"
    volumes:
    - .:/code
    - logvolume01:/var/log
    links:
    - redis
  redis:
    image: redis
volumes:
  logvolume01: {}
  • There are two services running, web and redisan in-memory data structure store used as a database, cache, and message broker.

web

  • build from Dockerfile in the parent directory.
  • Mapped both internal and external port to 50005000.
  • Parent directory (.) is mapped to /code, and dictionary (logvolume01) is mapped to /var/log in the container.
  • links to the redis container.

redis

  • Builds redis service using the redis image.

After the yaml file is set up, run:

docker-compose up

This will start up the services specified in the yaml files.

Summary

  • Define your app’s environment with a Dockerfile so it can be reproduced anywhere.
  • Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment.
  • Run docker-compose up and Compose will run your entire app.
Copyright ©2024 Educative, Inc. All rights reserved