在给定的docker-compose容器中执行Python脚本

我已经做了一个python脚本来创build一个DB和RethinkDB里面的一些表

但是现在我正试图在用docker-compose启动的docker-compose容器中启动这个python脚本。

这是我的docker-compose.yml重新考虑容器configuration

 # Rethink DB rethink: image: rethinkdb:latest container_name: rethink ports: - 58080:8080 - 58015:28015 - 59015:29015 

我试图在启动我的容器后执行脚本

 docker exec -it rethink python src/app/db-install.py 

但是我得到这个错误

rpc错误:code = 2 desc = oci运行时错误:exec失败:exec:“python”:可执行文件找不到$ PATH

在我的容器中找不到Python。 这是可能的执行一个给定的容器内使用docker-composedocker exec的python脚本?

rethinkdb图像基于debian:jessie image:

https://github.com/rethinkdb/rethinkdb-dockerfiles/blob/da98484fc73485fe7780546903d01dcbcd931673/jessie/2.3.5/Dockerfile

debian:jessie image没有安装python。

所以你需要创build你自己的Dockerfile,如下所示:

 FROM rethinkdb:latest RUN apt-get update && apt-get install -y python 

然后改变你的docker组成:

 # Rethink DB rethink: build : . container_name: rethink ports: - 58080:8080 - 58015:28015 - 59015:29015 

build : . 是Dockerfile的path。

首先找出你的container是否有python可执行文件:

 docker exec -it rethink which python 

如果存在,则使用上一步中由which命令提供的absolute path

 docker exec -it rethink /absolute/path/to/python src/app/db-install.py 

如果没有,你可以将你的python script转换成bash script ,这样你就可以在没有额外的executableslibraries情况下运行它。

或者你可以创build一个dockerfile ,使用base image ,并安装python

dockerfile:

 FROM rethinkdb:latest RUN apt-get update && apt-get install -y python 

Docker撰写文件:

 rethink: build : . container_name: rethink ports: - 58080:8080 - 58015:28015 - 59015:29015