Is there a way to define the namespace your Compose Swarm lives in, in the yaml?

Viewed 433

I have a simple docker-compose file which is used to launch my containers. I wish to have another yaml file which contains additional, optional containers. It can live in a separate directory. My goal is to find a way to force the namespace of the created swarms so they exist within the same network/use space so they can talk to each other.

compose1.yaml
  services:
    web:
      build: .

compose2.yaml
  services:
    web1:
      build: .

So if i run both of these they would be prepended with the folder they exist in, in my case: a, and b respectively.

I wanted to ensure that they flow together, despite not being in the same file hierarchy.

I have been coming over keywords in the docker-compse documents, and was not sure what the best way to do this in the yaml file would be, but noticed in the CLI, might be able up to update various names.

How does one accomplish this?

Note: I have also created a third file under the b directory, a sibling to compose2.yaml. So i can run those separately and they work just fine.

a/
  compose.yaml
b/
  compose2.yaml
  another.yaml

So i have been able to merge them together by doing: cd /b/ && docker-compose -f compose2.yaml -f another.yaml up -d to run 2 files together, and they exist under the B namespace. Likewise, I can also run them sequentially instead of referencing them in 1 command.

So my question is how can I do something like:

docker-compose --namespace test compose.yaml up
docker-compose --namespace test compose2.yaml up

such that I could view items accordingly with docker? It seems that I would need to consider running the command from under the first shared parent folder?

so if a and b existed under test, I could just do:

cd /test
docker-compose -f a/compose.yaml up -d
docker-compose -f b/compose2.yaml up -d

then my services would be listed as: test_web, test_db-box, etc.

1 Answers

So I found out that one person's namespace is another person's project-name.

That being said, after understanding nuances, the project-name ( -p | --project-name ) is the prepend for the compose services.

docker-compose --project-name foo -f a/compose.yaml up
docker-compose --project-name foo -f b/compose2.yaml up

This will create the services: foo_web_1

The format for this is: %{prepend}%{servicename}%{number}

The issue then is, Can we find a way to implement this CLI property to work from within the YAML file, possibly as a config option for the file. The Docker Compose website information states that you can supply an environment variable ( _ COMPOSE_PROJECT_NAME_ ) to change change the project name from the default of the base directory, BUT not from within a Compose YAML.

If i want to then launch multiple compose files, under a particular project what I would want to do is to just encapsulate it with a BASH or SHELL script.

#/bin/bash
export COMPOSE_PROJECT_NAME=ultimate-project
docker-compose -f a/compose.yaml up -d
docker-compose -f b/compose2.yaml up -d

and that would create services :

ultimate-project_web_1
ultimate-project_web2_1
Related