问题
I have a Docker image that I use for Django development, and via some xauthority file mechanics, I use Eclipse from within the container. For the most part, Eclipse works well, but there are a few nagging issues that seem to be related to the fact it's running inside a container; after all, Docker wasn't really developed for this purpose. So, I wonder...
If I run an instance of Eclipse on my local machine, can I configure a given project to access the libraries installed in a running container? That is, have it resolve imports, run code using Python 2 or 3/Django 1 or 2 depending on the individual project and the container it's accessing?
Host Machine: CentOS 7
Base Image: Ubuntu 16.04
Added Info: Here is my run command(thought there is some scripting that precedes it for the xauth stuff), which shows how I mount my volumes to a given Docker environment:
docker run -h django-env \
-d -p 8000:8000 \
-w=/home/$USER \
--user $USER \
-v $XAUTH:$XAUTH -v $XSOCK:$XSOCK \
-v psql_var_lib:/var/lib/postgresql \
-v psql_var_log:/var/log/postgresql \
-v psql_var_etc:/etc/postgresql \
--mount type=bind,source=$LOCAL_REPO/django-env-opt,target=/opt \
--mount type=bind,source=$LOCAL_REPO/django-env-home,target=/home/$USER \
-e XAUTHORITY=$XAUTH -e DISPLAY \
--entrypoint "" hildy:django_python1 bash -c "sudo /etc/init.d/postgresql
start && /opt/eclipse/eclipse/eclipse"
回答1:
I use Eclipse from within the container.
You don’t need to. The purpose of the container is to provide you with a compilation environment. As long as you can share files between the container and host, all code editing can happen from your local eclipse.
So the question is how do you share files? Via volumes. The simplest way is to say docker run -v <hostdir>:<containerdir>.
Ref: https://docs.docker.com/storage/volumes/
回答2:
First, I'd like to thank @Timir for leading me to the correct answer and helping me realize that I do not need to run Eclipse from within the container. Trying to do so was just a fundamental misunderstanding about what Docker is for, as this issue came about soon after beginning to learn Docker in the first place. You can call me a noob, it's fine, you're not wrong..but on to the resolution.
In my case, what I did was create a volume with docker volume create <volume name I chose> and mounted the volume to the container's /usr/ directory in the run command like so: -v <volume name I chose>:/usr.
Next, in Eclipse I navigated to window > preferences > pydev> interpreters > python interpreter > new. Then, I just went to the directory on my host machine where docker volumes live - on my CentOS 7 host, that is /var/lib/docker/volumes/<volume name I chose>/_data/bin/python3.5 -chose that location and gave it a name. Then when I created a given PyDev project, I just chose the interpreter I created and I was in business.
来源:https://stackoverflow.com/questions/50010108/can-my-local-eclipse-install-access-libraries-in-docker-container