问题
I am using the same docker-compose.yml file for multiple projects. I am really lazy so I don't want to start them with docker-compose -p $PROJECT_NAME up.
As of Docker version 17.06.0 is it possible to set the variable directly in the docker-compose.yml file.
回答1:
Right now we can use .env file with custom project name and set
COMPOSE_PROJECT_NAME=SOMEPROJECTNAME
It's not flexible but it's better than nothing. Currently there is an open issue regarding this as a proposal.
回答2:
I know this question was asked a long time ago, but I ran into the same problem. There's a suggestion to add the feature https://github.com/docker/compose/issues/745, but they don't want to.
However, I have a Makefile in the root of the directory and then you can add something like in the Makefile:
.PHONY: container-name
container-name:
docker-compose -p $PROJECT_NAME up -d container-name
and then run make container-name
I know it isn't what you asked for, but could maybe make your life a bit easier.
回答3:
You can add them as your environment variables which are available through the session in which you are bringing up your containers using the docker compose.
Ie, if you wanted to use $PROJECT_NAME somewhere inside your docker-compose.yaml then if this variable has a value in your session, then it would be picked up.
Inside the yaml you can assign it to anything as you want it. You want as a commandline arg to some script, even that is also possible. ie,
working_dir: /opt
command: /bin/bash -c './script.sh ${PROJECT_NAME}'
volumes:
- /var/run/:/host/var/run/
I'm using docker version : Docker version 17.09.0-ce, build afdb6d4 docker-compose version : docker-compose version 1.14.0, build c7bdf9e
来源:https://stackoverflow.com/questions/44924082/set-project-name-in-docker-compose-file