gpt4 book ai didi

docker - 关于Docker文件系统的奥秘

转载 作者:行者123 更新时间:2023-12-02 20:37:58 25 4
gpt4 key购买 nike

刚刚开始使用docker。我已经创建了一个镜像,启动了一个容器,但是当我在容器中写入文件时,它会显示在本地磁盘上。我很奇怪为什么会这样,如果那样,我怎么可能运行图像的许多实例。

这是我的工作

docker container ls
CONTAINER ID IMAGE COMMAND
CREATED STATUS PORTS NAMES
5bf6d45b4f79 docker_1_web "bundle exec rails s…" About an hour ago Up About an hour 0.0.0.0:3000->3000/tcp docker_1_web_1
d5d0991da014 postgres "docker-entrypoint.s…" About an hour ago Up About an hour 5432/tcp docker_1_db_1

然后
docker exec -it 5bf6d45b4f79 echo "hello" > temp.txt


docker exec -it 5bf6d45b4f79 cat temp.txt
hello

但是在我的Mac命令行中的本地磁盘上
cat temp.txt
hello

在我的Mac命令行上
echo "goodbye" >> temp.txt
cat temp.txt
hello
goodbye

然后在容器中
docker exec -it 5bf6d45b4f79 cat temp.txt
hello
goodbye

h!怎么来的?我以为Docker容器写入了自己的文件系统。所以我完全迷惑了。

这也意味着我无法运行图像的多个实例,因为它们都将写入相同的文件。

最佳答案

让我们澄清一下:

When you do:

docker exec -it 5bf6d45b4f79 echo "hello" > temp.txt
docker exec -it 5bf6d45b4f79 cat temp.txt
hello

You're executing echo "hello" in your container but stdoutput is stored in your host. So, is normal that you see temp.txt in your host.



如果要在容器中保存temp.txt,请尝试:
docker exec -it 5bf6d45b4f79 bash -c "echo 'hello' > temp.txt"

并且temp.txt将在您的容器workdir上生成。

When you do:

But on my local disk in my Mac command line

cat temp.txt hello Followed on my Mac command line by

echo "goodbye" >> temp.txt cat temp.txt hello goodbye

And then in the container

docker exec -it 5bf6d45b4f79 cat temp.txt hello goodbye

You're sharing temp.txt, maybe it's generated in a docker volume shared between host and docker

关于docker - 关于Docker文件系统的奥秘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50985917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com