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" ]