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
#!/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