2

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.

7
  • Is python already installed in the base ubunu image? I dont thin so. To use this image you should minimal do an apt-get update and then apt-get install your stuff in the docker file before calling any none existing command Commented Aug 29, 2016 at 22:27
  • Apologies, I have added in the two lines into the dockerfile which I had removed when trying different base images. Commented Aug 29, 2016 at 22:31
  • In the second line you copy a file or directory with the name "requirements" into the container. But your not installing or registering anthing in ubuntus environment Commented Aug 29, 2016 at 22:32
  • How would I do that? Commented Aug 29, 2016 at 22:40
  • 1
    You completely changed your question during my last comment. Useless to talk further. Use this: hub.docker.com/_/python Commented Aug 29, 2016 at 22:44

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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"]


Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.