docker run centos cat /etc/hosts > asdf
asdf保存在我的主机linux中
docker run centos sh -c 'cat /etc/hosts > /tmp/asdf ' docker run centos cat /tmp/asdf cat: /tmp/asdf: No such file or directory
这不行,我该怎么办?
docker run centos sh -c 'cat < /etc/hosts' 127.0.0.1 localhost
好
docker run centos cat < /etc/hosts
什么也没有发生,我怎么可以猫容器中的主机文件?
docker run centos sh -c 'cat /etc/hosts > /tmp/asdf ' docker run centos cat /tmp/asdf cat: /tmp/asdf: No such file or directory这不行,我该怎么办?
redirect确实起作用,但在成功的cat /etc/hosts > /tmp/asdf ,容器停止。 然后运行第二个单独的容器,它不能访问第一个创build的/tmp/asdf文件。
这两个容器唯一的共同之处在于它们是从相同的映像centos运行的,但是它们具有不同的ID和不同的名称空间。
你需要运行容器,阻止它的主进程停止( -d ),然后执行容器内的命令,并引用它的名字:
docker run -d -it --name mycentos centos /bin/sh docker exec mycentos sh -c 'cat /etc/hosts > /tmp/asdf' docker exec mycentos cat /tmp/asdf
或者,更类似于真实情况,您应该将文件保存到数据卷。 然后,临时容器将访问共同的数据。
docker run centos cat < /etc/hosts什么都没有发生,我怎么可以猫容器中的主机文件?
(docker run -i centos cat) < /etc/hosts
cat /etc/hosts | docker run -i centos cat