user6723321
user6723321

Reputation:

Simple Python Docker Script

I have a very simple python script. I am trying to get it to run from within docker. I have simplified it so it just prints hello world now.

My docker set up:

[docker-compose.yaml]

version: '2'
services:
   dev:
     build: .
     volumes:
        - ./app.py:/app.py

[Dockerfile]

FROM ubuntu
RUN apt-get update -y
RUN apt-get install -y python python-pip python-dev build-essential libpq-dev


ADD ./requirements /code/requirements

RUN pip install --upgrade pip

RUN pip install -r /code/requirements/base.txt

VOLUME /app.py

CMD ["python","/app.py"]

When I run docker-compose up I get the following error:

ERROR: for dev Cannot start service dev: oci runtime error: exec: "Python": executable file not found in $PATH

However, docker should be built into the Ubuntu Image. It is the same image I use for my Python Flask script but that works no issue.

Upvotes: 2

Views: 5640

Answers (3)

Lorenzo Vitali
Lorenzo Vitali

Reputation: 340

I had the same issue but with a different settings, with apt-get I installed python3 and I used the command python, so it was not seeing the path to the python3 bin because it was looking for python.

What I suggest is to try the same thing I did with python3 installing it and all the packages you need


FROM ubuntu:16.04

RUN apt-get update -y && apt-get install -y python3-pip python3-dev build-essential

COPY . /usr/src/app WORKDIR /usr/src/app

CMD ["python3", "file.py"]


Upvotes: 2

Gabriel Fernandez
Gabriel Fernandez

Reputation: 660

It seems that the python you have installed can't be found by ubuntu. I suggest adding to your dockerfile (after the line that installs python):

RUN "PYTHONPATH=/usr/lib/python2.7" >> ~/.bashrc
CMD source ~/.bashrc && <other commands>

Or

RUN "PYTHONPATH=/usr/lib/python3.4" >> ~/.bashrc
CMD source ~/.bashrc && <other commands>

If you would rather use Python3

That will add your python path to your bash register file and have it take effect.

Upvotes: 2

salehinejad
salehinejad

Reputation: 7378

Since you just want to print a 'Hello world' (or anything that does not need an additional library), you can use the following template:

[docker-compose.yml]

version: '2'
services:
    my_service:
        build: ./<service_dir_name> # replace <.> with the directory containing your files in the current directory where .yml is located.

[Dockerfile]

FROM python:2.7-alpine

# Copy your files in this order: <files in the current dir>  <destination in the container>
COPY codes/ /app

WORKDIR app
ENTRYPOINT python -u my_code.py

alpine is a very light linux with python pre-installed. In case you need to add some libraries, add

RUN pip install <library> 

to the Dockerfile. Your python code is located in the

<files in the current dir>

Hope that helps.

Upvotes: 1

Related Questions