Mapping local dir when using docker interpreter
Answered
Hi all,
I try to debug code I have on my local system using docker container as my interpreter. My code uses some local files during its run, these files located on my local system but not in the container. Basically the container is used just as an environment. When I try to run my code it doesn't recognize the files I try to read, how can I map my local dir to debug my code (same as you can do when using ssh remote interpreter)?
System:
Ubuntu 18.04
Pycharm 2019.3
Python 3.7.4
Please sign in to leave a comment.
You can add your custom Volume Bindings in your run configuration (Run | Edit Configurations... -> Docker container settings).
If you store those files inside the project, make sure the Path Mappings of the interpreter are correct (File | Settings | Project | Project Interpreter).
So basically the only possibility is to place the files within the project? What if that can't be done?
It is possible to use files that are located outside of your project by adding the needed directories as Content Root in File | Settings | Project | Project Structure and configuring the corresponding Volume Binding in your Python run configuration. Example:
Hi Sergey,
Thank you for your detailed explanation, how do I get to the second menu? will it work even if the container is running on a remote server? (if not what should I do?)
Go to Run | Edit Configurations... menu, choose your Python run configuration (or create one) and there you will see this Docker container settings field.
No, PyCharm doesn't support running Docker on the remote machine yet https://youtrack.jetbrains.com/issue/PY-33489
But in the description of that feature request, you can find a workaround. Not sure if it covers your case though.
Sorry for annoying but I can't see "Docker container settings" field under my Python run configuration. Maybe first I need to set my project interpreter to be a docker container one? (if that is the case then I understood)
About "PyCharm doesn't support running Docker on the remote machine yet" this will be really helpful! waiting for it.
Thank you very much!
Yes, you are right, you should switch to Docker interpreter first. Sorry I didn't mention that.
Thank you!
I was trying to use a remote docker interpreter. The host machine is remote. The setup was successful, however, I found it impossible to sync my local source with the container.
I just want to confirm that this thread is talking about the case when the docker engine is still local. There is no way to set up "true" remote docker development because we cannot bind a local source directory to a container running on a remote docker host.
It seems to me that the only way to accomplish this is to use an ssh remote interpreter with FTP source sync.
Am I getting this right?
Hi Sergei, my use case is exactly like the OP described (using the docker container purely as an interpreter/debugger). What is the best practice for granting the container access to my project? Should I grant access in the Dockerfile, or in the Pycharm settings....or something else?
Thanks!
Jetty
The best practice is just as Sergey described, using volume bindings in the run/debug configuration.
Is there a way to do the custom volume binding for the Python Console? There's no “Docker container settings” for the setting page for Python Console, and also not on the Python Interpreter page itself.
My Pycharm version is 2023.2.6
Wenfei Yan , please check the corresponding settings under File | Settings | Build, Execution, Deployment | Console | Python Console:
Mikhail Tarabrikov Thanks for replying, but I have tried this Path mappings and for some reason it doesn't work.
I added the exact same mappings I had in the “Docker container settings”, but in the Python console it still can't find those files. Actually, I tried os.listdir(<Remote Path>), and found they doesn't exist.
So I assumed this “Path mappings” is a different thing than what we have in “Docker container settings”. For example, in the run configuration, there's also a “Path mappings” option, which doesn't work as well. Also, in the “Path mappings”, it says “Remote path” rather than “Container path”, so maybe it's mapping to the Linux VM for docker engine (I'm on macOS) rather than the actual Docker container?
Do you know whether the “Path mappings” is supposed to work for the custom docker colume bindings? If yes, it should be a bug.
Wenfei Yan , this appears to be a known issue, unfortunately. Please see this comment for details. You can try to configure docker-compose interpreter with defined
volumes
section as a workaround.Apologies for the inconvenience.