As a developer, I have a million different projects and now they're all using docker with compose. I have a project structure like:
/flatscanner
/www
index.html
/api
index.php
/docker
docker-compose.yml
/konoro
/www
index.html
/api
index.php
/docker
docker-compose.yml
To bring up all the services for a service, I go to it's docker directory and up -d
, but the prefix is based on the folder, so is always docker_
and I'm getting conflicts as they use similar services: nginx
, mysql
, redis
etc.
In order to keep them unique, I need to create another folder inside /docker with the project name so the containers are prefixed with the project name instead:
/flatscanner
/www
/api
/docker
/flatscanner
docker-compose.yml
/konoro
/www
/api
/docker
/konoro
docker-compose.yml
I feel like the folder prefix should be configurable, so I can have a YML file like:
version: '2',
prefix: 'flatscanner'
services:
nginx
image: 'nginx'
Then I'll have containers like: flatscanner_nginx_1
and konoro_nginx_1
instead a conflicting service called docker_nginx_1
you can use -p or the COMPOSE_PROJECT_NAME environment variable for that.
Yes, you should use the project name.
Duplicate of #745
Or define it in your .env file in the docker folder
MY_PROJECT_NAME=flatscanner
/flatscanner
/www
index.html
/api
index.php
/docker
.env
docker-compose.yml
/konoro
/www
index.html
/api
index.php
/docker
.env
docker-compose.yml
There is another use case where the project name environment variable suggested above is not enough.
Suppose you have 2 compose files under the same folder, one for dev and another for test.
It is common to run dev and test at the same time, and with the environment variable solution set in .env, they will still have the same prefix. Always specifying -p or an inline environment variable is not so convenient. I believe a prefix option is best suited in this case.
What about having names without the same prefix ? We want to customize container/images names separately of a common prefix.
well you can always use name
to provide explicit name without prefix (but also without suffix id so it cannot be use for services ;)
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
you can use -p or the COMPOSE_PROJECT_NAME environment variable for that.
thanks for answer.
Most helpful comment
you can use -p or the COMPOSE_PROJECT_NAME environment variable for that.