[英]what is a bare-bones Dockerfile/docker-compose.yml to run python scripts (with specific versions of python/packages)
My laptop (Macbook) pre-installed an old version of Python (2.7).我的笔记本电脑(Macbook)预装了旧版本的 Python(2.7)。
I have a couple of different python scripts task1.py
and task2.py
that require Python 3.7 and pip install some_handy_python_package
我有几个不同的 python 脚本
task1.py
和task2.py
需要 Python 3.7 和pip install some_handy_python_package
Several online sources say updating the system-wide version of Python on a Macbook could break some (unspecified) legacy apps.一些在线消息来源称,在 Macbook 上更新 Python 的系统范围版本可能会破坏一些(未指定的)遗留应用程序。
Seems like a perfect use-case for Docker, to run some local scripts with a custom Python setup, but I do not find any online examples for this simple use case:似乎是 Docker 的完美用例,使用自定义 Python 设置运行一些本地脚本,但我没有找到这个简单用例的任何在线示例:
mystuff
has two scripts task1.py
and task2.py
(plus a Dockerfile and docker-compose.yml file)mystuff
有两个脚本task1.py
和task2.py
(加上 Dockerfile 和 docker-compose.yml 文件)pip install some_handy_python_package
pip install some_handy_python_package
docker run -it --rm some-container-name
THEN at a bash prompt 'inside' docker run the script(s) via python task1.py
docker run -it --rm some-container-name
THEN at a bash prompt 'inside' docker run the script(s) via python task1.py
docker-compose run --rm console python task1.py
docker-compose run --rm console python task1.py
I assume the Dockerfile starts off something like this:我假设 Dockerfile 是这样开始的:
FROM python:3.7
RUN pip install some_handy_python_package
but I'm not sure what to add to either the Dockerfile or a docker-compose.yml file so I can either a) run in Docker a bash prompt that lets me run python task1.py
, or b) lets me define a 'console' service that can invoke python task1.py
from the command line but I'm not sure what to add to either the Dockerfile or a docker-compose.yml file so I can either a) run in Docker a bash prompt that lets me run
python task1.py
, or b) lets me define a 'console'可以从命令行调用python task1.py
的服务
In case it helps someone else, here's a kind of basic example how to run some local-folder python scripts inside Dockerized python environment.如果它对其他人有帮助,这里有一个基本示例,如何在 Dockerized python 环境中运行一些本地文件夹 python 脚本。 (A better example would setup a volume share within the Dockerfile.)
(更好的示例是在 Dockerfile 中设置卷共享。)
cd sc2
pwd # /Users/thisisme/sc2` -- you use this path later, when run docker, to set a volume share
Create Dockerfile创建 Dockerfile
# Dockerfile
FROM python:3.7
RUN pip install some_package
Build the container, named rp
in this example:构建容器,在本例中命名为
rp
:
docker build -t rp.
In the local folder, create some python scripts, for example: task1.py在本地文件夹中,创建一些 python 脚本,例如:task1.py
# task1.py
from some_package import SomePackage
# do some stuff
In the local folder, run the script in the container by creating a app
share point:在本地文件夹中,通过创建
app
共享点运行容器中的脚本:
docker run --rm -v YOUR_PATH_TO_FOLDER:/app rp python /app/task1.py
Specifically:具体来说:
docker run --rm -v /Users/thisisme/sc2:/app rp python /app/task1.py
And sometimes it is handy to run the python interpreter in the container while developing code:有时在开发代码时在容器中运行 python 解释器很方便:
docker run -it --rm rp1
>>> 2 + 2
4
>>>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.