简体   繁体   English

没有Web服务器的Python脚本运行到Docker?

[英]Run Python script without web server into Docker?

I have been working with Docker previously using services to run a website made with Django. 我以前一直在使用Docker使用服务来运行用Django制作的网站。

Now I would like to know how I should create a Docker to just run Python scripts without a web server and any service related with websited. 现在我想知道如何创建一个Docker来运行没有Web服务器的Python脚本以及任何与websited相关的服务。

An example of normal docker which I am used to work is: 我以前工作的普通码头工人的一个例子是:

version: '2'
services:
  nginx:
    image: nginx:latest
    container_name: nz01
    ports:
      - "8001:8000"
    volumes:
      - ./src:/src
      - ./config/nginx:/etc/nginx/conf.d
    depends_on:
      - web
  web:
    build: .
    container_name: dz01
    depends_on:
      - db
    volumes:
      - ./src:/src
    expose:
      - "8000"
  db:
    image: postgres:latest
    container_name: pz01
    ports:
        - "5433:5432"
    volumes:
      - postgres_database:/var/lib/postgresql/data:Z
volumes:
    postgres_database:
        external: true

How should be the docker-compose.yml file? docker-compose.yml文件应该如何?

Simply remove everything from your Dockerfile that has nothing to do with your script and start with something simple, like 只需从Dockerfile中删除与脚本无关的所有内容,并从简单的内容开始,例如

FROM python:3

ADD my_script.py /

CMD [ "python", "./my_script.py" ]

You do not need Docker compose for containerizing a single python script. 您不需要Docker compose来容纳单个python脚本。

The example is taken from this simple tutorial about containerizing Python applications: https://runnable.com/docker/python/dockerize-your-python-application 这个例子来自这个关于容器化Python应用程序的简单教程: https//runnable.com/docker/python/dockerize-your-python-application

You can easily overwrite the command specified in the Dockerfile (via CMD ) when starting a container from the image. 从映像启动容器时,可以轻松覆盖Dockerfile中指定的命令(通过CMD )。 Just append the desired command to your docker run command, eg: 只需将所需命令附加到docker run命令,例如:

docker run IMAGE /path/to/script.py

You can easily run Python interactively without even having to build a container: 您可以轻松地以交互方式运行Python,甚至无需构建容器:

docker run -it python

If you want to have access to some code you have written within the container, simply change that to: 如果您想要访问容器中编写的某些代码,只需将其更改为:

docker run -it -v /path/to/code:/app: python

Making a Dockerfile is unnecessary for this simple application. 这个简单的应用程序不需要制作Dockerfile。

Most Linux distributions come with Python preinstalled. 大多数Linux发行版都预装了Python。 Using Docker here adds significant complexity and I'd pretty strongly advise against Docker just to run a simple script. 在这里使用Docker会增加显着的复杂性,我非常强烈建议不要使用Docker来运行一个简单的脚本。 You can use a virtual environment to isolate a particular Python package's dependencies from the rest of the system. 您可以使用虚拟环境将特定Python包的依赖关系与系统的其余部分隔离开来。

(There is a pretty consistent stream of SO questions around getting filesystem permissions and user IDs right for scripts that principally want to interact with the host system. Also remember that running docker anything implies root-equivalent permissions. If you don't want Docker's filesystem and user namespace isolation, IMHO it's easier to just not use Docker where it doesn't make sense.) (对于主要希望与主机系统交互的脚本,获得文件系统权限和用户ID的问题非常一致。还要记住,运行docker任何东西都意味着root等价权限。如果你不想要Docker的文件系统和用户名称空间隔离,恕我直言,更容易不使用Docker,这是没有意义的。)

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

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