I have a golang Dockerfile, where I want to run the final scratch image as the deployment image, as such:
...
# Prepare the app for deployment
FROM base as build
# Copy source files and restore dependencies--
COPY . .
RUN make restore-dependencies
# Create user to run app
ENV USER=appuser
RUN adduser \
--disabled-password \
--gecos "" \
--shell "/sbin/nologin" \
--no-create-home \
"${USER}"
RUN make
# # Deployment image
FROM scratch
# Import the user and group files from the build stage
COPY --from=build /etc/passwd /etc/passwd
COPY --from=build /etc/group/ /etc/group
# Copy static binary from previous stage
COPY --from=build /go/bin/ideaservice /go/bin/ideaservice
# Use an unprivileged user
USER appuser:appuser
# Run binary
ENTRYPOINT [ "/go/bin/ideaservice" ]
I want to run this scratch image as the unpriviledged user, and pass an environment variable, like so:
docker run -e POSTGRES_CONN_STR="host=postgres..."
However, the env var is not passed to the container. I have searched around for a solution, and have found out that it is because the env vars passed through run only go to the root user, not the one that is in this case set.
How do I pass an environment variable to the current user, so that my application running within the container can access it?