Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize docker container bash

Tags:

bash

docker

unix

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 !

like image 614
Kern Avatar asked Feb 20 '17 16:02

Kern


People also ask

How do you edit a running docker container?

To verify, use the cat command to print the contents inside the file. In this way, you can use any editor of your choice to edit files inside the container. If you already have a container running in the background, you can even use the Docker exec command to get access to the bash of the container.

Does Alpine have bash?

By default, bash is not included with BusyBox and Alpine Linux. The postmarketOS project, which is designed to run on mobile devices, is based on Alpine Linux. Many Docker images are also based upon Alpine, and you may install bash shell in Docker-based images too.


2 Answers

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
like image 62
Sébastien Le Gall Avatar answered Oct 07 '22 17:10

Sébastien Le Gall


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.

like image 40
jdhao Avatar answered Oct 07 '22 17:10

jdhao