我正在为一个容器创建一个Dockerfile,该容器将包含一个基于ASP.NET Core 2.2的应用程序。
要通过.NET配置系统利用环境变量,我必须使用某种格式,但这会使环境变量非常难看。
FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY src/ ./
RUN dotnet publish App/App.csproj -c Release -o /out
FROM microsoft/dotnet:2.2-runtime AS runtime
WORKDIR /app
COPY --from=build /out ./
ENV NYBUS_ENVIRONMENT=Production
ENV NYBUS_LOGGLY__APIKEY=loggly \
NYBUS_Nybus__RabbitMq__Connection__Hostname=host.docker.internal \
NYBUS_Nybus__RabbitMq__Connection__Username=guest \
NYBUS_Nybus__RabbitMq__Connection__Password=guest \
NYBUS_Nybus__RabbitMq__UnackedMessageCountLimit=
ENTRYPOINT [ "dotnet", "App.dll" ]
我试图使用参数,但是它们似乎不起作用
FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY src/ ./
RUN dotnet publish App/App.csproj -c Release -o /out
FROM microsoft/dotnet:2.2-runtime AS runtime
WORKDIR /app
COPY --from=build /out ./
ENV NYBUS_ENVIRONMENT=Production
ENV LOGGLY_APIKEY=loggly \
RABBITMQ_HOSTNAME=host.docker.internal \
RABBITMQ_USERNAME=guest \
RABBITMQ_PASSWORD=guest \
RABBITMQ_MESSAGE_LIMIT=
ENV NYBUS_LOGGLY__APIKEY=${LOGGLY_APIKEY} \
NYBUS_Nybus__RabbitMq__Connection__Hostname=${RABBITMQ_HOSTNAME} \
NYBUS_Nybus__RabbitMq__Connection__Username=${RABBITMQ_USERNAME} \
NYBUS_Nybus__RabbitMq__Connection__Password=${RABBITMQ_PASSWORD} \
NYBUS_Nybus__RabbitMq__UnackedMessageCountLimit=${RABBITMQ_MESSAGE_LIMIT}
ENTRYPOINT [ "dotnet", "App.dll" ]
有什么建议吗?
答案 0 :(得分:1)
您遇到的问题是因为ENV仅在构建时评估,而不在运行时重新评估。因此,如果您重新配置ENV变量“ LOGGLY_APIKEY”,则在启动映像时它将不会更改。
如果要在运行时评估和复制它们,则可以使用Shell脚本包装器。创建一个start.cmd,在构建时将其复制,然后更改您的入口点以将start.cmd文件用作第一个参数。
set NYBUS_Nybus__RabbitMq__Connection__Hostname=%RABBITMQ_HOSTNAME%
.... Other set values ....
shift
start %*
您的dockerfile的入口点为:
ENTRYPOINT [ "start.cmd", "dotnet", "App.dll" ]
答案 1 :(得分:0)
尝试使用ARG代替ENV作为短名称。