如何将变量传递给docker-compose和dockerfile

时间:2019-01-12 15:05:41

标签: docker docker-compose

我正在使用docker-compose:

version: "3"

services:
   app:
        build:
            context: .
            dockerfile: Dockerfile

和dockerfile:

FROM microsoft/dotnet:2.2-sdk

WORKDIR /app

# copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# copy and build everything else
COPY . ./
RUN dotnet publish -c Release -o out

ENTRYPOINT ["dotnet", "out/testapp.dll"]

我的问题是,如何使用docker-compose up命令将参数传递给docker-compose和dockerfile?

解决方案:

我使用此代码“ variable = value docker-compose up”将变量发送到docker-compose,并将此代码添加到docker-compose文件中

  environment:
        - variable=${variable}

并在Dockerfile中以这种格式使用入口点:

ENTRYPOINT dotnet "testapp.dll" "$variable"

1 个答案:

答案 0 :(得分:0)

您似乎想使用environment variables。声明一个环境变量(例如这里的DLL):

version: "3"

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    environment:
      - DLL=out/testapp.dll

然后在Dockerfile中将其用作环境变量(最后一行):

FROM microsoft/dotnet:2.2-sdk

WORKDIR /app

# copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# copy and build everything else
COPY . ./
RUN dotnet publish -c Release -o out

ENTRYPOINT dotnet "$DLL"