简体   繁体   English

Docker 容器不执行 Python 命令

[英]Docker Container Not Executing Python Commands

I have a simple script writecsv.py which queries an external API, parses the response, and writes to 2 CSVs.我有一个简单的脚本writecsv.py ,它查询外部 API,解析响应,并写入 2 个 CSV。

My Dockerfile reads:我的 Dockerfile 显示:

FROM python:3.8.5-slim-buster
COPY . /src
RUN pip install -r /src/requirements.txt
CMD ["python", "./src/writecsv.py"]

In the directory containing my Dockerfile, I have 4 files:在包含我的 Dockerfile 的目录中,我有 4 个文件:

writecsv.py # My script that queries API and writes 2 csvs
keys.yaml # Stores my API keys which are read by writecsv.py
requirements.txt 
Dockerfile

When I build this image, I use docker build -t write-to-csv-application.当我构建这个图像时,我使用docker build -t write-to-csv-application. and to run this image, I use docker run write-to-csv-application为了运行这个图像,我使用docker run write-to-csv-application

I am able to show that the script runs, and the 2 CSV files are successfully created by printing the contents of the current working directory before and after calling csv.DictWriter.我能够显示脚本运行,并且通过在调用 csv.DictWriter 之前和之后打印当前工作目录的内容,成功创建了 2 个 CSV 文件。

So far, so good.到目前为止,一切都很好。 Now I'd like to expose these files on localhost:5000, to be downloaded.现在我想在 localhost:5000 上公开这些文件,以供下载。

My current approach isn't working.我目前的方法不起作用。 I don't know Docker very well, so any suggestions are welcomed.我不太了解Docker,所以欢迎任何建议。 Here's where things go wrong:这是 go 出错的地方:

I then add 2 more lines to my Dockerfile;然后我在 Dockerfile 中再添加 2 行; expose 5000, and http.server to get:公开 5000 和 http.server 得到:

FROM python:3.8.5-slim-buster
COPY . /src
RUN pip install -r /src/requirements.txt
EXPOSE 5000/tcp
CMD ["python", "./src/writecsv.py"]
CMD python -m http.server -dp ./src/ 5000

Now when I build this image, again using docker build -t write-to-csv-application.现在,当我构建此图像时,再次使用docker build -t write-to-csv-application. and run this image using docker run -p 5000:5000 write-to-csv-application I don't get any command line output from the writecsv.py program, that I did previously.并使用docker run -p 5000:5000 write-to-csv-application运行此图像我没有从 writecsv.py 程序中获得任何命令行 output,这是我之前所做的。 I am able to access localhost:5000 and see the image file structure, but I find that the files weren't created.我可以访问 localhost:5000 并查看图像文件结构,但我发现文件没有创建。 The output in command line hangs indefinitely (which I would expect, as I don't have anything to terminate the http server.)命令行中的 output 无限期挂起(我希望如此,因为我没有任何东西可以终止 http 服务器。)

What I've tried:我试过的:

  • I wrote the files to./src/data/ and pointed the http.server to /src/data/, which doesn't exist.我将文件写入./src/data/,并将 http.server 指向 /src/data/,它不存在。
  • I pointed the http.server to./ and checked the entire file structure: they aren't being written anywhere when ran with docker run -p 5000:5000 write-to-csv-application我将 http.server 指向./ 并检查了整个文件结构:当使用docker run -p 5000:5000 write-to-csv-application时,它们没有被写入任何地方

This worked for me.这对我有用。 I neglected the requirements.txt.我忽略了 requirements.txt。 I just made a very basic example.我只是做了一个非常基本的例子。

"The main purpose of a CMD is to provide defaults for an executing container". “CMD 的主要目的是为执行容器提供默认值”。 See the official docs for more information.有关详细信息,请参阅官方文档

writecsv.py: writecsv.py:

import csv 
    
fields = ['Name', 'Branch', 'Year', 'CGPA'] 
    
rows = [ ['Nikhil', 'COE', '2', '9.0'], 
         ['Sanchit', 'COE', '2', '9.1'], 
         ['Aditya', 'IT', '2', '9.3'], 
         ['Sagar', 'SE', '1', '9.5'], 
         ['Prateek', 'MCE', '3', '7.8'], 
         ['Sahil', 'EP', '2', '9.1']] 
    
filename = "university_records.csv"
    
with open(filename, 'w') as csvfile: 
    csvwriter = csv.writer(csvfile) 
    csvwriter.writerow(fields) 
    csvwriter.writerows(rows)

Dockerfile: Dockerfile:

FROM python:3.8.5-slim-buster
WORKDIR /src
COPY . /src
EXPOSE 5000/tcp
RUN ["python", "./writecsv.py"]
CMD python -m http.server -d . 5000
docker build -t python-test . 
docker run --rm -it -p 5000:5000 --name python-test python-test

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

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