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/...tosrc/vdp/...(orvdp/...) โ at this point you should be able to runpython -m vdpwithin thesrcdirectory 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.
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/...tosrc/vdp/...(orvdp/...) โ at this point you should be able to runpython -m vdpwithin thesrcdirectory 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.
Python is not able to find the file base_functions because it was run from other folder.
You may want to change the working directory of in the Dockerfile:
WORKDIR /path/to/workdir
Importing python files in Docker container - Stack Overflow
django - How to import python module from docker - Stack Overflow
Setting up the docker module in Python
Python import error in Docker compoment
Videos
ยป pip install docker
After much confusion, I got the container to run by setting a PYTHONPATH env variable in the Dockerfile:
ENV PYTHONPATH "${PYTHONPATH}:/app/"
Setting the PYTHONPATH as such works but feels clumsy:
ENV PYTHONPATH "${PYTHONPATH}:/app/"
Using Docker's WORKDIR allows for a much cleaner solution:
FROM python:3.7
WORKDIR /code
RUN pip install fastapi uvicorn python-osc
EXPOSE 80
COPY app ./app
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"]
Note how the COPY statement changed.
I'm not sure what you are trying to do. But here are some tips, that may help you.
- Python libraries for working with docker from python
- https://github.com/docker/docker-py
- https://github.com/deepgram/sidomo
- You can start container's python console
docker run -ti myimage python - Also you can have connected
volumeswhere 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} ...
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.
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
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
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
ยป pip install docker-py