Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass an argument along with docker-compose up?

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

like image 540
BubbleTea Avatar asked Jan 29 '16 20:01

BubbleTea


People also ask

How do you pass ARG docker?

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> .

How do I pass an environment variable from docker compose to Dockerfile?

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.


2 Answers

Based on dnephin answer, I created this sample repo that you can pass an variable to docker-compose up.

The usage is simple:

MAC / LINUX
  • 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.
  • 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.
WINDOWS (Powershell)
  • $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.
like image 56
Rafael Delboni Avatar answered Oct 03 '22 07:10

Rafael Delboni


You need to ensure 2 things:

  1. The docker-compose.yml has the environment variable declared. For example,
services:     app:         image: python3.7         environment:             - "SECRET_KEY=${SECRET_KEY}" 
  1. have the variable available in the environment when 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.

like image 33
Jorge Leitao Avatar answered Oct 03 '22 07:10

Jorge Leitao