简体   繁体   English

使用Docker for Windows从Python脚本创建Docker映像时无法获取数据输出文件

[英]Not able to get output file with data while creating a docker image from a Python script using Docker for windows

I am using Docker for windows with Linux containers, I have created a simple python script where I need to take input from 2 text files and append them and export them into a text file. 我将Docker用于带有Linux容器的Windows,我创建了一个简单的python脚本,需要从2个文本文件中获取输入并将它们附加并导出到文本文件中。 Below is the code for the test_script.py 以下是test_script.py的代码

#including libraries
import pandas as pd
from os import path

#setting path to data
path2data1 = './data1'
path2data2 = './data2'
path2output = './'

#reading input file
input_data1 = pd.read_table(path.join(path2data1,"sample_data_input1.txt"))
input_data2 = pd.read_table(path.join(path2data2,"sample_data_input2.txt"))

#adding both the data
combined_data = input_data1.append(input_data2, ignore_index = True)

#print data in a output file
combined_data.to_csv(path.join(path2output, 'outputdata.csv'), 
                   header=True, index=False, encoding='utf-8')

Now I am trying to create a docker container with this, I want to just pass the folder location as the data keeps on changing everyday. 现在,我试图以此创建一个docker容器,我想只传递文件夹位置,因为数据每天都在变化。 Also I want the output file after running the docker image. 我也想在运行docker镜像后输出文件。

I wrote the following Dockerfile 我写了以下Dockerfile

# Use an official Python runtime as a parent image
FROM python:3
ENV http_proxy http://proxy-chain.xxx.com:911/
ENV https_proxy http://proxy-chain.xxx.com:912/


COPY . /app
WORKDIR /app/

# Install any needed packages specified
RUN pip install pandas

# Run test_script.py when the container launches
CMD ["python", "test_script.py"] 

So I am building the docker image using docker build -t test_build . 所以我正在使用docker build -t test_build . . It is building successfully without any error. 它正在成功构建,没有任何错误。

I am running the image with docker run --volume ./test_script.py:/test_script.py test_build > ./output.txt then It is creating the output file but that is coming to be empty. 我正在使用docker run --volume ./test_script.py:/test_script.py test_build > ./output.txt运行映像,然后创建输出文件,但是该文件docker run --volume ./test_script.py:/test_script.py test_build > ./output.txt空。

How can I get the data along with the file 如何获取数据和文件

@archit you need to attach a volume to your docker. @archit,您需要将卷附加到docker。
A volume is the only way that you can persist your output file and also the way your docker will get the input file to run on every time you want to use the docker. 卷是您可以持久保存输出文件的唯一方法,也是您每次使用docker时docker将使输入文件运行的唯一方式。

docker run \
  -v host_volume:/app \
  test_build

In it you should put your input file that you want the docker to use, not your script, that one you added when you built the docker. 在其中应放置要让Docker使用的输入文件,而不是您在构建Docker时添加的脚本的脚本。

I suggest one of two things: 我建议以下两件事之一:

  1. Change your code to take the most update input file in the volume directory and execute it, that way you don't need to pass it any params every time you run it. 更改代码以获取卷目录中最新的输入文件并执行它,这样就不必每次运行时都将其传递给任何参数。
  2. Change your docker file from CMD to ENTRYPOINT . ENTRYPOINT文件从CMD更改为ENTRYPOINT
    Then when you run it you can do this: 然后,当您运行它时,您可以执行以下操作:
    docker run -it -v path_in_your_comp:path_inside_your_docker test_build path_inside_your_docker/input_file_name path_inside_your_docker/output_file_name
    You need to have your python script able to read this params when you start it, via the command args. 启动时,您需要使python脚本能够通过args命令读取此参数。 keep in mind that the path is the name you mapped the volume inside your docker. 请记住,路径是您在docker内部映射卷的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM