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
If you want to pass multiple build arguments with docker build command you have to pass each argument with separate — build-arg. docker build -t <image-name>:<tag> --build-arg <key1>=<value1> --build-arg <key2>=<value2> .
Pass variables into Dockerfile through Docker Compose during build. If you want to pass variables through the docker-compose process into any of the Dockerfiles present within docker-compose. yml , use the --build-arg parameter for each argument to flow into all of the Dockerfiles.
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 up
to create and start both app
and db
container. The api should then be running on your docker daemon on port 3030. TEST=DO docker-compose up
to create and start both app
and db
container. The api should execute the npm run test
inside the package.json
file. $env:TEST="";docker-compose up
to create and start both app
and db
container. The api should then be running on your docker daemon on port 3030. $env:TEST="do";docker-compose up
to create and start both app
and db
container. The api should execute the npm run test
inside the package.json
file. You need to ensure 2 things:
docker-compose.yml
has the environment variable declared. For example,services: app: image: python3.7 environment: - "SECRET_KEY=${SECRET_KEY}"
docker-compose up
is 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With