如何使用Python将文件保存在Docker容器外部。

huangapple go评论72阅读模式
英文:

how to save a file outside the docker container with python

问题

以下是您要翻译的部分:

"我有这个结构:

myapp

  • Dockerfile
  • main.py
  • req.txt

这是我的Dockerfile的内容:

FROM python:3.8
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
COPY . /app
WORKDIR /app
RUN pip3 install -r req.txt
RUN playwright install
RUN playwright install-deps

CMD ["python", "./main.py"]

而在我的main.py中,最终我有这段代码

 current_path = os.path.dirname(os.path.abspath(__file__))
 result_path = os.path.join(current_path, 'result.json')

 with open(result_path, 'w') as json_file:
    json.dump(result, json_file, indent=4)

我可以看到脚本成功运行,但我看不到应该在同一文件夹中创建的文件,我理解文件正在创建在

> /app/results.json

但有没有办法让这个文件在容器之外创建?

编辑:
我已经使用以下命令构建了我的容器:

docker build -t myapp .

然后像这样运行:

docker run myapp
英文:

So I have this structure:

myapp

  • Dockerfile
  • main.py
  • req.txt

this is the content of my Dockerfile:

FROM python:3.8
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
COPY . /app
WORKDIR /app
RUN pip3 install -r req.txt
RUN playwright install
RUN playwright install-deps

CMD ["python", "./main.py"]

and on my main.py, eventually, I have this code

 current_path = os.path.dirname(os.path.abspath(__file__))
 result_path = os.path.join(current_path, 'result.json')

 with open(result_path, 'w') as json_file:
    json.dump(result, json_file, indent=4)

I can see that the script is running successfully, but I can't see the file that was supposed to be created in the same folder, I understand that the file is being created at

> /app/results.json

but there's a way for this file to be created outside the container?

EDIT:
I have done my build with:

docker build -t myapp .

and I run like this:

docker run myapp

答案1

得分: 2

在docker run中使用绑定挂载

docker run -v /path/to/host_app_dir:/app myapp

这将绑定两个目录:在你的主机系统中的/path/to/host_app_dir和docker内部的/app文件夹。任何写入/app的文件都会出现在/path/to/host_app_dir中。它们实际上是硬盘上的同一个文件夹。这就是绑定挂载的魔力。

请注意,在docker构建过程中将某些内容复制到/app文件夹中(COPY . /app)。使用上述绑定挂载选项将替换/app的内容为主机文件夹的内容。请参阅文档

我建议您使用不同的文件夹而不是/app来在主机和容器之间共享文件。

英文:

Use bind mount at docker run:

docker run -v /path/to/host_app_dir:/app  myapp

This will bind two directories: the /path/to/host_app_dir in your host system and the /app folder inside docker. Any file written to /app will appear in /path/to/host_app_dir. They are essentially the same folder on your hard disk. That is the magic of bind mount.

Be careful, because you copy something into the /app folder (COPY . /app) during docker build. The content of /app will be replaced by the content of your host folder using the bind mount option above. See the documentation.

I suggest that you use a different folder instead of /app to share files between the host and container.

答案2

得分: 2

目前为止,你运行应用的方式是完全正确的。

在Docker中,要将文件保存到主机文件系统上,有一个概念叫做挂载卷,通过这个概念,可以将主机上的文件或目录直接挂载到正在运行的容器中。

所以现在我不知道你希望将文件写到主机的哪个位置,但一般的命令会是这样的:

docker run -v app:/app -it myapp

这将主机上的app文件夹挂载到容器中的app文件夹。

关于同一主题的更多阅读:
https://docs.docker.com/storage/bind-mounts/

希望这有所帮助。谢谢。

英文:

So far so good, the way you are running app is perfectly alright.

So in Docker to save files on host's file system theres is a concept called mounting the volume from which file or directory of host can be directly mounted to your running container.

So right now I do not know where you want that file to be written on host. but the general command will be like this:

docker run -v app:/app -it myapp

This mounts app folder of your host to app folder in the container

More read on the same topic:
https://docs.docker.com/storage/bind-mounts/

Hope this helps. Thanks.

答案3

得分: 0

你需要使用 -v 选项将你的机器上的一个文件夹挂载到容器内的一个文件夹。在这个文件夹内创建的文件将位于容器之外。

英文:

You need to mount bind a folder on your machine to a folder inside the container with the-v option.
Files created inside this folder are then outside of the container.

huangapple
  • 本文由 发表于 2023年5月22日 12:47:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76303103.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定