我一直在尝试使用docker commit --change ...
标志更新容器PATH env变量值,但由于无法获得所需的结果,我必须对所涉及的某些部分缺乏了解。
给出:
$ docker run -it centos:6 /bin/bash
[root@3b459018aa84 /]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
exit
尝试1:
$ docker commit -c "ENV PATH /usr/local/texlive/bin:${PATH}" 3b459018aa84 test-changes
$ docker run -it test-changes /bin/bash
[root@25116cd93d79 /]# echo $PATH
/usr/local/texlive/bin:/opt/local/bin:/opt/local/sbin:/Users/Ivan/.jenv/shims:/Users/Ivan/.jenv/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/usr/local/share/dotnet:/opt/X11/bin
结果PATH似乎包含主机的PATH值。我猜是因为我使用了双引号导致
${PATH}
在传递给docker commit
之前被扩展。尝试2:
$ docker commit -c 'ENV PATH /usr/local/texlive/bin:${PATH}' 3b459018aa84 test-changes
$ docker run -it test-changes /bin/bash
[root@c10a98a7fbce /]# echo $PATH
/usr/local/texlive/bin:
尝试3:
$ docker commit -c "ENV PATH=\"/usr/local/texlive/bin:${PATH}\"" 3b459018aa84 test-changes
$ docker run -it test-changes /bin/bash
[root@240c3e268d08 /]# echo $PATH
/usr/local/texlive/bin:/opt/local/bin:/opt/local/sbin:/Users/Ivan/.jenv/shims:/Users/Ivan/.jenv/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/usr/local/share/dotnet:/opt/X11/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands
尝试4:
$ docker commit -c 'ENV PATH=\"/usr/local/texlive/bin:${PATH}\"' 3b459018aa84 test-changes
$ docker run -it test-changes /bin/bash
[root@bb10d27f1360 /]# echo $PATH
"/usr/local/texlive/bin:"
尝试5:
$ docker commit -c 'ENV PATH="/usr/local/texlive/bin:${PATH}"' 3b459018aa84 test-changes
$ docker run -it test-changes /bin/bash
[root@a683ab06f4d3 /]# echo $PATH
/usr/local/texlive/bin:
尝试6:
再次阅读
docker commit
文档后,我看到了--pause
标志。因此,我测试了一种理论,即暂停容器会影响提交期间对环境变量的访问。$ docker commit -c 'ENV PATH="/usr/local/texlive/bin:${PATH}"' -p=false 3b459018aa84 test-changes
$ docker run -it test-changes /bin/bash
/usr/local/texlive/bin:
不,也没有运气...
所需的输出:
[root@c10a98a7fbce /]# echo $PATH
/usr/local/texlive/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
换句话说,我希望输出就像
ENV PATH /usr/local/texlive/bin:${PATH}
被添加到Dockerfile中一样。我猜想我错过了一些愚蠢的东西,希望对其他人显而易见,但是我没有想法。
万一涉及到这一点,我正在Mac OS 10.13.2,Docker 17.12.0-ce-mac55(23011)上运行。
最佳答案
第一步:访问容器的$ PATH变量
您可以使用inspect
做到这一点:
docker container inspect container_name --format '{{ index .Config.Env 0 }}'
第二步:在$ PATH后面添加一些内容并提交到新镜像
我将上一步的命令放在``(反引号)内,
docker container commit -c "ENV `docker container inspect container_name --format '{{ index .Config.Env 0 }}':some_other_path_goes_here`" container_name new_image_name
请访问以下问题以查看其工作原理:
例
$ docker container run --rm -itd --name my_container ubuntu
25b357a26d68f047dd98c87ff4598800db160ad3136470f3285ccd20c1297cfa
$ docker container exec -it my_container /bin/sh -c 'echo $PATH'
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
$ docker container commit -c "ENV `docker container inspect my_container --format '{{ index .Config.Env 0 }}':some_other_path_goes_here`" my_container new_image_name
sha256:584226328c3da8b6239ec8f8b92f71ce2e03090341c6efb03602055fbf1ff582
$ docker container run --rm -itd --name my_new_container new_image_name
40279673d0c45de7ab53d6be488d6ff32259e3bc8a76bd2ef41a53f065a2ba30
$ docker container exec -it my_new_container /bin/sh -c 'echo $PATH'
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:some_other_path_goes_here
关于docker - 如何使用commit “--change”标志更新Docker容器PATH?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49235330/