0

Using Docker Compose, how do you pass environment variables to the app user shell such that the environment variables are always loaded when that user runs a command?

For example, I have tried via Dockerfile:

FROM phusion/passenger-ruby26

ENV FOO=bar

RUN dpkg --clear-avail
RUN rm -rf /var/lib/apt/lists/*
RUN apt-get -qq update --fix-missing
RUN apt-get -qq install -f
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get clean
RUN apt-get -qq install -y build-essential apt apt-utils \
                           software-properties-common wget inetutils-ping \
                           sudo netcat

COPY --chown=app:app . .

ENTRYPOINT ["sh", "./startup.sh"]

startup.sh is:

_user="$(id -u -n)"
echo "User name : $_user"

su - app -c 'echo "**************** root printenv:"'
printenv
su - app -c 'echo "**************** app printenv:"'
su - app -c 'printenv'

The build/run output includes a number of existing/pre-set variables -- but not FOO. When I don't use sudo, I can see the variable in the printenv run as root. However, the variable is not available to app user.

What is the best way to make FOO available to user app? Any help is much appreciated.

Sam
  • 1,205
  • 1
  • 21
  • 39
  • 1
    You shouldn't need `sudo` in Docker at all, but one of its features is cleaning the environment; the linked question describes the `sudo -E` option. I might focus on including your application in the Dockerfile over trying to make an interactive debugging shell work well. – David Maze Aug 17 '20 at 10:57
  • Thanks @DavidMaze. I changed out sudo for su in the script, and I can see FOO=bar in the *root* user printenv. What is the preferred way of getting that to app user? – Sam Aug 17 '20 at 12:31
  • 1
    You shouldn't need `su` in Docker at all. Just set your image's `USER`, or the `docker run -u` option, to the user you need. – David Maze Aug 17 '20 at 13:25
  • I take your point, But the script is only to show the symptom of the problem: When I set environment variables in compose or env_file, they are only available to root. Hence my question. – Sam Aug 17 '20 at 13:34
  • Try `docker run -u app --entrypoint /bin/env your-image`. You should see the environment variables you set in the image, and you're running as the application user. – David Maze Aug 17 '20 at 13:39

0 Answers0