-1

I am trying to allow passing build args to a dockerfile during build process but I cannot figure out how to get it to work.

This is my dockerfile

FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
RUN apt-get update && apt-get install openssh-server -y \
     && echo "root:Docker!" | chpasswd
COPY sshd_config /etc/ssh/
WORKDIR /app
EXPOSE 80
EXPOSE 443
EXPOSE 2222
ARG node_build=production
ENV node_build_env=$node_build
ARG net_build=Release
ENV net_build_env=$net_build

FROM node:12.18.3 AS node-build

RUN echo $build_command_env
WORKDIR /root
COPY ["MyProject/package.json", "."]
COPY ["MyProject/package-lock.json", "."]
RUN npm i --ignore-scripts
COPY ["MyProject/angular.json", "."]
COPY ["MyProject/tsconfig.json", "."]
COPY ["MyProject/tslint.json", "."]
COPY ["MyProject/src", "./src"]

RUN npx ng build -c $node_build_env

FROM microsoft/dotnet:2.2-sdk AS build

RUN echo $net_build_env
WORKDIR /src
COPY ["MyProject/MyProject.csproj", "MyProject/"]
COPY ["MyProject.ServiceModel/MyProject.ServiceModel.csproj", "MyProject.ServiceModel/"]
COPY ["MyProject.ServiceInterface/MyProject.ServiceInterface.csproj", "MyProject.ServiceInterface/"]
COPY ["NuGet.config", "NuGet.config"]
RUN dotnet restore "MyProject/MyProject.csproj"
COPY . .
WORKDIR "/src/MyProject"
RUN dotnet build "MyProject.csproj" -c $net_build_env -o /app

FROM build AS publish

RUN dotnet publish "MyProject.csproj" -c $net_build_env -o /app

FROM base AS final
WORKDIR /app
COPY --chown=www-data:www-data --from=publish /app .
COPY --chown=www-data:www-data --from=node-build /root/wwwroot ./wwwroot
ENTRYPOINT service ssh start && dotnet MyProject.dll

I have tried many different ways. I tried just using ARG and declaring after every FROM but that didn't work. This version I try copying ARG to ENV but that also didn't work.

How do I do this?

Guerrilla
  • 13,375
  • 31
  • 109
  • 210

2 Answers2

0

Issue was that variable had to be in double quote:

RUN dotnet build "MyProject.csproj" -c "$net_build_env" -o /app
Guerrilla
  • 13,375
  • 31
  • 109
  • 210
-1

You have to add --build-arg <arg>=<value> in your docker build command in order to use ARG arguments in your containerfile.

There's no need for ENV if you're only going to use the args in container build process

ahmadali shafiee
  • 4,350
  • 12
  • 56
  • 91