Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Environment variables empty in Docker container bash

I have a simple docker container that runs a script on startup which exports some variables.

So the final line in my Dockerfile is CMD ./startup.sh

And startup.sh has

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#!/usr/bin/env bash

export testvar="test"
echo $testvar
node app.js

The output in terminal when running container shows "test" as I would expect.

However if I then run docker exec -it *containerid* bash and run echo $testvar inside the container it’s empty.

Has the environment var not persisted? Or does the terminal from running docker exec bash not have permission to see it or something?

>Solution :

docker exec starts a new shell in the container. It’s not a child of the the initial process, so it won’t inherit any environment variables from that process.

If you want to set environment variables that will be visible in docker exec, then set them on the container itself, either in your Dockerfile:

FROM docker.io/node:18

ENV testvar=test
CMD node app.js

Or on the docker run command line:

docker run -e testvar=test myimagename
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading