Maurice Marquis
Maurice Marquis

Reputation: 3

Docker container won't run python

I'm trying to run a python application inside a container. I keep getting:

"/bin/sh: 1: python3: not found

I've tried many different iterations, including using python as my base image, with different failures. This time I built an Ubuntu container and ran the commands one at a time in the command line and it works in bash. But when I run the container it still can't seem to find python.

Here's what I currently have for my dockerfile:

FROM ubuntu
CMD mkdir pong
WORKDIR /pong
CMD apt-get update 
CMD apt-get install python3 -y
CMD apt-get install python3-pip -y

COPY . /pong

CMD pip3 install pipenv
CMD pip3 install pyxel

CMD python3 main.py

I've spent a lot of time on the docker documentation too, so forgive me for posting this simple question, but I'm stumped. Thank you in advance!

Upvotes: 0

Views: 5202

Answers (2)

Cyril G.
Cyril G.

Reputation: 2017

Replace all CMD by RUN, the last one should be ENTRYPOINT.

FROM ubuntu
RUN mkdir pong
WORKDIR /pong
RUN apt-get update 
RUN apt-get install python3 -y
RUN apt-get install python3-pip -y

COPY . /pong

RUN pip3 install pipenv
RUN pip3 install pyxel

ENTRYPOINT ["python3", "main.py"]  

The main purpose of a CMD is to provide defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT instruction as well.

The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile.

For more details:
CMD
RUN
ENTRYPOINT

Upvotes: 1

Ron
Ron

Reputation: 6621

The sh shell does not know the full path of the executable python3

This should work better:

CMD /usr/bin/python3 main.py

Also, note that for the container not to halt, you need to keep the main.py process constantly running in the foreground. If it exits, the container stops.

Upvotes: 0

Related Questions