[英]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: 我建议以下两件事之一:
CMD
to ENTRYPOINT
. 将ENTRYPOINT
文件从CMD
更改为ENTRYPOINT
。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.