Read environment variables from file before starting docker

10/21/2020

I have this dockerfile

FROM rabbitmq:3.7.12-management
CMD . /files/envinfo && echo $RABBITMQ_DEFAULT_USER && rabbitmq-server

In the envinfo I have this content

export RABBITMQ_DEFAULT_USER='anothername'

When the docker starts up the echo of RABBITMQ_DEFAULT_USER really prints out anothername. But when the service starts it doesnt see it.

If I set the environment variable another way from the kubernetes file it works as it should.

You can see the rabbitmq image I extend here. https://github.com/docker-library/rabbitmq/blob/35b41e318d9d9272126f681be74bcbfd9712d71b/3.8/ubuntu/Dockerfile

I have another process that fetches the file and puts it in /files/envinfo to make it available for this docker image when it starts. So I cant use environment settings from kubernetes.

Looking forward to hear some suggestions =)

-- Ole Bille
docker
kubernetes

2 Answers

10/21/2020

You can try to debug it further:

1) Connect to the container and check if the env variable set within it

docker exec -it <container-id> /bin/bash

Then in your container:

echo $RABBITMQ_DEFAULT_USER

2) Use the Kubernetes evironment variable configuration instead of script execution in CMD

See the docs.

apiVersion: v1
kind: Pod
metadata:
  name: envar-demo
  labels:
    purpose: demonstrate-envars
spec:
  containers:
  - name: envar-demo-container
    image: gcr.io/google-samples/node-hello:1.0
    env:
    - name: DEMO_GREETING
      value: "Hello from the environment"
    - name: DEMO_FAREWELL
      value: "Such a sweet sorrow"

Note: I suspect that an evironment variable set within the CMD command is not available in all shells of the container, e.g. when you open a new bash within it. This is what the Kubernetes config takes care of.

-- code-gorilla
Source: StackOverflow

10/21/2020

I agree with @code-gorilla use Kubernetes environment variables. But another way to do it is to source the environment variables before the entry point:

ENTRYPOINT ["source /files/envinfo && docker-entrypoint.sh"]

Overriding CMD will only change the argument to ENTRYPOINT, that's probably why it doesn't work for you.

-- jokarls
Source: StackOverflow