简体   繁体   English

在 nginx docker 容器内运行 python 脚本

[英]Running a python script inside an nginx docker container

I'm using nginx to serve some of my docs.我正在使用 nginx 来提供我的一些文档。 I have a python script that processes these docs for me.我有一个为我处理这些文档的 python 脚本。 I don't want to pre-process the docs and then add them in before the docker container is built since these docs can grow to be pretty big and they increase in number.我不想预处理文档,然后在构建 docker 容器之前添加它们,因为这些文档可能会变得非常大并且数量会增加。 What I want is to run my python (and bash) scripts inside the nginx container and have nginx just serve those docs.我想要的是在 nginx 容器内运行我的 python(和 bash)脚本,并让 nginx 只为这些文档提供服务。 Is there a way to do this without pre-processing the docs before building the container?有没有办法在构建容器之前不对文档进行预处理? I've attempted to execute RUN python3 process_docs.py , but I keep seeing the following error:我试图执行RUN python3 process_docs.py ,但我一直看到以下错误:

/bin/sh: 1: python: not found
The command '/bin/sh -c python process_docs.py' returned a non-zero code: 127

Is there a way to get python3 onto the Nginx docker container?有没有办法让 python3 进入 Nginx docker 容器? I was thinking of installing python3 using:我正在考虑使用以下方法安装 python3:

apt-get update -y
apt-get install python3.6 -y

but I'm not sure that this would be good practice.但我不确定这是否是一个好习惯。 Please let me know the best way to run my pre processing script.请让我知道运行我的预处理脚本的最佳方式。

You can use a bind mount to inject data from your host system into the container.您可以使用绑定挂载将数据从主机系统注入到容器中。 This will automatically update itself when the host data changes.这将在主机数据更改时自动更新。 If you're running this in Docker Compose, the syntax looks like如果你在 Docker Compose 中运行它,语法看起来像

version: '3.8'
services:
  nginx:
    image: nginx
    volumes:
      - ./html:/usr/share/nginx/html
      - ./data:/usr/share/nginx/html/data
    ports:
      - '8000:80'  # access via http://localhost:8000

In this sample setup, the html directory holds your static assets (checked into source control) and the data directory holds the generated data.在此示例设置中, html目录保存您的 static 资产(签入源代码管理), data目录保存生成的数据。 You can regenerate the data from the host , outside Docker, the same way you would if Docker weren't involved您可以在 Docker 之外从主机重新生成数据,就像不涉及 Docker 时一样

# on the host
. venv/bin/activate
./regenerate_data.py --output-directory ./data

You should not need docker exec in normal operation, though it can be extremely useful as a debugging tool.在正常操作中您不需要docker exec ,尽管它作为调试工具非常有用。 Conceptually it might help to think of a container as identical to a process;从概念上讲,将容器视为与流程相同可能会有所帮助。 if you ask "can I run this Python script inside the Nginx process ", no, you should generally run it somewhere else.如果您问“我可以在 Nginx进程中运行此 Python 脚本吗”,不,您通常应该在其他地方运行它。

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

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