Runit in Docker container does not pass on environment variables

1.3k Views Asked by At

I'm trying to run a multi-process app in Docker with runit serving as init process but runit does not pass environment variables to the app:

In Dockerfile:

CMD ["runit"]

The service file /etc/service/app/run looks like this:

#!/bin/sh
exec 2>&1

echo "ENV_VAR=$ENV_VAR"
exec app

When I run the docker container with ENV_VAR set, the variable doesn't get passed to the app by runit. Output:

# docker run --name container -e ENV_VAR=loremipsum -d IMAGE_NAME
# docker logs container
- runit: $Id: 25da3b86f7bed4038b8a039d2f8e8c9bbcf0822b $: booting.
- runit: warning: unable to open /dev/console: file does not exist
- runit: enter stage: /etc/runit/1
/etc/runit/1: 6: /etc/runit/1: /etc/init.d/rcS: not found
/etc/runit/1: 7: /etc/runit/1: /etc/init.d/rmnologin: not found
/etc/runit/1: 12: /etc/runit/1: /etc/init.d/rc: not found
- runit: warning: child failed: /etc/runit/1
- runit: leave stage: /etc/runit/1
- runit: enter stage: /etc/runit/2
ENV_VAR=
...
* app failed because ENV_VAR was not set correctly *

How can I make runit pass the environment variable to the service/app?

2

There are 2 best solutions below

3
Servet TAS On

you can try this way

ENV MY_ENV_VAR $MY_ENV_VAR

or

example here

ARG var_name
ENV env_var_name=$var_name

and

docker build --build-arg var_name=${VARIABLE_NAME} (...)
0
Mostafa Hussein On

I came by your question while working on similar setup and found out it was working for me so as you can see in my example below that you have the ability to call the environment variable directly.

Here is the example I have tested:

Script 1

# This is the run script that will be used for this dummy service
$ cat stackoverflow-question/run 
#!/bin/sh
exec 2>&1

exec echo "$Q_NUM"

Script 2

# This is the run script that will be used for this dummy service
$ cat stackoverflow-question/run 
#!/bin/sh
exec 2>&1

NUMBER=$Q_NUM

exec echo "$NUMBER"

I have used an image called dockage/alpine-runit for testing so feel free to use your own runit installation still applies.

Note: I Have tested both the default CMD that comes with this image and the runit command to reproduce your example and both worked as expected as you can see it prints the question number as we wanted.

$ docker run -it --rm -v "$PWD:/service/" \
  -e Q_NUM=54089994 dockage/alpine-runit:latest runit
- runit: $Id: 25da3b86f7bed4038b8a039d2f8e8c9bbcf0822b $: booting.
- runit: enter stage: /etc/runit/1
- runit: leave stage: /etc/runit/1
- runit: enter stage: /etc/runit/2
54089994
54089994
54089994
54089994