简体   繁体   English

自定义docker容器bash

[英]Customize docker container bash

I try to set a custom configuration for Docker container bash prompt to display the git branch name when connected. 我尝试为Docker容器bash提示设置自定义配置,以便在连接时显示git分支名称。

I found everything to make it properly, but I fail to execute the ~/.profile or even ~/.bash_profile files at container's building. 我发现一切都做得很好,但是我无法在容器的建筑物上执行~/.profile甚至~/.bash_profile文件。

If I perform source ~/.profile manually inside the container, it works fine. 如果我在容器内手动执行source ~/.profile ,它可以正常工作。 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. 我试图将RUN /bin/bash -c "source /root/.profile"RUN source /root/.profile放在我的Dockerfile中, source /root/.profile我的entrypoint.sh文件中的source /root/.profile没有任何成功。

I saw some solutions when running docker run , but I am using docker-compose. 我在运行docker run时看到了一些解决方案,但我使用的是docker-compose。

Thank you all if you have any piece of advice :D ! 如果你有任何建议,谢谢大家:D!

I'm not sure using the ~/.profile configuration file is the best way to do what you want. 我不确定使用〜/ .profile配置文件是做你想要的最好的方法。 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. 此外,使用RUN source /root/.profile不会产生任何影响,因为该行只会执行一次,并且在尝试执行de容器内的bash二进制文件时不会持久。 (It will actually run a new bash session). (它实际上会运行一个新的bash会话)。

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). 所以..首先,你要做的配置应该在.bashrc文件中(因为它通常出现在那里)。

Then, as the bash man page say : 然后,正如bash手册页所说:

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. 当bash作为交互式登录shell或作为具有--login选项的非交互式shell调用时,它首先从文件/ etc / profile中读取并执行命令(如果该文件存在)。 After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order 读完该文件后,它按顺序查找〜/ .bash_profile,〜/ .bash_login和〜/ .profile

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. 当启动不是登录shell的交互式shell时,如果存在这些文件,bash将从/etc/bash.bashrc和〜/ .bashrc读取并执行命令。

What you should probably do : 你应该做什么:

In the Dockerfile : 在Dockerfile中:

COPY config/.bashrc /root/.bashrc

The .bashrc file you want to copy into your container is located in a config repo. 要复制到容器中的.bashrc文件位于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命令运行bash:

docker run XXX /bin/bash

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM