How do i pass an argument along with docker-compose up

丶灬走出姿态 提交于 2019-12-09 05:21:57

问题


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



回答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:

  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.



来源:https://stackoverflow.com/questions/35093256/how-do-i-pass-an-argument-along-with-docker-compose-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!