9

When I run docker-compose up -d, docker always creates container name and network name prepended with the folder name that contains docker-compose.ymlfile.

I can specify the container name as follows:

nginx:
    container_name: nginx
    build:
        context: .
        dockerfile: .docker/docker-nginx.dockerfile

But how can I specify the network name so that it doesn't prepend folder name to it?

Thanks.

Rashidul Islam
  • 1,623
  • 3
  • 18
  • 24

2 Answers2

16

Docker Prepends the current folder name with all the components name created using docker compose file.

Eg : If the current folder name containing the docker-compose.yml file is test, all the volumes,network and container names will get test appended to it. In order to solve the problem people earlier proposed the idea of using -p flag with docker-compose command but the solution is not the most feasible one as a project name is required just after the -p attribute. The project name then gets appended to all the components created using docker compose file.

The Solution to the above problem is using the name property as in below.

volumes: 
  data:
    driver: local
    name: mongodata

networks: 
  internal-network:
    driver: bridge
    name: frontend-network

This volume can be referred in the service section as

services:
  mongo-database:
      volumes: 
        - data:/data/db
      networks: 
        - internal-network

The above name attribute will prevent docker-compose to prepend folder name.

Note : For the container name one could use the property container_name

services:
  mongo-database:
    container_name: mongo
Shubhanshu Rastogi
  • 2,133
  • 1
  • 20
  • 30
  • Extremely useful reference, never knew about the network `name` attribute. I have used `container_name` before but had to come here to refresh my mind on using it. Would it be possible to update the example with `-p flag` , I do not see any reference on `docker-compose up --help`, and as far as I know `-p` for `docker run` is for ports? – CvRChameleon Aug 05 '20 at 08:43
  • Yes are correct -p is for ports and that could be done in docker compose using ports: - '30050:8161' Be careful with the alignment. You can use vs code with docker-compose extension to get this done easily – Shubhanshu Rastogi Aug 05 '20 at 12:10
  • The point was that your answer alluded to `-p` and its usage, it sort of suggested that you could use it to create `--project-name`, which are two different concepts. Just wanted to try and clear that up for future readers (and me). – CvRChameleon Aug 05 '20 at 12:38
9

You have to place a ".env" file in the root of your docker-compose project directory.

COMPOSE_PROJECT_NAME=MyFancyProject

See Docker docs for further information: https://docs.docker.com/compose/reference/envvars/

If you don't use docker-compose, you can use the "-p" parameter to set this on docker run.

opHASnoNAME
  • 20,224
  • 26
  • 98
  • 143
  • then docker will prepend the project name instead of folder name, right? – Rashidul Islam Mar 08 '17 at 09:43
  • For the record, the `-p` argument is also supported by `docker-compose` command line. – Arnaud P Nov 07 '18 at 11:36
  • 6
    Why can't I just specify the name in the compose file? This makes no sense. The purpose of the compose file is to describe the project right? The name of the project isn't a variable, and other things may depend on it being a static value so that it can be referenced. – theferrit32 Oct 29 '19 at 21:52
  • The project name now can also be set by the top level `name` property. Check [this answer](https://stackoverflow.com/a/44924392/368613). – imy Jan 20 '23 at 15:44