Reputation: 2785
I have a situation where i need to source
a bash environment file that lives in a separate volume (pulled in via volumes_from
in docker-compose
) when a container starts so that all future commands run on the container will execute under that bash environment (it runs some scripts and sets a lot of dynamic variables pulled in from other places). The reason I'm using a volume instead of just adding this command directly to the image is because the environment file I need to include is outside the Dockerfile context, and Dockerfiles don't support that.
I tried adding a source /path/to/volume/envfile
line in to the root user's .bashrc
file in the hopes that it would be run when the container started, but that didn't work. I'm assuming that's because the volumes aren't actually mounted until after the container / shell has started and .bashrc
commands have already run (which makes sense).
Does anyone have any idea on how I can accomplish something like this? I'm open to alternative methods, however the one thing I can't change here is moving the file I need inside of the Docker context, as that would break quite a number of other things.
My (slightly edited) Dockerfile and docker-compose.yml files: https://gist.github.com/joeellis/235d90799eb647ab00ec
EDIT: And as a test, I'm trying to run a rake db:create:all
on the container, like docker-compose run app rake db:create:all
which is returning an error that the environment file I need cannot be found / loaded. Interestingly enough, if I shell into the container and run the command, it all seems to work just great. So maybe when a container is given a command via run
, it doesn't necessarily open up a shell, but uses something else?
Upvotes: 2
Views: 1263
Reputation: 3683
The problem is that the shell within which your /src/app/bin/start-app
is ran is not an interactive shell => .bashrc
is not read!
You can fix this by doing this: Add the env file to instead: /root/.profile
RUN echo "source /src/puppet/path/to/file/env/bootstrap" >> /root/.profile
And also run your command as login shell via (sh is bash anyhow for you via the hack in the Dockerfile :) ):
command: "sh -lc '/src/app/bin/start-app'"
as the command. This should work fine :) The problem really is just missing to source that file because you're running in a non-interactive shell when running via the docker command instruction. It works when you shell into the container, because bam you got an interactive shell that sources that file :)
Upvotes: 1