English 中文(简体)
对Dockerfile中特殊壳变量的解释
原标题:Interpretation of special shell variables in Dockerfile

I m a docker newbie and I m learning how to write Dockerfile. I ve been experimenting with different ways of writing RUN instructions, and I ve come across some behavior that I don t understand why, so I d like to know about it.

当我建造和操作以下Dockerfile.v1时,该样本中就存在。 预计情况也是如此。

FROM ubuntu:latest
RUN mkdir sample_dir && cd sample_dir && touch sample_file

Then, I decided to use shell variables for general purpose writing, I created the following Dockerfile.v2 and RUN.

FROM ubuntu:latest
RUN mkdir sample_dir && cd $_ && touch sample_file

Then, sample_file was created in /root, which is not what I expected. Since the sample_dir itself was created in /, I assume WORKING DIRECTORY is /.

为什么在使用者的家名录中产生? 如何解释?

❯ docker run -it --rm 1ce bash                                                                          2023/09/17 18:01:16
root@2ce4010e4613:/# 
root@2ce4010e4613:/# ll
total 60
drwxr-xr-x   1 root root 4096 Sep 17 09:01 ./
drwxr-xr-x   1 root root 4096 Sep 17 09:01 ../
-rwxr-xr-x   1 root root    0 Sep 17 09:01 .dockerenv*
lrwxrwxrwx   1 root root    7 Aug 16 02:25 bin -> usr/bin/
drwxr-xr-x   2 root root 4096 Apr 18  2022 boot/
drwxr-xr-x   5 root root  360 Sep 17 09:01 dev/
drwxr-xr-x   1 root root 4096 Sep 17 09:01 etc/
drwxr-xr-x   2 root root 4096 Apr 18  2022 home/
lrwxrwxrwx   1 root root    7 Aug 16 02:25 lib -> usr/lib/
drwxr-xr-x   2 root root 4096 Aug 16 02:25 media/
drwxr-xr-x   2 root root 4096 Aug 16 02:25 mnt/
drwxr-xr-x   2 root root 4096 Aug 16 02:25 opt/
dr-xr-xr-x 212 root root    0 Sep 17 09:01 proc/
drwx------   1 root root 4096 Sep 17 07:39 root/
drwxr-xr-x   5 root root 4096 Aug 16 04:54 run/
drwxr-xr-x   2 root root 4096 Sep 17 07:39 sample_dir/
lrwxrwxrwx   1 root root    8 Aug 16 02:25 sbin -> usr/sbin/
drwxr-xr-x   2 root root 4096 Aug 16 02:25 srv/
dr-xr-xr-x  13 root root    0 Sep 17 09:01 sys/
drwxrwxrwt   2 root root 4096 Aug 16 04:52 tmp/
drwxr-xr-x  11 root root 4096 Aug 16 02:25 usr/
drwxr-xr-x  11 root root 4096 Aug 16 04:52 var/
root@2ce4010e4613:/# ll /root/
total 16
drwx------ 1 root root 4096 Sep 17 07:39 ./
drwxr-xr-x 1 root root 4096 Sep 17 09:01 ../
-rw-r--r-- 1 root root 3106 Oct 15  2021 .bashrc
-rw-r--r-- 1 root root  161 Jul  9  2019 .profile
-rw-r--r-- 1 root root    0 Sep 17 07:39 sample_file
root@2ce4010e4613:/# 

我知道,我可以通过不使用美元或使用工作组的指示来避免这种行为,但我只想知道为什么发生这种情况。

我认为,这可能是因为违约是/bin/sh,因此,我具体指明了下文所示的SHELLINSTRUC的布局,但没有任何变化。

https://docs.docker.com/engine/vis/buider/pshell” rel=“noestlow noreferer” https://docs.docker.com/engine/vis/buider/pshell>

FROM ubuntu:latest
RUN mkdir sample_dir && cd $_ && touch sample_file
SHELL [ "/bin/bash" ]
问题回答

<代码>_在互动的Bash中仅具有实际意义。 更标准、更可行的解决办法是使用固定变量。

RUN d=sample_dir && mkdir "$d" && cd "$d" && touch sample_file

......当然,<代码>cd在此基本上没有用处。

RUN d=sample_dir && mkdir "$d" && touch "$d"/sample_file

拟议的特别参数:,$@,。 拖欠的炮弹是符合PPOSIX要求的壳体,而不是GNU bash(例如见,问题),因此,其他特定壳的延伸物是没有的。 <> 代码>_ 是标准界定的特别参数之一,因此扩大到空洞。 由于你没有引述变数的扩展,因此关于<代码>cd的论点 只是丢失了,这 s了你在<代码>/root上的结晶。

更具体地说,在Docker的情形下,还记得,每条<代码>RUN线开始在新的集装箱内出现新的壳体,而部分空壳环境已经消失。 特别是<代码>RUN cd 获胜t 具有持久性效应。 我不妨建议使用Dockerfile。 更改名录的指令

WORKDIR /sample_dir
RUN touch sample_file

如果目录存在的话,还将制作目录。





相关问题
KornShell- Creating a fixed width text file

I need to create a simple fixed width text file in KornShell (ksh). My current attempt using printf to pad the string isn t working out very well. What s the shortest, cleanest way to create a fixed ...

unix find command

how to use the find command to find files/directories which are not matching the pattern. for eg: find <some options > -name "dontfile.txt" should give me output of all the find whose file ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

1. 露台式照像

在Windows XP中,没有一时出现指挥时,是否有办法操作一种灰色文字? 我常常需要把“善待”(工作)与“灰色”同起来,即使我的文字没有产出,......