Two docker-compose .yml in the same network with COMPOSE_PROJECT_NAME

I am trying to have my own network name on debian:jessie for my docker-compose files (server.yml and test.yml), as test.yml gets only started from time to time, but needs access to some services in the server.yml. I can make it work with docker-compose -p nameofproject up, but not with COMPOSE_PROJECT_NAME.

server.yml

version: '2'

networks:
  mynetwork:
    driver: bridge

services:
  app1:
    networks:
      - mynetwork
    environment:
      POSTGRES_PASSWORD: somepassword
      COMPOSE_PROJECT_NAME: serverstack

  app2:
    networks:
      - mynetwork
    environment:
      COMPOSE_PROJECT_NAME: serverstack
    depends_on:
      - app1

My expectation is that when the container is starting I should see

Creating serverstackmynetwork_app_1
Creating serverstackmynetwork_app_2

the network should be named (docker network ls)

serverstack_mynetwork
just like when I do the following, which actually works

docker-compose -p serverstack up
And then I can connect just by using docker-compose up with the second file (which works just fine when using the -p option on the server.yml)

testing.yml

version: '2'

networks:
  testapp_network:
    external:
      name: serverstack_mynetwork

services:
  testapp:
    networks:
      - testapp_network

But using it without -p serverstack on the server.yml I see directories as names

Creating directoryofapp1_app1_1
Creating directoryofapp2_app2_1

so COMPOSE_PROJECT_NAME is being omitted and I also cannot connect to the server service though serverstack_mynetwork

As far as I know, you cannot set the project name directly inside the compose file (see Proposal: make project-name persistent. · Issue #745 · docker/compose · GitHub)
You have to use the -p flag or set the COMPOSE_PROJECT_NAME environment variable before running docker-compose either by exporting the variable or by setting it in a .env file.

For ex:

  • .env

COMPOSE_PROJECT_NAME=serverstack

  • test.yml

networks:
testapp_network:
external:
name: ${COMPOSE_PROJECT_NAME}_mynetwork

I am fine with the .env file version if that works. I just seem to be doing something wrong.

server.yml

app1:
  env_file:
    - server.env
app2:
   env_file:
    - server.env

and delete the environment: part

server.env

COMPOSE_PROJECT_NAME=serverstack

but it continues starting as

Creating directoryofapp1_app1_1
Creating directoryofapp2_app2_1

and docker network ls confirms that the network is still called directory_mynetwork

I did the same mistake: the file should be strictly called .env, not [something].env.

tree -a
.
├── .env
├── server.yml
└── test.yml

Thanks.

Strange that the doc would then mention file names…???

Is the COMPOSE_PROJECT_NAME=serverstack in an .env treated as a system variable? In other words, if I have two different .env files, in different folders, for 2 different projects and each defines the variable, does the call of the 2nd project also override the 1st?

  1. env_file provides environment variables to a container, but are not available to the Compose file. That’s why ${COMPOSE_PROJECT_NAME}-mynetwork won’t work while using env_file directive.

  2. On the contrary, .env provides environment variables for use inside the Compose file.

$ cat .env
TAG=v1.5

$ cat docker-compose.yml
version: ‘2.0’
services:
web:
image: “webapp:${TAG}”

It is not treated as a system variable, you can have the same variable with different values in two separate folders without any issue :slight_smile:

I am potentially confused :slight_smile:

so what you are saying ist that for COMPOSE_PROJECT_NAME I need to use .env file, so that the Compose file sees it, but I do not need to declare env_file: .env in the docker-compose.yml (tested and confirmed). That also seems to suggest that I can still use asecond.env with other variables and a name (this is just not seen by the Compose file)

It starts to make sense. So I changed to not have env_file: .env (I was wondering why making it redundant, if it can be only named .env :blush: )

But when I start the test.yml (with serverstack_mynetwork) I see the following

Found orphan containers (serverstack_app1_1, serverstack_app2_1) for this project. What does that mean?