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.

  • Python web application project structure and docker support
  • Access all usb from docker container in docker native osx
  • Setting Docker Variables
  • .pgpass for PostgreSQL replication in Dockerized environment
  • Can't connect to django server running inside docker container (Docker for mac)
  • DNS failover with Docker Swarm 1.12
  • 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 ?

  • Running docker on Ubuntu: mounted host volume is not writable from container
  • Restart Docker Container (Automatically) when Image changes in Portainer(?)
  • Docker proxy with custom networks
  • Docker Container compared with Unikernel
  • mongod --bind_ip using docker-compose version 2
  • how to set docker container hostname/IP permanently
  • 2 Solutions collect form web for “Execute Python script inside a given docker-compose container”

    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.

    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
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.