Sitemap
Fsegredo

Fsegredo

DevOps || Terraform || Ansible || Docker || Kubernetes || .Net || CNCF

Docker compose — Include

4 min readSep 5, 2023
Press enter or click to view image in full size

In Docker Compose 2.20 (available on docker desktop v.4.22), there’s a new include attribute that’s quite useful. Let’s begin.

Essentially, it is now possible to explicitly declare dependencies to other composes by incorporating the flag include and specifying the location of the other composes. We will see some examples, but let’s start with the advantages.

  • Possibility to reuse composes easily.
  • If your compose gets too big or complex to read and manage, you can use include as a way to refractor.
  • Separation of concerns, for example different services handled by different teams.

Let’s say I have two APIs and a Database, but each should have its own separate structure, but the deployment should be together.

One may immediately consider the subsequent command line, which is valid if all the composes follow a consistent path and are simple. However, if one of the composes employs relatives paths, it may not work as aspected.

docker compose -f database-compose.yaml -f serviceA-compose.yaml -f ...

Given the structure, let’s see an example of this approach failing.



docker-playground/
├─ Apps/
│ ├─ service/
│ │ ├─ Dockerfile
│ │ ├─ docker-compose.yaml
│ │ ├─ index.html
│ │ ├─ package.json
│ │ ├─ .env
│ ├─ webserver/
│ │ ├─ docker-compose.yaml
# ../service/docker-compose.yaml
services:
api:
build: .
env_file:
- ./.env
# docker-compose.yaml
services:
webapp:
image: httpd
depends_on:
- api
docker compose -f docker-compose.yaml -f ../service/docker-compose.yaml up -d

Doing compose up from the webserver directory will fail because it assumes the webserver as the base folder for all the composes loaded, so it will not be able, for example, to find the Dockerfile of /service/docker-compose.yaml and all the other files to build the image successfully.

Press enter or click to view image in full size
Error location components to build service api
Press enter or click to view image in full size
Erro locating service api components

In addition to the advantages listed above, the include attribute also aids in resolving this issue.

“How can we use the atribute?”

According to the compose specification:

"include": {
"type": "array",
"items": {
"type": "object",
"$ref": "#/definitions/include"
},
"description": "compose sub-projects to be included."
},

The simpliest format (Short Syntax):

include:
- extra-compose.yaml
services:
...

Since is a list of items, you can load mode than one compose

include:
- ../shared/compose-share.yaml
- ../extra/compose-extra.yaml
services:
...

Each path listed in the include section is loaded as an individual Compose application model, with it's own project directory, in order to resolve relative paths. — docs.docker.com

If you would like to have more control over your parsing, you can use the Long Syntax, which will allow you to define the project directory and associated environments.

include:
- path: ../shared/compose-share.yaml
env_file: ../vars/.env

services:
..

In Action:

Get Fsegredo’s stories in your inbox

Join Medium for free to get updates from this writer.

Using the same directory structure as before, we will use the include atribute instead of loading multiple composes with the flag -f.

Now let’s change the main compose webapp and add an include to load the service api from another compose file.

# BEFORE
services:
webapp:
image: httpd
depends_on:
- api
# AFTER
include:
- path: ../service/docker-compose.yaml
env_file: ../webserver/.env
services:
webapp:
image: httpd
depends_on:
- api # api service is declared the include compose

Subsequently, we only need to execute the standard docker compose up -d, and no more -f required.

We can see that the include compose was successfully loaded.

Also with the env_file you can override values for interpolation variables defined on the include composes.

Lets take the api service for example.

services:
api:
build: .
ports:
- ${API_PORT}:8080
API_PORT = "8888"

If we were to execute directly, the container would be published to the port 8888. However, as we have defined the env_file in the webserver directory, it will be overridden.

env_file: ../webserver/.env
API_PORT = "7070"

It will actually be publish on port 7070

Press enter or click to view image in full size

and that’s it, a simple overview for the atribute include.

Few pointers:

  • The composes defined in the include are loaded recursively, which means your composes in the include section can also have includes.

example:

#commons/compose.yaml
services:
apibase:
build: .
ports:
- ${API_BASE}:1234
#../service/docker-compose.yaml
include:
- ../commons/compose.yaml

services:
api:
build: .
ports:
- ${API_PORT}:8080
depends_on:
- apibase # api service is declared the include commons compose
../webserver/docker-compose.yaml
include:
- path: ../service/docker-compose.yaml
env_file: ../webserver/.env
services:
webapp:
image: httpd
depends_on:
- api # api service is declared the include compose
  • All the composes defined are merged together, you can verify by using the docker compose config it will give you the final compose , that you can use to debug before actually creating your containers:
  • As we mentioned earlier, every include compose uses its parent folder as the base folder, avoiding issues with relative paths.

If this post was helpful, please click the clap 👏 button below a few times👇

--

--

Fsegredo
Fsegredo

Written by Fsegredo

DevOps || Terraform || Ansible || Docker || Kubernetes || .Net || CNCF