How to pass environment variable from docker run command to supervisor
I am trying to pass supervisor varibale from docker run command so that it can execute script with variable value. We need to set this up at runtime so that each developer can have their own queue in rabbitmq so we don't bump each other into queues during testing.
Docker start command:
docker run -i -p 5672:5672 -p 9200:9200 -p 9300:9300 -p 9001:9001 -p 15672:15672 -e "PARENT_HOSTNAME=MACHINED58" --rm --name shovel -t dtwill/blkmesa:shovel
Docker CMD (after looking at the docs, I know why the error occurs, cannot provide arbritrary parm http://supervisord.org/running.html ):
CMD /usr/bin/supervisord
Supervisor configuration:
[program:update_rabbit_config]
command=/src/update_rabbit_config.sh
redirect_stderr=true
priority=200
startsecs=3
the script is executed by the supervisor:
machineName=$PARENT_HOSTNAME
echo machine name = $machineName
sed -i .bak "s/|machine|/'$machineName'/" /etc/rabbitmq/rabbitmq.config
This is mistake:
INFO exited: update_rabbit_config (exit status 1; not expected)
... so if anyone knows how to do this, I would be very grateful and happy dancing when I wake up.
Thank!
[Updated] I've updated the relevant snippets to use the guidelines in the answer. Also I include the result of the script when I ran it manually (after using nsenter to connect to the running container):
root@1e2aeaa3dfb8:/src# bash update_rabbit_config.sh
machine name =
sed: -e expression #1, char 1: unknown command: `.'
It looks like the environment variable is not in context.
source to share
I'm not a supervisor expert, but the documentation says that:
Note that the subprocess inherits the shell environment variables used to start the "supervisor", with the exception of those overridden here (
environment
). See Subprocess Environment .
From my understanding, you should remove environment=PARENT_HOSTNAME=%(PARENT_HOSTNAME)s
from supervisor config.
There is also a problem with your team sed
. Since you used single quotes '
, no variable substitution will occur. Try this instead:
echo machine name = $PARENT_HOSTNAME
sed -i .bak "s/|machine|/$PARENT_HOSTNAME/" /etc/rabbitmq/rabbitmq.config
source to share