Using ARG in ENTRYPOINT

Hi everyone, I’ve tried looking all over how to accomplish this and I can’t seem to figure it out. I’m trying to figure out how to pass in an argument to docker build to set the --environment dynamically in the ENTRYPOINT statement. I have tried a lot of things and nothing seems to work. Any advice would be appreciated.

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /app

COPY . .
RUN dotnet restore "./some.csproj" --disable-parallel -s "something"
RUN dotnet publish "./some.csproj" -c release -o /app --no-restore

FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=build /app ./

ARG env=Staging
ENV environment=$env

EXPOSE 80
EXPOSE 443
ENTRYPOINT ["dotnet", "some.dll", "--environment=$environment"]

When I’m checking the pod and looking at the logs, this Console.WriteLine($"environment: {app.Environment.EnvironmentName}"); is outputting the following:
image

You need a shell to use environment variables.

There are two forms of RUN, CMD and ENTRYPOINT instructions. “exec” and “shell”. In the RUN instructions you use “shell” form, while in the ENTRYPOINT you use the “exec” form. That is the way you should, but environment variables can be used only in the shell form or when you start a shell before the main process. Create an entrypoint script like this at /entrypoint.sh:

#!/usr/bin/env sh

exec dotnet some.dll --envrionment=$environment "$@"

And change the entrypoint to:

ENTRYPOINT ["/entrypoint.sh"]

Of course when make the entrypoint script executable.

By the way, are you sure you want an entrypoint and not a command (CMD)? CMD is the argument of entrypoints if there is an entrypoint.

You can read more about it in my blogpost: Constructing commands to run in Docker containers - DEV Community

2 Likes

Thanks! In my instance I had to use:

ENTRYPOINT ["/app/entrypoint.sh"]

But this works. Appreciate it.