Mil0R3
Mil0R3

Reputation: 3956

How to import python module from docker

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?

Upvotes: 1

Views: 5731

Answers (2)

Ko Ga
Ko Ga

Reputation: 906

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.

Upvotes: 0

Sardorbek Imomaliev
Sardorbek Imomaliev

Reputation: 15360

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
  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} ...

Upvotes: 1

Related Questions