How do i pass an argument along with docker-compos

2020-05-25 06:14发布

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

7条回答
地球回转人心会变
2楼-- · 2020-05-25 06:33

You need to pass the variables as environment variables:

testvar=test docker-compose up ...

or

export testvar=test
docker-compose up
查看更多
SAY GOODBYE
3楼-- · 2020-05-25 06:33

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楼-- · 2020-05-25 06:44

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.
查看更多
叛逆
5楼-- · 2020-05-25 06:46

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.

查看更多
太酷不给撩
6楼-- · 2020-05-25 06:47

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.

version: '3'
services:
  my_service_1:
    command: bash 
    container_name: <cont-name>
    image: <img id>

perform docker-compose up -d to start the container, and attach to the "service" using:

docker-compose exec -e "TERM=xterm-256color" my_service_1 bash
查看更多
贼婆χ
7楼-- · 2020-05-25 06:53

When dealing with build argumenets please declare them in compose yml file as follows

services:
  app: (name of service
    build:
      context: docker/app/ (where is your docker build root)
      dockerfile: Dockerfile (that is optional)
      args:
        - COMPOSER_AUTH_TOKEN (name of variable, value will be taken from host environment)

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

查看更多
登录 后发表回答