简体   繁体   中英

Running a python script inside an nginx docker container

I'm using nginx to serve some of my docs. I have a python script that processes these docs for me. 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. What I want is to run my python (and bash) scripts inside the nginx container and have nginx just serve those docs. 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:

/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? I was thinking of installing python3 using:

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

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. You can regenerate the data from the host , outside Docker, the same way you would if Docker weren't involved

# 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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