Search code examples
dockerdocker-composedockerfilecontainersdocker-entrypoint

Pass ENV var through 'docker run -e MY_VAR=value' to entrypoint.sh?


I am trying to pass ENV vars at runtime to my docker container's startup file (entrypoint.sh) through the dockerfile. The ENV vars come from a key vault in an azure devops pipeline.

I can't seem to pass in ENV vars with a simple docker run -e YUMMY_USER=$(MY_USER) -e YUMMY_PASSWORD=$(MY_PW) yummyAppImage. The vars are used to login to an authorization service. It should be noted that both the user and password coming from the key vault contain special characters. My dockerfile looks like this:

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
MAINTAINER YummyLumpkins <yummy@lumpkins.com>

WORKDIR /app

COPY . ./
RUN dotnet restore

RUN dotnet publish YummyApp -c Release -o out

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /app
COPY --from=build-env /app/out .

COPY entrypoint.sh ./
RUN dos2unix entrypoint.sh && chmod +x entrypoint.sh
CMD ["/app/entrypoint.sh"]

And my shell script file looks like so:

#!/bin/sh
set -e

az login --service-principal --username $YUMMY_USER --password $YUMMY_PASSWORD
dotnet /app/YummyApp.dll

exec "$@"

I have tried to manually pass in the ENV vars in the docker run command, like so: docker run -e YUMMY_USER=exampleuser12983#$23 -e YUMMY_PASSWORD=examplepw(*&876 YummyAppImage but the login service simply doesn't see the arguments.

Perhaps my syntax is incorrect? Any help would be greatly appreciated. Thanks!


Solution

  • Quote your variables. Special characters in those strings without quotes will be interpreted by the shell running the command:

    #!/bin/sh
    set -e
    
    az login --service-principal --username "$YUMMY_USER" --password "$YUMMY_PASSWORD"
    dotnet /app/YummyApp.dll
    
    exec "$@"
    

    And also in your docker run command depending on your shell:

    docker run \
      -e 'YUMMY_USER=exampleuser12983#$23' \
      -e 'YUMMY_PASSWORD=examplepw(*&876' \
      YummyAppImage