Hitting local IDE breakpoints on libraries within a running docker container - python

I have a Dockerfile containing the lines:
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
I would like to set some breakpoints on libraries installed via requirements in my local IDE. I am wondering how to launch the docker image such that these files are accessible from my local IDE. The relevant modules are located within the image at:
/usr/local/lib/python3.7/site-packages
so, i was thinking of using the line:
docker run \
-v site_pkgs:/usr/local/lib/python3.7/site-packages
--entrypoint python3 \
app-dev
but, this seems to overwrite the containers directory rendering it unable to find the modules it expects. Any guidance in how to perform this type of debugging on a running container would be very helpful. Thank you!

a) If you just mean to get the python package module code in container, and have test application in your local IDE to call these modules. Then you do not need to run this container, just:
docker cp /usr/local/lib/python3.7/site-packages .
After that, these python module's py files will be in your local(docker host). Then you can use your local IDE to debug them, of course with your own test code.
b) If you mean to use local IDE directly debug the code in container, then VSCode IDE is your choice.
NOTE: you need to use insiders build currently, as it's a pretty new feature also I think is a great feature.
See Developing inside a Container, vscode give you ability to set IDE in your local host machine, but still can let IDE debug the code in container.

Related

Select interpreter of docker container in the vscode

In vscode, we can install the python extension, then select the interpreter which we want, like python in the conda environment.
So we can use "shift" + "enter" key for running the code line by line in the terminal.
For managing different virtual environment, using docker container is a better way.
If I already install the docker, and pull the python image. How to select the interpreter which is created in the docker container? Not just remote to the docker container.
Tested on VSCode 1.61:
Install the Remote-Containers extension
Build/start the Docker container
Open the Command Palette and type Remote-Containers: Attach to Running Container..., then select the running Docker container
VSCode will restart and reload
On the Get Started page, click the Open... and enter the path to the Docker volume mounted to your source code. It must be set to the same path as WORKDIR in your Dockerfile-local, e.g. to /app.
Install the Python extension on the container
Open the Command Palette and type Python: Select Interpreter, then select the Docker interpreter
Open the Command Palette and type Python: Configure Tests, then select the framework you use
Source: https://dev.to/alvarocavalcanti/setting-up-a-python-remote-interpreter-using-docker-1i24
UPD. Remote development extensions seem to be one of the main focuses in VSCode development currently, e.g. the newer versions have got the Remote explorer Activity tab enabled by default, which allows much more intuitive approach to connecting to Docker containers. Check release notes here: https://github.com/microsoft/vscode-docs/tree/main/remote-release-notes
inside your devcontainer.json file that vscode created you have the image key and its value is the route to the image, if you want to use to change the python version you can do so there or using the quick instructions in vscode docs here
I think it's impossible, I am afraid you must remote to the docker container.
I really can't imagine out you taking a python interpreter in Linux to work on windows directly.
If your objective is to have vscode to work on a local project and run it with a docker-based interpreter, the solution is: mounting the local project directory to the docker container that contains the interpreter, then in vscode open the project directory (mounted) from the container.
How to mount your project directory:
docker run -v /user/localproject:/root/mountedproject
https://docs.docker.com/storage/volumes/
I have tested it. It should work.
Make sure all the extensions you want/need are installed in the container. Click Extensions on the left hand side to see which are installed locally and which are installed within the container.
Install everything within the container that you typically use locally (particularly Python and Pylance). You'll see a 'Install in Dev Container' button. Install everything you need, then restart VS Code and you should be good to go.
Example showing the install in Dev Container button

VsCode Remote Debugging, change pythonpath to point to docker container's python interpreter

I currently have a tensorflow-gpu docker container fully built. I have a volume from my host's workspace ($PWD) to /workspace inside the docker container.
I can successfully remote debug the python scripts inside my workspace/docker's workspace folder, but as expected, my pylint and intellisense don't work inside vscode, as the pythonPath is set to my local python interpreter, and the pylint init-hooks are using my local python environment to figure out if it should squiggly line something.
How do I change my local vscode's pythonpath to point to the python interpreter on docker rather than on the host? I want pylint to be calibrated to the docker's environment rather than the local host's environment.
I followed up on Brett's answer and you can now do the following:
Install the remote-containers extension.
Click "shift+command(ctrl)+p" and type and select "remote-containers: open folder in remote docket container"
I hope this helps. More information is available here:
https://code.visualstudio.com/blogs/2019/05/02/remote-development
https://devblogs.microsoft.com/python/remote-python-development-in-visual-studio-code/
https://youtu.be/jKB_nIoC1gQ
What you want is remote interpreter support which isn't supported yet. Please subscribe to this issue to know the status of this feature.

how to read files from a python module inside docker

How can I read files within a python module while executing in docker? I have a python module which I import in my code. normally in order to fetch the relative path of the module one can do <<module_name>>.__path__. however this approach does not work in docker but works locally.
Is there a common way in which I can read the files from the module in docker as well as in local?
There are a few things to consider:
Where is python installed in the container and what version? Does
that match your dev environment?
Look at your dockerfile - what is your working directory? Did you set one? Perhaps, you are running your python code from one directory, but trying to import a module from another.
Is your PYTHONPATH set in your container?
Have you installed the modules in the container that you're attempting to use? Perhaps with a requirements.txt file or manually? If so, are you executing your python code with the same python version/path that you installed the modules with?
Are you using a virtual environment? Has it been sourced?
What user is your container running as? Does it have access to the python modules? You may need to chown the site-packages path or run as a different user or root.

How to run a new Jupyter Notebook file that's not part of a pre-built docker image in docker?

I am new to Docker. In order to take the Udacity Deep Learning course, I had to set up TensorFlow on my Windows machine using Docker. (Although TensorFlow is now available on Windows, it only supports Python 3.5, however the Udacity course material requires Python 2.7. Therefore, I have to stick with the Docker way of using TensorFlow.)
To work on the assignments, I followed the instructions here as detailed below:
First, I installed docker toolbox.
Then, I launch Docker using the Docker Quickstart Terminal. For the first time, I ran:
docker run -p 8888:8888 --name tensorflow-udacity -it gcr.io/tensorflow/udacity-assignments:0.6.0.
Each time after, I just run this in my docker terminal:
docker start -ai tensorflow-udacity
Finally, in the address bar, with http://192.168.99.100:8888 I get the assignment Jupyter notebooks up and running (see image below).
However, what I want now is to run the final project of the course which is not part of the pre-built Udacity docker image. How can I do that? The final project can be found here, with the "digit_recognition.ipynb" specifically being the file to run in docker.
Any guidance is much appreciated.
First of all, you need a way to get this Jupyter notebook (final project) on your Docker instance.
What is an easy way to copy a file inside of a Docker container? Well, not a lot.
We could attach a volume.
We could rewrite the Dockerfile to include the final project.
We could also enter in the Docker container and download the file.
I am going to detail the last one, but, don't forget that there are many solutions to one problem.
How do we enter in the Docker container?
docker exec -it [container-id] bash
How can we get the [container-id] ?
docker ps
It will show you a list of containers, match the one you want to enter in.
Once you're in your container. How can we download the file we want?
We should try to figure out if we have wget or curl utilities to download a file. If we don't, we have to install them from any package manager available (try apt-get, if it works, do: apt-get install wget).
Once we have something to download files from the Internet, we have to find out where the notebooks are stored. That is the difficult part.
Look for any folder which might contain, there could also be some kind of magic one liner to type using find, unfortunately, I am no magic wizard anymore.
Let's assume you are in the good folder.
wget https://raw.githubusercontent.com/udacity/machine-learning/master/projects/digit_recognition/digit_recognition.ipynb
That's all! Reload your page and you should see the notebook displayed.
Side-note: You might also need to install extra dependencies in the container.
An alternative and much easier way is this:
Just start up your container like this: $ docker start -ai
tensorflow-udacity
Then, click the upload button and locate the final
project iPython Notebook file and upload it.
That's it. Whatever changes you make will be retained and you'll be able to see the new file in the container going forward!

Installing Anaconda on Amazon Elastic Beanstalk

I've added deploy commands to my Elastic Beanstalk deployment which download the Anaconda installer, and install it into /anaconda. Everything goes well, but I cannot seem to correctly modify the PATH of my instance to include /anaconda/bin as suggested by the Anaconda installation page. If I SSH into an instance and manually add it, everything works fine. But this is obviously not the correct approach, as machines will be added automatically by EB.
So my question is: how can I use Anaconda in my script?
A couple more details:
I've tried adding /anaconda/bin to the system PATH all ways I can think of. Pre/post deploy scripts, custom environment variables, etc. It seems that no matter what I do, the modifications don't persist to when the application is run.
I've tried to include Anaconda via adding it to sys.path: sys.path.append('/anaconda/bin')
to no avail. Using the following: sys.path.append('/anaconda/lib/python2.7/site-packages') allows me to import some packages but fails on import pandas. Strangely enough, if I SSH into the instance and run the application with their python (/opt/python/run/venv/bin/python2.7) it runs fine. Am I going crazy? Why does it fail on a specific import statement when run via EB?
Found the answer: import pandas was failing because matplotlib was failing to initialize, because it was trying to get the current user's home directory. Since the application is run via WSGI, the HOME variable is set to /home/wsgi but this directory doesn't exist. So, creating this directory via deployment command fixed this issue.
My overall setup to use Anaconda on Elastic Beanstalk is as follows:
.ebextensions/options.config contains:
commands:
00_download_conda:
command: 'wget http://repo.continuum.io/archive/Anaconda-2.0.1-Linux-x86_64.sh'
test: test ! -d /anaconda
01_install_conda:
command: 'bash Anaconda-2.0.1-Linux-x86_64.sh -b -f -p /anaconda'
test: test ! -d /anaconda
02_create_home:
command: 'mkdir -p /home/wsgi'
00_download_conda simply downloads Anaconda. See here for latest Anaconda version download link. The test commands are EB's way of letting you only execute the command if the test fails...Just prevents double downloading when in development.
01_install_conda installs Anaconda with options -b -f -p /anaconda which allows it to be installed in the specified directory, without user input, and skips installation if it has already been installed.
02_create_home creates the missing directory.
And finally - to use Anaconda inside your python application: sys.path.append('/anaconda/lib/python2.7/site-packages')
Cheers!

Categories

Resources