0

In my project, I used entrypoint.sh as a entrypoint wrapper script to set some dynamic variables inside the docker container.

#!/bin/sh
# entrypoint.sh

# Setting dynamic variable with another script 
. /build/vault-util.sh

exec "/bin/bash"

Then in Dockerfile, I have

COPY vault-util.sh entrypoint.sh /build
ENTRYPOINT ["/build/entrypoint.sh"]

I am able to see the desired variables after starting the container with docker run -it [id] /bin/bash. It is listed in the environments. However, if I try logging into the same container with docker exec -it [id] /bin/bash. The variables are not showing up there. I am wondering what causes different environments with these two commands and what is a potential fix? Thanks!

  • You keep on reproducing the same mistake, using sh on one hand and bash on the other. Be constant. Either run your entrypoint with `["bash", "/build/entrypoint.sh"]` and with the right shebang `#!/usr/bin/env bash`; or exec in your container with `sh`: `docker exec -it [id] sh` – β.εηοιτ.βε Apr 16 '22 at 17:27
  • The `docker exec` debugging shell isn't a child process of the main container process. You shouldn't usually need `docker exec` often enough that this is a practical problem, though. – David Maze Apr 16 '22 at 20:39
  • 1
    In [my answer to your previous question](https://stackoverflow.com/a/71890136) I suggested ending the entrypoint script with `exec "$@"` and leaving a `CMD` in your Dockerfile. If you did this, you could `docker run --rm -it your-image bash` to get a temporary container with an interactive shell running as the main container process, but having run the entrypoint setup. – David Maze Apr 16 '22 at 20:40
  • @David Maze The container is finally deployed to the kubernetes cluster and the variables need to be accessed there. So I am wondering will this also pass variables to the kubernetes deployment? – Wu Qintian Apr 16 '22 at 23:08
  • If you don't override the Pod's `command:` (matching the Docker `ENTRYPOINT`), then yes, this sequence will run though this entrypoint script and initialize the variables. Like in Docker, neither `kubectl describe pod` nor `kubectl exec` will be able to see the environment variables but they will be there in the main container process. – David Maze Apr 16 '22 at 23:45
  • Thank you @David Maze. Then is there anyway to see the variables in the Kubernetes deployment? I mean how can I test that it is actually working – Wu Qintian Apr 18 '22 at 04:02
  • You could have your application log its settings on startup (this can be useful for diagnosing all kinds of configuration problems). – David Maze Apr 18 '22 at 09:57

0 Answers0