Docker Compose, what is it?

Quick explanation of Docker Compose for beginners. Why use it? How does it work? Where to start?

Find more tutorials explaining the basics of a subject in ten minutes with the tag what is it.

Go read Docker, what is it? for a quick reminder.

Docker Compose is a tool orchestrating the creation and the execution of Docker containers. It is especially useful for managing multi-container applications, describing the links between them and with the host.

In order to describe the infrastructure, Docker Compose uses a YAML file. By default this file is named docker-compose.yml. It contains instructions, using docker vocabulary, strongly improving the basic docker commands.

Example of docker-compose.yml

version: '3' # Create a virtual network named tuto4dev. # The services of this network can communicate with each other # by just using their name. networks: tuto4dev: services: tuto4dev-front: build: tuto4dev-nodejs # The basic image for this service. user: "node" # Avoid being root for more security. tty: true # Allocating a tty avoids the container to end. environment: # These variables are available in the container. - EXECUTION_ENVIRONMENT=development - PORT=4242 - HOST=0.0.0.0 ports: # The host accesses the port 4242 # of the container using its port 443. - "443:4242" volumes: # Mount a volume for sharing a folder between the host and the container. - /path/on/host:/path/in/container:delegated networks: - tuto4dev depends_on: # The front can only exist if the API exists. - tuto4dev-api tuto4dev-api: build: tuto4dev-nodejs # The basic image for this service. user: "node" # Avoid being root for more security. tty: true # Allocating a tty avoids the container to end. environment: # These variables are available in the container. - EXECUTION_ENVIRONMENT=development - PORT=1337 - HOST=0.0.0.0 expose: # Only the members of the network can contact an exposed port # even the host cannot access it. - "1337" volumes: # Mount a volume for sharing a folder between the host and the container. - /path/on/host:/path/in/container:ro networks: - tuto4dev

Run the composition

docker-compose up

Docker Compose can use environment variables directly in the docker-compose.yml file. Note that it actually might be a bad idea to use this feature: you would not be able to run any docker-compose command without initializing the variables. Consider your options before doing it.

Example of docker-compose.yml with environment variables

version: '3' services: tuto4dev-front: build: tuto4dev-nodejs volumes: - ${T4D_HOST_FRONT_PATH}:${T4D_CONTAINER_FRONT_PATH}:ro

While Docker Compose supports a large majority of the Dockerfile's instructions, be sure to keep independent the building of your Docker image. Docker Compose must only be used for creating the internal and external links: virtual networks, ports exposition, volume mounting, environment transmission... It is important to not give too much responsibilities to Docker Compose in case you want to use another tool for orchestrating your containers: like Kubernetes for instance.

Learn more about Docker Compose in the official documentation.

Have a nice day :)