docker環(huán)境變量配置不生效/ect/profile的解決方法
問題描述
docker啟動(dòng)的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
問題分析
通過查看幾個(gè)用戶環(huán)境變量文件,發(fā)現(xiàn)有如下調(diào)用鏈
~/.bash_profile ~/.bashrc /etc/bashrc /etc/profile.d/*.sh
唯獨(dú)漏了配置文件 /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
中
無論哪種方式都會(huì)執(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
再次啟動(dòng),就可以生效了
其實(shí),幾個(gè)配置文件也都有提示
# 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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
docker基礎(chǔ)知識(shí)之掛載本地目錄的方法
本篇文章主要介紹了docker基礎(chǔ)知識(shí)之掛載本地目錄的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04Docker 配置固定IP及橋接的實(shí)現(xiàn)方法
這篇文章主要介紹了Docker 配置固定IP和橋接的實(shí)現(xiàn)方法的相關(guān)資料,這里詳細(xì)介紹了Docker 的四種網(wǎng)絡(luò)模式及如何實(shí)現(xiàn)橋接的案例,需要的朋友可以參考下2016-10-10詳解如何將java項(xiàng)目打包成docker鏡像并且可運(yùn)行
java?項(xiàng)目打包成?Docker?可運(yùn)行的鏡像,其目的是便于運(yùn)用docker容器來管理項(xiàng)目,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2024-11-11用Docker swarm快速部署Nebula Graph集群的教程
這篇文章主要介紹了用Docker swarm快速部署Nebula Graph集群的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09