简体   繁体   English

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

[英]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 ,这样您就可以在没有额外的executableslibraries情况下运行它。

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的路径。

Docker-compose码头工人组成

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

Dockerfile Dockerfile

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

Docker-compose码头工人组成

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.

相关问题 无法执行脚本 docker-compose Laravel - Failed to execute script docker-compose Laravel 当我尝试 docker-compose 时无法执行脚本 docker-compose - Failed to execute script docker-compose when I try docker-compose up TesseractNotFoundError:两个docker容器python应用程序(docker-compose) - TesseractNotFoundError: two docker container python app (docker-compose) docker-compose exec导致[Errno 2]没有此类文件或目录:docker容器中的'docker-compose':'docker-compose' - docker-compose exec causes [Errno 2] No such file or directory: 'docker-compose': 'docker-compose' in docker container PermissionError: [WinError 5] Access Denied: [4776] 运行 docker-compose up 时无法执行脚本 docker-compose - PermissionError: [WinError 5] Access Denied: [4776] Failed to execute script docker-compose when running docker-compose up 如何在 docker-compose 之后在 shell 脚本中运行 python 文件? - How to run python file after docker-compose in a shell script? 通过 docker-compose 调用 python 脚本的 Crontab 未执行 - Crontab that calls python script via docker-compose not executing 当使用 docker-compose 'command:' 运行时,Supervisord 在 Docker 容器内退出,但从 bash 运行时不会退出 - Supervisord exits inside Docker container when run with docker-compose 'command:', but not when its run from bash 使用 docker-compose 启动 Jupyter notebook 时激活 Docker 容器内的 conda env - Activating a conda env inside a Docker container when using docker-compose to start Jupyter notebook Docker-compose没有建立一个容器 - Docker-compose is not building one container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM