简体   繁体   English

在容器内运行Docker脚本应用程序

[英]Running Docker script application inside a container

I'm new to Docker and containers - I've got a Python 3 script that pulls data from a web site based on command line parameters and prints the results. 我是Docker和容器的新手-我有一个Python 3脚本,该脚本根据命令行参数从网站中提取数据并打印结果。 It uses Redis for caching data if there's no network connection available. 如果没有可用的网络连接,它将使用Redis缓存数据。 The Dockerfile I have looks like this 我拥有的Dockerfile看起来像这样

FROM ubuntu:latest
RUN apt-get update -y && apt-get -y install redis-server && apt-get install -y python3-pip python3.5-dev build-essential
COPY . /app
WORKDIR /app
RUN pip3 install -r requirements.txt
ENTRYPOINT ["python3"]
CMD ["pn.py"]

I'm able to build the image, but when I start the container it presumably runs the Python command to run the script inside the container and exits, and as a consquence I cannot see the results from the host. 我能够构建图像,但是当我启动容器时,它大概运行了Python命令来在容器内运行脚本并退出,因此,我无法从主机看到结果。

The script requires command line inputs, otherwise it will fail. 该脚本需要命令行输入,否则将失败。 How can these be specified as part of a single Docker run command I can use to run the script from the host? 如何将它们指定为我可以用来从主机运行脚本的单个Docker运行命令的一部分?

Alternatively how can I start the container without calling the script, and instead run the script manually inside the running container? 或者,如何在不调用脚本的情况下启动容器,而是在正在运行的容器中手动运行脚本?

It depends on what you want to do with the results. 这取决于您要如何处理结果。

Assume the following: 假设以下内容:

  • the ENTRYPOINT line tells the image what should be executed when you start the container. ENTRYPOINT行告诉图像启动容器时应执行的操作。 That will be PID 1 . 那将是PID 1
  • when that line exists, the CMD content is used as argument(s) to the ENTRYPOINT . 当该行存在时, CMD内容用作ENTRYPOINT
  • docker containers always exit when their main task (PID 1) exits. docker容器始终在其主要任务(PID 1)退出时退出。

Basically your docker is doing what it is told to do. 基本上,您的码头工人正在执行被告知要做的事情。 If you want it to keep running, you should give a task that won't exit as ENTRYPOINT or CMD (if ENTRYPOINT was not present). 如果希望它继续运行,则应给出不会以ENTRYPOINTCMD退出的任务(如果不存在ENTRYPOINT )。

For manually start the container on a shell mode and see what happens, you can run it as: 要以shell模式手动启动容器并查看会发生什么,可以按以下方式运行它:

docker run -ti [OPTIONS] [CONTAINER_NAME] bash

and then you'll be in a shell "into" the container, so you can run the script manually: 然后您将进入一个“进入”容器的外壳程序中,因此可以手动运行脚本:

#~ python3 pn.py

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

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