1

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?

5
  • 2
    Please explain what you want to do in more details! Commented Aug 23, 2016 at 1:58
  • You want to coding inside docker container ? Commented Aug 23, 2016 at 2:04
  • @pigletfly no, I want to coding outside docker but import some module inside docker. Commented Aug 23, 2016 at 2:23
  • see new edit. I think this general direction in which this problem could be solved. Commented Aug 25, 2016 at 10:15
  • @Mil0R3 , hi, did you manage to import a module in a docker container to outside code? Commented Mar 17, 2021 at 11:02

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

Sign up to request clarification or add additional context in comments.

4 Comments

I have done this, what' my problem: for example django module is inside docker now, outside the docker (volumes) I can write code, when I type from django., how can I get code hints?
@Milor8 why would you want that? I don't understand reasoning for this.
@SardorbekImomaliev I think I know what he means as I'm facing a similar issue with caffe. As the installation procedure for its python wrapper on systems other than Ubuntu is somewhat not straightforward, I thought about using a ubuntu docker container with a caffe python module installation. My question, and maybe OPs question, is how can I import a python module that was installed in a container?
@fabda01, hi, did you manage to import a module in a docker container to outside code?
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.