Kern
Kern

Reputation: 868

Customize docker container bash

I try to set a custom configuration for Docker container bash prompt to display the git branch name when connected.

I found everything to make it properly, but I fail to execute the ~/.profile or even ~/.bash_profile files at container's building.

If I perform source ~/.profile manually inside the container, it works fine. But I don't want my users to type any command to enable the custom prompt.

I tried to put RUN /bin/bash -c "source /root/.profile" or RUN source /root/.profile in my Dockerfile, source /root/.profile in my entrypoint.sh file without any success.

I saw some solutions when running docker run, but I am using docker-compose.

Thank you all if you have any piece of advice :D !

Upvotes: 9

Views: 8149

Answers (2)

jdhao
jdhao

Reputation: 28497

This is what I do to make ~/.bash_profile work when I run the docker container. You can use the CMD instruction to start bash as a login shell:

CMD ["bash", "-l"]

The CMD command is run every time the container is started. The -l option will tell bash to start as a login shell so that ~/.bash_profile is sourced for sure.

You might also be interested in the difference between login and non-login shell.

Upvotes: 1

Sébastien Le Gall
Sébastien Le Gall

Reputation: 630

I'm not sure using the ~/.profile configuration file is the best way to do what you want. Also, using RUN source /root/.profile won't have any effect since the line will be executed once only and won't be persistent when trying to execute the bash binary inside de container. (It will actually run a new bash session).

So.. first of all, the kind of configuration you are trying to do should be in the .bashrc file (Just because it is the place where it usually appear).

Then, as the bash man page say :

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order

And :

When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if these files exist.

What you should probably do :

In the Dockerfile :

COPY config/.bashrc /root/.bashrc

The .bashrc file you want to copy into your container is located in a config repo. This is where you should put you configuration.

Then, in the entrypoint :

exec "$@"

Then, you could run bash using the docker command :

docker run XXX /bin/bash

Upvotes: 4

Related Questions