1. Overview
1.概述
In this article, we’ll see how to update the PATH variable in Docker. Firstly, we’ll update it globally. Then, we’ll restrict the change to a subset of instructions.
在这篇文章中,我们将看到如何更新Docker中的PATH变量。首先,我们将全局地更新它。然后,我们将把这个变化限制在一个指令子集上。
2. Updating the Global PATH Variable
2.更新全局PATH变量
The ENV statement can be used to update the PATH variable. Let’s write an example Dockerfile to showcase this behaviour:
ENV语句可用于更新PATH变量。让我们写一个例子Dockerfile来展示这一行为。
FROM ubuntu:latest
RUN echo $PATH
ENV PATH="$PATH:/etc/profile"
RUN echo $PATH
The first line states that we use the most recent Ubuntu image. We’re also logging the value of the PATH variable before and after the ENV instruction.
第一行指出,我们使用的是最新的Ubuntu镜像。我们还记录了PATH变量在ENV指令前后的值。
Let’s build our image:
让我们建立我们的形象。
$ docker build -t baeldungimage .
#4 [1/3] FROM docker.io/library/ubuntu:latest
#5 [2/3] RUN echo /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
#5 0.683 /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
#6 [3/3] RUN echo /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/etc/profile
#6 0.893 /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/etc/profile
As expected, /etc/profile has been appended to PATH.
正如预期的那样,/etc/profile已被追加到PATH。
3. Updating PATH Only for a Sequence of Instructions
3.只为一连串的指令更新PATH
We’ll use a RUN instruction to run a sh script to export a new PATH. After that, we’ll add another instruction inside the same RUN statement. This will print the local value of PATH inside the RUN statement. We’ll also log the PATH global variable afterward to confirm that it didn’t change.
我们将使用一个RUN指令来运行一个sh脚本来导出一个新的PATH。之后,我们将在同一个RUN语句里面添加另一条指令。这将在RUN语句中打印出PATH的本地值。我们还将在之后记录PATH全局变量,以确认它没有变化。
Here’s our new Dockerfile:
这是我们的新Dockerfile。
FROM ubuntu:latest
RUN echo $PATH
RUN export PATH="$PATH:/etc/profile"; echo $PATH
RUN echo $PATH
We can now build the image:
我们现在可以建立图像了。
$ docker build -t baeldungimage .
#7 [1/4] FROM docker.io/library/ubuntu:latest
#4 [2/4] RUN echo /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
#4 0.477 /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
#5 [3/4] RUN export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/etc/profile"; echo /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
#5 0.660 /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/etc/profile
#6 [4/4] RUN echo /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
#6 0.661 /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
The log confirms that after exporting the PATH variable, its value’s used by further instructions inside the same RUN command. However, the global variable hasn’t changed.
日志证实,在导出PATH变量后,它的值被同一RUN命令中的其他指令使用。然而,全局变量并没有改变。
4. Updating PATH Inside Shell Sessions
4.在Shell会话中更新PATH
Let’s now see how we can update PATH for shell sessions only. First, we’ll modify the .bashrc file to update the PATH at the beginning of every shell session. Then, we’ll start a shell session to check this behaviour.
现在让我们看看如何只为shell会话更新PATH。首先,我们将修改.bashrc文件,在每个shell会话开始时更新PATH。然后,我们将启动一个shell会话来检查这一行为。
4.1. Editing the .bashrc File
4.1.编辑.bashrc文件
We’ll edit the .bashrc file to export a new PATH at the beginning of every shell session. To do so, we’ll run a quick script to append the export to the original file. As we did earlier, we’ll check that this change doesn’t impact the global PATH variable.
我们将编辑.bashrc文件以在每个shell会话开始时导出一个新的PATH。为此,我们将运行一个快速脚本来将导出的文件附加到原始文件中。正如我们先前所做的那样,我们将检查这一变化是否影响全局PATH变量。
Here’s the new Dockerfile:
这是新的Dockerfile。
FROM ubuntu:latest
RUN echo $PATH
RUN echo "export PATH=$PATH:/etc/profile" >> ~/.bashrc
RUN cat ~/.bashrc
RUN echo $PATH
Additionally, let’s note that we used the cat command to view the file.
此外,让我们注意,我们使用了cat命令来查看文件。
Let’s build the image:
让我们来建立形象。
$ docker build -t baeldungimage .
#4 [1/5] FROM docker.io/library/ubuntu:latest
#5 [2/5] RUN echo /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
#5 0.447 /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
#6 [3/5] RUN echo "export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/etc/profile" >> ~/.bashrc
#7 [4/5] RUN cat ~/.bashrc
#7 0.956 # ~/.bashrc: executed by bash(1) for non-login shells.
#7 0.956 # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
#7 0.956 # for examples
#7 0.956
#7 0.956 # If not running interactively, don't do anything
#7 0.956 [ -z "$PS1" ] && return
[... .bashrc full content]
#7 0.956 export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/etc/profile
#7 DONE 1.0s
#8 [5/5] RUN echo /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
#8 0.867 /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
As we can see, the global PATH didn’t change. However, the export line was indeed added at the end of the file. As a result, whenever .bashrc is loaded, it updates the PATH variable for the running shell.
我们可以看到,全局的PATH并没有改变。然而,导出行确实被加到了文件的末尾。因此,每当.bashrc被加载时,它就会为运行中的shell更新PATH变量。
4.2. Running a Container in Interactive Mode
4.2.在交互式模式下运行一个容器
Let’s now focus on the few first lines of the .bashrc file from the output we saw earlier. These lines are from the original file. It states clearly “If not running interactively, don’t do anything”.
现在让我们把注意力集中在我们先前看到的输出中的.bashrc文件的前几行。这几行是来自原始文件。它明确指出”如果不是交互式运行,不要做任何事情”。
It’s crucial to understand that when we build a Dockerfile, the RUN command’s not interactive. So we won’t be able to just source our .bashrc file and RUN a script during the build to check if the path has been updated.
关键是要明白,当我们构建一个Dockerfile时,RUN命令不是交互式的。因此,我们无法在构建过程中,只需将我们的.bashrc文件作为源码,然后RUN一个脚本来检查路径是否已经更新。
Instead, we can run a container in interactive mode and open a shell session:
相反,我们可以在交互式模式下运行一个容器并打开一个shell会话:
$ docker run -it --name interactiveimage baeldungimage
root@18781222594f:/# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/etc/profile
Once in the shell session, we printed the PATH. We can see that /etc/profile was appended, confirming that our .bashrc file has been taken into account.
一旦进入shell会话,我们打印了PATH。我们可以看到/etc/profile被追加了,确认我们的.bashrc文件已经被考虑进去了。
5. Conclusion
5.总结
In this tutorial, we’ve seen how to update the PATH variable in Docker. Initially, we updated the variable globally, but we’ve also learned how to update it with more restrictions.
在本教程中,我们已经看到了如何更新Docker中的PATH变量。最初,我们在全局范围内更新了这个变量,但我们也学会了如何在更多的限制下更新它。
As always, the code is available over on GitHub.
像往常一样,代码可在GitHub上获得。