You're practically renaming your package (it smells like a package, having __init__.py and __main__.py) from "src" to "vdp" when you're creating the dockerfile.

I'd recommend:

  • Rename src/... to src/vdp/... (or vdp/...) โ€“ at this point you should be able to run python -m vdp within the src directory on your host machine and things should work.
  • Change the Dockerfile stanzas to
    COPY src /src
    WORKDIR /src
    ENTRYPOINT ["python3", "-m", "vdp"]
    โ€“ this'd be equivalent to how you're running it on the host machine.

Another option would be to drop the /src in the repository and change things to COPY vdp /src/vdp.

The third option, of course, would be to set up proper Python packaging, have your setup.py build a proper wheel, then simply install that in the Docker container.

Answer from AKX on Stack Overflow
Discussions

Importing python files in Docker container - Stack Overflow
When running my docker image, I get an import error: File "./app/main.py", line 8, in import wekinator ModuleNotFoundError: No module named 'wekinator'` How do I import lo... More on stackoverflow.com
๐ŸŒ stackoverflow.com
django - How to import python module from docker - Stack Overflow
I made a docker image contains nginx, uwsgi and some python module, using volumes out the docker to develop code. So how should I use python environment from docker when coding? More on stackoverflow.com
๐ŸŒ stackoverflow.com
May 24, 2017
Setting up the docker module in Python
If you're using pycharm or vsc or similar you might need to set the python version to use in the settings. More on reddit.com
๐ŸŒ r/docker
4
7
December 16, 2022
django - How to add a Python module to a docker container? - Stack Overflow
I created a Boilerplate project from the Divio Django project template: (Python 3.6, Django CMS, HTML 5) Checking out the repository returns a couple of files, among which are a docker-compose and a More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
PyPI
pypi.org โ€บ project โ€บ docker
docker ยท PyPI
Connect to Docker using the default socket or the configuration in your environment: import docker client = docker.from_env() You can run containers: >>> client.containers.run("ubuntu:latest", "echo hello world
      ยป pip install docker
    
Published ย  May 23, 2024
Version ย  7.1.0
๐ŸŒ
Pythonโ‡’Speed
pythonspeed.com โ€บ articles โ€บ importerror-docker
Debugging ImportError and ModuleNotFoundErrors in your Docker image
January 17, 2025 - The next few are basically just to give you access to the Python standard library. The last entry, ending with site-packages, is where pip will install packages. So long as youโ€™re installing your code with pip, youโ€™re all good. It doesnโ€™t matter what directory youโ€™re in, you will be able to import it successfully. $ docker run -it python:3.8-slim-buster bash root@3c19e2314f01:/# pip install --quiet flask root@3c19e2314f01:/# python -c "import flask; print('success')" success root@3c19e2314f01:/# cd /tmp root@3c19e2314f01:/tmp# python -c "import flask; print('success')" success
๐ŸŒ
Deepgram
deepgram.com โ€บ learn โ€บ import-a-docker-container-in-python
Import a Docker Container in Python - Deepgram Blog โšก๏ธ
June 13, 2024 - With Sidomo, you can pull FFMPEG with docker and easily run it from Python. ... The example below will grab audio from a URL, transcode it, and print debug messages to prove that it worked. The process's stdout (the raw audio output) is disabled since we only want to see the debug messages. ... from sidomo import Container url = 'http://www2.warwick.ac.uk/fac/soc/sociology/staff/sfuller/media/audio/9_minutes_on_epistemology.mp3' with Container( 'cellofellow/ffmpeg', stdout=False ) as c: for line in c.run( 'bash -c \"\ wget -nv -O tmp.unconverted %s;\ ffmpeg -i tmp.unconverted -f wav -acodec pcm_s16le -ac 1 -ar 16000 tmp.wav;\ cat tmp.wav\ \"\ ' % url ): print line
๐ŸŒ
GitHub
github.com โ€บ docker โ€บ docker-py
GitHub - docker/docker-py: A Python library for the Docker Engine API ยท GitHub
Connect to Docker using the default socket or the configuration in your environment: import docker client = docker.from_env() You can run containers: >>> client.containers.run("ubuntu:latest", "echo hello world
Starred by 7.2K users
Forked by 1.7K users
Languages ย  Python
Find elsewhere
๐ŸŒ
Docker
docker.com โ€บ blog โ€บ how-to-dockerize-your-python-applications
How to โ€œDockerizeโ€ Your Python Applications | Docker
By entering pip install python-dotenv, you can quickly get this library up and running. With the from dotenv import load_dotenv and from dotenv import dotenv_values commands, you can load configurations from files with or without touching your ...
Published ย  November 6, 2024
Top answer
1 of 2
1

I'm not sure what you are trying to do. But here are some tips, that may help you.

  1. Python libraries for working with docker from python
    • https://github.com/docker/docker-py
    • https://github.com/deepgram/sidomo
  2. You can start container's python console docker run -ti myimage python
  3. Also you can have connected volumes where you will store your source code and than run this code with container's environment

NEW IDEA

Importing module in python means having module's folder in your PYTHONPATH. So basically you probably would need to mount your docker with something like sshfs to some folder, and than add this folder to your PYTHONPATH. After that you can do from {docker_module} ...

2 of 2
0

Not sure if this is what you're asking but if you have Python libraries within the Docker image and you want to append them to an already-exisitng Pipfile, you can copy the requirements.txt file from the Docker container and then use pipenv to install them.


# In terminal session 1 run the container with the default shell in interactive mode. This way the container stays alive and we can copy the file from the container to the host
docker run -it --rm $USER/$IMAGE_NAME:1.0.0.XXXXX sh

# In terminal session 2
docker cp $CONTAINER_ID:/requirements.txt ./

# This will add the requirements from the text file into the Pipfile
pipenv install -r requirement.txt

# Sometimes requirements will get corrupted
pipenv clean

If using Visual Studio Code, reload window to refresh dependencies in Python interpreter.

๐ŸŒ
Medium
medium.com โ€บ @shuklashubh818 โ€บ exploring-docker-py-a-python-library-for-docker-automation-8df5bc727fbe
Exploring Docker-Py: A Python Library for Docker Automation | by Shubh Prakash Shukla | Medium
February 15, 2024 - To begin using Docker-Py, you first need to install the library using pip: ... Once installed, you can import the Docker client into your Python script and start interacting with Docker:
๐ŸŒ
Reddit
reddit.com โ€บ r/docker โ€บ setting up the docker module in python
r/docker on Reddit: Setting up the docker module in Python
December 16, 2022 -

Hi. Could anyone help explain to me how to actually setup the docker module in python. I have tried so far:

pip install docker

However, after doing this, I try running the basic script:

import docker
client = docker.from_env()

After doing so, I get the following:

Exception has occurred: ModuleNotFoundError

No module named 'docker'

Could anyone please explain to me when I am getting this message. It's really confusing. Apologies if this is very obvious

Top answer
1 of 3
4

you can use a requirements.txt file. To write your module to import and call him in your dockerfile

$ more requirements.txt 
paramiko
lxml
pg8000

And in your dockerfile :

ADD             ./requirements.txt ./
RUN             python3 -m pip install -r requirements.txt
2 of 3
3

Use Dockerfile to install python packages.

Dockerfile content

FROM python:3

RUN pip install --upgrade pip && \
    pip install aldryn_apphooks_config
$: docker build -t "web:python" .
Sending build context to Docker daemon  3.584kB
Step 1/2 : FROM python:3
 ---> 59a8c21b72d4
Step 2/2 : RUN pip install --upgrade pip &&     pip install aldryn_apphooks_config
 ---> Running in e16a679af3d8
.
.
.
.
Successfully built aldryn-apphooks-config django-treebeard djangocms-admin-style
Installing collected packages: pytz, Django, django-appdata, django-treebeard, django-classy-tags, django-sekizai, djangocms-admin-style, django-formtools, django-cms, aldryn-apphooks-config
Successfully installed Django-2.1.8 aldryn-apphooks-config-0.5.2 django-appdata-0.2.1 django-classy-tags-0.8.0 django-cms-3.6.0 django-formtools-2.1 django-sekizai-0.10.0 django-treebeard-4.3 djangocms-admin-style-1.3.0 pytz-2018.9
Removing intermediate container e16a679af3d8
 ---> 78ecd015d983
Successfully built 78ecd015d983
Successfully tagged web:python

Start the container and verify the package installion.

$ docker run -it web:python /bin/bash
root@1d1df7416b8a:/# pip freeze
aldryn-apphooks-config==0.5.2
Django==2.1.8
django-appdata==0.2.1
django-classy-tags==0.8.0
django-cms==3.6.0
django-formtools==2.1
django-sekizai==0.10.0
django-treebeard==4.3
djangocms-admin-style==1.3.0
pytz==2018.9
๐ŸŒ
Docker Community
forums.docker.com โ€บ general
Python import error in Docker compoment - General - Docker Community Forums
April 15, 2025 - I try to package my code in image using code below: FROM torch_gpu:v1 WORKDIR /app COPY ./code/* ./ CMD ["python","main.py"] when I try to run it with docker run . It goes wrong with File "/app/main.py", line 30, in load_AU_model from OpenGraphAU.conf import get_config, set_env, set_outdir, set_logger ModuleNotFoundError: No module named 'OpenGraphAU' The file dir tree is /app ---/OpenGraphAU ----conf.py ---main.py I donโ€™t kown where is wrong and how to fix it? but when I try run...
๐ŸŒ
PyPI
pypi.org โ€บ project โ€บ docker-py
docker-py ยท PyPI
A Python library for the Docker Remote API.
      ยป pip install docker-py
    
Published ย  Nov 02, 2016
Version ย  1.10.6
๐ŸŒ
Docker Community
forums.docker.com โ€บ general
How to run python package inside a Docker container? - General - Docker Community Forums
September 3, 2020 - 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 mypro...
๐ŸŒ
Docker Hub
hub.docker.com โ€บ _ โ€บ python
python - Official Image | Docker Hub
As with all Docker images, these likely also contain other software which may be under other licenses (such as Bash, etc from the base distribution, along with any direct or indirect dependencies of the primary software being contained). Some additional license information which was able to be auto-detected might be found in the repo-info repository's python/ directoryโ .
๐ŸŒ
Docker Docs
docs.docker.com โ€บ reference โ€บ docker engine api โ€บ sdk โ€บ examples
Examples using the Docker Engine SDKs and Docker API
February 2, 2023 - Each of these examples show how to perform a given Docker operation using the Go and Python SDKs and the HTTP API using curl. This first example shows how to run a container using the Docker API. On the command line, you would use the docker run command, but this is just as easy to do from your own apps too. This is the equivalent of typing docker run alpine echo hello world at the command prompt: ... package main import ( "context" "io" "log" "os" "github.com/moby/moby/api/pkg/stdcopy" "github.com/moby/moby/api/types/container" "github.com/moby/moby/client" ) func main() { ctx := context.Back
๐ŸŒ
Visual Studio Code
code.visualstudio.com โ€บ docs โ€บ containers โ€บ quickstart-python
Python in a container
November 3, 2021 - Build, run, and verify the functionality of a Django, Flask, or General Python app. Debug the app running in a container. Install Docker on your machine and add it to the system path.
๐ŸŒ
Pythonrepo
pythonrepo.com โ€บ repo โ€บ docker-docker-py-python-devops-tools
A Python library for the Docker Engine API | PythonRepo
January 13, 2022 - [thibaultbronchain@mactibo ~:]$ export DOCKER_HOST=tcp://192.168.59.103:2376 [thibaultbronchain@mactibo ~:]$ export DOCKER_CERT_PATH=/Users/thibaultbronchain/.boot2docker/certs/Docker-Wordpress-NoASG-DualNode-0 [thibaultbronchain@mactibo ~:]$ export DOCKER_TLS_VERIFY=1 [thibaultbronchain@mactibo ~:]$python Python 2.7.6 (default, Sep 9 2014, 15:04:36) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import docker >>> from docker.client import Client >>> from docker.utils import kwargs_from_env >>> client