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
You need to pass the variables as environment variables:
or
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:
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.
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 bothapp
anddb
container. The api should then be running on your docker daemon on port 3030.TEST=DO docker-compose up
to create and start bothapp
anddb
container. The api should execute thenpm run test
inside thepackage.json
file.$env:TEST="";docker-compose up
to create and start bothapp
anddb
container. The api should then be running on your docker daemon on port 3030.$env:TEST="do";docker-compose up
to create and start bothapp
anddb
container. The api should execute thenpm run test
inside thepackage.json
file.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.
Not sure if it helps in this case but you can specify a command in your docker-compose.yml script; you could possibly provide a different command for different service names, e.g.
perform
docker-compose up -d
to start the container, and attach to the "service" using:When dealing with build argumenets please declare them in compose yml file as follows
Well before running docker-compose up, export variable as other guys suggested. It will work. I tried. Use docker compose version 3 and above. Have fun