docker環(huán)境變量配置不生效/ect/profile的解決方法
問題描述
docker啟動的centos,每次進(jìn)入終端,配置在/ect/profile 的環(huán)境變量沒有生效
# 運(yùn)行centos 獲取systemctl權(quán)限 docker run \ --privileged \ -itd \ --name centos7 \ -p 8082:8080 \ -v /sys/fs/cgroup:/sys/fs/cgroup:ro \ centos:centos7 /usr/sbin/init # 進(jìn)入終端 docker exec -it centos7 /bin/bash
問題分析
通過查看幾個用戶環(huán)境變量文件,發(fā)現(xiàn)有如下調(diào)用鏈
~/.bash_profile ~/.bashrc /etc/bashrc /etc/profile.d/*.sh
唯獨漏了配置文件 /etc/profile
# 查看 ~/.bash_profile
$ more ~/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# 查看 ~/.bashrc
$ more ~/.bashrc
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# 查看 /etc/bashrc
$ more /etc/bashrc
# /etc/bashrc
for i in /etc/profile.d/*.sh; do
if [ -r "$i" ]; then
if [ "$PS1" ]; then
. "$i"
else
. "$i" >/dev/null
fi
fi
done
# 查看 /etc/profile
$ more /etc/profile
# /etc/profile
for i in /etc/profile.d/*.sh /etc/profile.d/sh.local ; do
if [ -r "$i" ]; then
if [ "${-#*i}" != "$-" ]; then
. "$i"
else
. "$i" >/dev/null
fi
fi
done
問題解決
所以,我們可以將環(huán)境放到文件夾/etc/profile.d 中
無論哪種方式都會執(zhí)行該文件夾中的shell腳本
添加環(huán)境變量
# 新建環(huán)境變量文件 vim /etc/profile.d/env.sh # /etc/profile.d/env.sh # jdk JAVA_HOME=/usr/local/jdk1.8.0_351 PATH=$JAVA_HOME/bin:$PATH
再次啟動,就可以生效了
其實,幾個配置文件也都有提示
# It's NOT a good idea to change this file unless you know what you # are doing. It's much better to create a custom.sh shell script in # /etc/profile.d/ to make custom changes to your environment, as this # will prevent the need for merging in future updates.
大致意思就說,用戶環(huán)境變量放在文件夾/etc/profile.d/ 中是比較好的方式
到此這篇關(guān)于docker環(huán)境變量配置不生效/ect/profile的解決方法的文章就介紹到這了,更多相關(guān)docker環(huán)境變量配置不生效內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決docker拉取鏡像報錯:error pulling image configurat
在使用Docker拉取Kafka鏡像時可能會遇到"error pulling image configuration"的錯誤,這可以通過編輯Docker配置文件并重啟Docker服務(wù)來解決,具體步驟包括:1. 編輯Docker配置文件;2. 使用命令systemctl restart docker重啟Docker服務(wù)2024-11-11

