16

Can we override our project and network name inside docker-compose.yml, i.e. without using flag or environment variable?

2
  • What is your understanding of "project name"? The name of the image? Nov 18, 2017 at 21:25
  • No, currently it is the directory name and can be override by COMPOSE_PROJECT_NAME, the first option is too rigid as directory name might change, the other affects all projects
    – ntviet18
    Nov 19, 2017 at 12:47

2 Answers 2

13

This can be done in with a parameter of the docker-compose call:

$> docker-compose -p THISISMYPROJECT_AND_NWK_NAME up -d

Unfortunately there is no way to persist it at the moment. (see: https://github.com/docker/compose/issues/745)

That's why I personally prefer adding a bash alias for my projects in ~/.bash_aliases (Debian based Linux) for example:

# project PROJECT shortcuts
alias dc_PROJECT_up='docker-compose -p PROJECT up -d'
alias dc_PROJECT_down='docker-compose -p PROJECT down'

# general docker compose shortcuts
alias dc='docker-compose '
alias dc_up='docker-compose up -d'
alias dc_down='docker-compose down'

So I can call dc_PROJECT_up to start my project with a project name PROJECT. The network name is PROJECT_default then.

Additionally you can setup additional networks with custom names in the docker-composer.yml like this (v. 2.1):

version: '2.1'

...

networks:
  mynwk:
    driver: bridge
    name: mynwk

 ...

You can check this with the following command:

 $> docker network ls

You should get a list of networks including yours and the default one.

1
  • 1
    It seems the name parameter does not work anymore with docker compose version 3 Aug 19, 2018 at 5:43
14

you can create an .env file with key=value options such as

COMPOSE_PROJECT_NAME={project name}

you should note that this will only work when the .env file is in the current path

3
  • I have multiple compose overrides for prod/staging/dev - is there a way to define a .env for each?
    – rnoodle
    Jul 9, 2020 at 22:39
  • 1
    it might makes sense to use a separate folder for each environment
    – sinaiy
    Jul 12, 2020 at 11:00
  • 1
    It can get clunky with folders. For example if you use different versions of your app. You use same repo, so it has same name, but container name should be different.
    – Andrius
    Feb 6, 2022 at 13:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .