Jérôme
Jérôme

Reputation: 2056

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

But now I'm trying to launch this python script inside my rethink container launched with docker-compose.

This is my docker-compose.yml rethink container config

# 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

Python is not found in me container. Is this possible to execute a python script inside a given container with docker-compose or with docker exec ?

Upvotes: 3

Views: 13663

Answers (3)

questionto42
questionto42

Reputation: 9630

Docker-compose

Assuming that python is installed, try:

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.

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.

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.

Dockerfile

To be able to run this python command, the Python file needs to be in the container. Therefore, in your Dockerfile that you need to call with 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. 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:

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.

Upvotes: 1

Farhad Farahi
Farhad Farahi

Reputation: 39527

First find out if you have python executable in the container:

docker exec -it rethink which python

If it exists, Use the absolute path provided by which command in previous step:

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.

Or you can create a dockerfile, use base image, and install python.

dockerfile:

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

Docker Compose file:

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

Upvotes: 3

Hakro
Hakro

Reputation: 2774

The rethinkdb image is based on the debian:jessie image :

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

The debian:jessie image does not come with python installed.

So you will need to create your own Dockerfile, something like :

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

Then change your docker-compose :

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

build : . is the path to your Dockerfile.

Upvotes: 0

Related Questions