簡體   English   中英

將參數從Shell傳遞到docker-compose / docker

[英]Pass parameter from Shell to docker-compose / docker

我正在嘗試使用docker-compose文件從Shell腳本構建docker,並希望將憑據作為參數傳遞。 (該憑證將在Dockerfile中使用)。 例如,在外殼文件中,$ {1}和$ {2}分別是用戶名和密碼。

在Dockerfile中,如果下面的命令顯示“ username”,那么我會很高興。 下面的示例簡單起見,$ {1} =用戶名

RUN echo ${test}

在Shell腳本中,由於應使用docker-compose.yml,因此需要使用以下命令。 我不想將憑據存儲在yml文件中,想通過shell腳本傳遞。

docker-compose -f docker-compose-el6.yml build --force-rm --pull
docker-compose -f docker-compose-el6.yml up -d --force-recreate
docker-compose -f docker-compose-el6.yml run -T --rm builder-el6 testmain.py

我在下面嘗試過,但是沒有運氣。 空白。
1)添加build-arg

docker-compose -f docker-compose-el6.yml build --build-arg test=username --force-rm --pull

得到以下錯誤

Services are built once and then tagged as `project_service`,
e.g. `composetest_db`. If you change a service's `Dockerfile` or the
contents of its build directory, you can run `docker-compose build` to rebuild it.

Usage: build [options] [SERVICE...]

Options:
    --force-rm  Always remove intermediate containers.
    --no-cache  Do not use cache when building the image.
    --pull      Always attempt to pull a newer version of the image.

2)添加環境變量

$SUDO test=username docker-compose -f docker-compose-el6.yml up -d --force-recreate

3)添加環境變量-另一種方法

export test=username
$SUDO docker-compose -f docker-compose-el6.yml up -d --force-recreate

4)添加環境變量-另一種方法

$SUDO docker-compose -f docker-compose-el6.yml run -T -e test=username --rm builder-el6 testmain.py

有關信息,這是Docker版本信息。

Client version: 1.7.1
Client API version: 1.19
Go version (client): go1.4.2
Git commit (client): 786b29d
OS/Arch (client): linux/amd64
Server version: 1.7.1
Server API version: 1.19
Go version (server): go1.4.2
Git commit (server): 786b29d
OS/Arch (server): linux/amd64
  1. 添加build-arg

    --build-arg選項使用一個鍵和一個值。 該命令在https://docs.docker.com/compose/reference/build/中進行了說明。 正確的命令:

     docker-compose -f docker-compose-el6.yml build --build-arg myArgument=myValue 

    要在shell上打印參數,請在Dockerfile中添加2行

     ARG myArgument RUN echo $myValue 

    Dockerfile參考說明在這里https://docs.docker.com/engine/reference/builder/#arg

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM