I have a Dockerfile based on a Home Assistant image where I create a script that I’d like to run when the container starts using CMD.
A simple example Dockerfile is:
FROM homeassistant/aarch64-base-debian
ENV FOO=BAR
RUN printf "#!/bin/bash \n\
echo env... \n\
env \n\
echo waiting... \n\
tail -f /dev/null" > ./script.sh
RUN chmod +x ./script.sh
CMD "./script.sh"
When script.sh is run, the only environment variables present are PWD, SHLVL, PATH and OLDPWD, none of the ones I set with ENV (e.g. FOO in the above example), or other standard ones like HOME are present.
If I log into the container using the command:
docker exec -it <CONTAINER_ID> /bin/bash
and print the environment of the container, all the variables are present, so it looks like it’s something specific to the CMD environment.
Why is this and how can I run the script so that it runs with the usual container environment?
I gess you need to change to
CMD [./script.sh]
Using shell form (no []) has a main drawback of no running your software as PID 1. And variables from Dockerfile are set for pid 1
Take a read here: https://docs.docker.com/engine/reference/builder/#cmd
Please provide a minimal reproducible example.
With the current example you provided, the env variables are present when running the container. Cannot reproduce, as noted by the previous commenter too.