问题
I have a docker-compose.yml file and in the terminal I am typing docker-compose up [something] but I would also like to pass an argument to docker-compose.yml. Is this possible? I've read about interpolation variables and tried to specify a variable in the .yml file using ${testval} and then docker-compose up [something] var="test" but I receive the following error:
WARNING: The testval variable is not set. Defaulting to a blank string.
ERROR: No such service:testval=test
回答1:
Based on dnephin answer, I created this sample repo that you can pass an variable to docker-compose up.
The usage is simple:
MAC / LINUXTEST= docker-compose upto create and start bothappanddbcontainer. The api should then be running on your docker daemon on port 3030.TEST=DO docker-compose upto create and start bothappanddbcontainer. The api should execute thenpm run testinside thepackage.jsonfile.
$env:TEST="";docker-compose upto create and start bothappanddbcontainer. The api should then be running on your docker daemon on port 3030.$env:TEST="do";docker-compose upto create and start bothappanddbcontainer. The api should execute thenpm run testinside thepackage.jsonfile.
回答2:
You need to pass the variables as environment variables:
testvar=test docker-compose up ...
or
export testvar=test
docker-compose up
回答3:
I'm not sure what you want to do here, but if what you need is to pass an environmental variable to a specific container docker-compose.yml allows you to do that:
web:
...
environment:
- RAILS_ENV=production
- VIRTUAL_HOST=www.example.com
- VIRTUAL_PORT=3011
This variables will be specific for the container you specified them to, and wil not be shared between containers.
Also "docker-compose up" doesn't take any argument.
回答4:
docker-compose don't include any flag or command in order to pass arguments.
Compose is primarily a tool for running services, not building images.
Try to use the ARG defined in every dockerfile for arguments.
It's not going to be possible to support every unique build scenario because docker-compose is not designed for that purpose. you can only really support the most common build scenarios.
You can see more information about his topic here.
回答5:
You need to ensure 2 things:
- The
docker-compose.ymlhas the environment variable declared. For example,
services:
app:
image: python3.7
environment:
- "SECRET_KEY=${SECRET_KEY}"
- have the variable available in the environment when
docker-compose upis called:
SECRET_KEY="not a secret" docker-compose up
Note that this is not equivalent to pass them during build, as it is not advisable to store secrets in docker images.
来源:https://stackoverflow.com/questions/35093256/how-do-i-pass-an-argument-along-with-docker-compose-up