DevOps ~ Docker Compose - rohit120582sharma/Documentation GitHub Wiki

If your Docker application includes more than one container (for example, a webserver and database running in separate containers). So building, running, and connecting the containers from separate Dockerfiles is cumbersome and time-consuming. Docker Compose solves this problem by allowing you to use a YAML file to define multi-container apps. You can configure as many containers as you want, how they should be built and connected, and where data should be stored. When the YAML file is complete, you can run a single command to build, run, and configure all of the containers.

Docker Compose is used to start up multiple Docker containers at the same time and automatically connect them together with some form of networking.

Each entry represents a container we want to create and can optionally build an image, can define the networking requirements (ports), can set volumes for persistent data-storage.

Doctor compose command-line and docker compose the .yml file is the combination which is used to start the multiple containers as a single service. It is a separate CLI tool that gets installed along with Docker and it automates some of the long-winded arguments we were passing to docker run command.

Docker Compose is a three step process:

  • 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 file so they can be run together in an isolated environment.
  • Run docker-compose up and Compose starts and runs your entire app.

Restart policies:

  • 'no': never attempt to restart the container if it stops or crashes
  • always: if container stops for any reason. always attempt to restart it
  • on-failure: only restart if the container stops with an error code
  • unless-stopped: always restart unless we (the developers) forcibly stop it


Compose File Syntax

A docker-compose.yml file is organized into four sections:

  • version

    • Specifies the Compose file syntax version. This guide will use Version 3 throughout.
  • services

    • A service is the name for a Container. This section defines the containers that will be started as a part of the Docker Compose instance.
  • networks

    • This section is used to configure networking for your application. You can change the settings of the default network, connect to an external network, or define app-specific networks.
  • volumes

    • Mounts a linked path on the host machine that can be used by the container.

Most of this guide will focus on setting up containers using the services section. Here are some of the common directives used to set up and configure containers:

  • image

    • Sets the image that will be used to build the container. Using this directive assumes that the specified image already exists either on the host or on Docker Hub.
  • build

    • This directive can be used instead of image. Specifies the location of the Dockerfile and context that will be used to build this container.
  • restart

    • Tells the container to restart if the system restarts.
  • volumes

    • Mounts a linked path on the host machine that can be used by the container.
  • environment

    • Define environment variables to be passed in to the Docker run command.
  • depends_on

    • Sets a service as a dependency for the current block-defined container.
  • port

    • Maps a port from the container to the host in the following manner: host:container.
  • links

    • Link this service to any other services in the Docker Compose file by specifying their names here. It creates a dependency network between the containers.
⚠️ **GitHub.com Fallback** ⚠️