[英]Execute Python script inside a given docker-compose container
I have made a little python script to create a DB and some tables inside a RethinkDB我制作了一个小 Python 脚本来在 RethinkDB 中创建一个数据库和一些表
But now I'm trying to launch this python script inside my rethink
container launched with docker-compose
.但现在我试图在我用
docker-compose
启动的rethink
容器中启动这个 python 脚本。
This is my docker-compose.yml rethink container config这是我的 docker-compose.yml 重新思考容器配置
# Rethink DB
rethink:
image: rethinkdb:latest
container_name: rethink
ports:
- 58080:8080
- 58015:28015
- 59015:29015
I'm trying to execute the script with after launching my container我正在尝试在启动我的容器后执行脚本
docker exec -it rethink python src/app/db-install.py
But I get this error但我得到这个错误
rpc error: code = 2 desc = oci runtime error: exec failed: exec: "python": executable file not found in $PATH
rpc 错误:代码 = 2 desc = oci 运行时错误:执行失败:执行:“python”:$PATH 中找不到可执行文件
Python is not found in me container.在我的容器中找不到 Python。 Is this possible to execute a python script inside a given container with
docker-compose
or with docker exec
?这是否可以使用
docker-compose
或使用docker exec
在给定容器内执行 python 脚本?
First find out if you have python
executable in the container
: 首先找出
container
是否有python
可执行文件:
docker exec -it rethink which python
If it exists, Use the absolute path
provided by which
command in previous step: 如果存在,请使用上一步中由
which
命令提供的absolute path
:
docker exec -it rethink /absolute/path/to/python src/app/db-install.py
If not, you can convert your python script
to bash script
, so you can run it without extra executables
and libraries
. 如果没有,您可以将
python script
转换为bash script
,这样您就可以在没有额外的executables
和libraries
情况下运行它。
Or you can create a dockerfile
, use base image
, and install python
. 或者您可以创建
dockerfile
,使用base image
,然后安装python
。
dockerfile: dockerfile:
FROM rethinkdb:latest
RUN apt-get update && apt-get install -y python
Docker Compose file: Docker撰写文件:
rethink:
build : .
container_name: rethink
ports:
- 58080:8080
- 58015:28015
- 59015:29015
The rethinkdb image is based on the debian:jessie image : rethinkdb图像基于debian:jessie图像:
https://github.com/rethinkdb/rethinkdb-dockerfiles/blob/da98484fc73485fe7780546903d01dcbcd931673/jessie/2.3.5/Dockerfile https://github.com/rethinkdb/rethinkdb-dockerfiles/blob/da98484fc73485fe7780546903d01dcbcd931673/jessie/2.3.5/Dockerfile
The debian:jessie image does not come with python installed. debian:jessie图像没有安装python。
So you will need to create your own Dockerfile, something like : 所以你需要创建自己的Dockerfile,例如:
FROM rethinkdb:latest
RUN apt-get update && apt-get install -y python
Then change your docker-compose : 然后改变您的docker-compose:
# Rethink DB
rethink:
build : .
container_name: rethink
ports:
- 58080:8080
- 58015:28015
- 59015:29015
build : .
is the path to your Dockerfile. 是Dockerfile的路径。
Assuming that python is installed, try:假设已安装 python,请尝试:
docker-compose run --rm MY_DOCKER_COMPOSE_SERVICE MY_PYTHON_COMMAND
For a start, you might also just go into the shell at first and run a python script from the command prompt.首先,您也可以先进入 shell 并从命令提示符运行 python 脚本。
docker-compose run --rm MY_DOCKER_COMPOSE_SERVICE bash
In your case, MY_DOCKER_COMPOSE_SERVICE is 'rethink', and that is not the container name here, but the name of the service (first line rethink:
), and only the service is run with docker-compose run
, not the container.在您的情况下, MY_DOCKER_COMPOSE_SERVICE 是“重新思考”,这不是这里的容器名称,而是服务的名称(第一行
rethink:
:),并且只有服务使用docker-compose run
,而不是容器。
The MY_PYTHON_COMMAND is, in your case of Python2, python src/app/db-install.py
, but in Python3 it is python -m src/app/db-install
(without the ".py"), or, if you have Python3 and Python2 installed, python3 -m src/app/db-install
. MY_PYTHON_COMMAND 在 Python2 的情况下是
python src/app/db-install.py
,但在 Python3 中是python -m src/app/db-install
(没有“.py”),或者,如果你有Python3 和 Python2 已安装, python3 -m src/app/db-install
。
To be able to run this python command, the Python file needs to be in the container.为了能够运行这个 python 命令,Python 文件需要在容器中。 Therefore, in your Dockerfile that you need to call with
build: .
因此,在您需要使用
build: .
, you need to copy your build directory to a directory in the container of your choice ,您需要将构建目录复制到您选择的容器中的目录
COPY $PROJECT_PATH /tmp
This /tmp
will be created in your build directory.此
/tmp
将在您的构建目录中创建。 If you just write ".", you do not have any subfolder and save it directly in the build directory.如果只写“.”,则没有任何子文件夹,直接保存在构建目录中。
When using /tmp
as the subfolder, you might write at the end of your Dockerfile:当使用
/tmp
作为子文件夹时,您可以在 Dockerfile 的末尾写:
WORKDIR /tmp
Or if you do not change the WORKDIR
from the build (" .
") context to /tmp
and you still want to reach /tmp
, run your Python file like /tmp/db-install.py
.或者,如果您没有将构建(“
.
”)上下文中的WORKDIR
更改为/tmp
并且您仍想访问/tmp
,请运行您的 Python 文件,例如/tmp/db-install.py
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.