Docker Community Forums

Share and learn in the Docker community.

How to run python package inside a Docker container?

Hi there,

My project directory looks like this the following. As you can see, I have structured the directory myproject to be seen as a Python package.

.
|-- Dockerfile
|-- README.md
|-- requirements.txt
`-- myproject
    |-- __init__.py
    |-- __main__.py
    |-- __pycache__
    |-- config.py
    |-- db.py
    |-- env.py
    |-- filehandle.py
    |-- main.py
    |-- pepkeys
    |-- test.py
    `-- util.py

On my host system, I can run the package like this without any issue:

python -m myproject

However, after I build my Docker image successfully, I cannot run the container, this is the error that I get:

/usr/local/bin/python: No module named  myproject

This is how my Dockerfile looks like, it is relatively simple.

FROM python:3.8.5-alpine

COPY . /app
RUN pip install -r /app/requirements.txt

WORKDIR /app

CMD ["python", "-m myproject"]

What do I need to do to be able to run my Python project as a package inside a Docker container?

Thanks.

I fixed the issue by changing my CMD line to:

CMD ["python", "-m", "myproject"]

When you run a Python image from Docker Hub, the interpreter is set up so you can play with the REPL directly. To start the REPL in a Python container, run the following command: $ docker run -it --rm python:rc Python 3.8.