如何查看在Docker容器中创建的文件? [英] How to see the file created in a docker container?

查看:2376
本文介绍了如何查看在Docker容器中创建的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个python脚本,可以在文本文件中生成输出。我已经构建了一个docker映像来运行该脚本。虽然运行该映像成功完成但我看不到输出文本文件。

I have a python script which generates outputs in a text file.I have built a docker image to run the script.While running the image its completing successfully but I cant see the output text file.

我的Docker文件是

My Docker file is

From python:2.7
LABEL maintainer="ABC"
LABEL description="This Dockerfile creates MTA image"
WORKDIR /Temp
ADD script_v1.py connect.txt /Temp/
RUN pip install pexpect
CMD [ "python", "/Temp/script_v1.py" ]

在script_v1脚本中我提到了/ Temp产生输出的位置

In script_v1 script i have mentioned the /Temp location to generate he output

    sys.stdout=open("/Temp/EXPORT_OP.txt","w")

我正在Linux容器模式下从Windows docker host运行映像。

I am running the image from windows docker host in Linux container mode.

推荐答案

我看到至少两种简单的方法可以实现所需的目标:

I see at least two simple ways of achieving what you want:


  1. 使用绑定挂载。简而言之,您将使主机文件系统中的目录可用于容器,并使用其路径保存文件。输出将在该文件夹中提供。

  1. Use a bind mount. In short, you'll make a directory in your host filesystem available to the container, and you'll use its path to save the file. The output will be available in that folder.

使用 docker cp ,在容器退出后将文件从容器内的位置复制到主机文件系统中。

Use docker cp after the container exits to copy the file from a location inside the container into your host filesystem.

另一种不会使文件直接在您的主机文件系统上可用的方法是 docker exec -it<容器> bash ,这将在容器内为您提供一个开放的终端,您可以使用它将 cd 放入正确的目录中,并 cat 文件。

Another way, which will not make the file available directly on your host filesystem, will be to docker exec -it <container> bash, which will leave you with an open terminal inside the container, which you can use to cd into the correct directory and cat the file.

这篇关于如何查看在Docker容器中创建的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆