Failed to run Python3 http.server on Docker’s container

  docker, python

I’m trying to create a Docker container which runs a Python http.server listening on port 8000. This is my Dockerfile:

FROM ubuntu:20.04 AS focal

WORKDIR /usr/src/server
COPY . .

RUN apt update && apt-get install -y build-essential python python3 zip net-tools iptables sudo curl

CMD ["python3", "-m http.server", "8000"]

First, I successfully built the image: docker build -t py_server .

Then I tried to run the image as a container: docker run --rm -p 8000:8000 py_server

But the following error was thrown:

/usr/bin/python3: Error while finding module specification for ' http.server' (ModuleNotFoundError: No module named ' http')

Not sure why python3 wasn’t able to find the http module when specified with CMD in the Dockerfile. I tested whether python3 in the container has http.server by directly executing the command using bash on the py_server image, and it worked:

$ docker run -it --rm -p 8000:8000 py_server bash
[email protected]:/usr/src/cs435_mp1_server# python3 -m http.server 8000
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...

I’m very confused about this.

Source: Docker Questions

LEAVE A COMMENT