Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run pycharm within my docker container?

I'm very new to docker. I want to build my python application within a docker container. As I build the application I want to be testing / running it in Pycharm and in the container I build.

How do I connect Pycharm pro to a specific container or image (either python or Anaconda)?

When I create a project, click pure python and then add remote, then clicking docker I get the following result

enter image description here

I'm running on Mac OS X El Capitan (10.11.6) with Docker version 1.12.1 and Pycharm Pro 2016.2.3

like image 275
kindjacket Avatar asked Oct 14 '16 14:10

kindjacket


1 Answers

Docker-for-mac only supports connections over the /var/run/docker.sock socket that is listening on your OSX host.

If you try to add this to pycharm, you'll get the following message:

Only supported on Linux

"Cannot connect: java.lang.ExceptionInInitializerError, caused by: java.lang.IllegalStateException: Only supported on Linux"

So PyCharm really only wants to connect to a docker daemon over a TCP socket, and has support for the recommended TLS protection of that socket. The Certificates folder defaults to the certificate folder for the default docker-machine machine, "default".

It is possible to implement a workaround to expose Docker for Mac via a TCP server if you have socat installed on your OSX machine.

On my system, I have it installed via homebrew:

brew install socat

Now that's installed, I can run socat with the following parameters:

socat TCP-LISTEN:2376,reuseaddr,fork,bind=127.0.0.1 UNIX-CLIENT:/var/run/docker.sock

WARNING: this will make it possible for any process running as any user on your whole mac to access your docker-for-mac. The unix socket is protected by user permissions, while 127.0.0.1 is not.

This socat command tells it to listen on 127.0.0.1:2376 and pass connections on to /var/run/docker.sock. The reuseaddr and fork options allow this one command to service multiple connections instead of just the very first one.

I can test that socat is working by running the following command:

docker -H tcp://127.0.0.1:2376 ps

If you get a successful docker ps response back, then you know that the socat process is doing its job.

Now, in the PyCharm window, I can put the same tcp://127.0.0.1:2376 in place. I should get a "Connection successful" message back:

connection successful

This workaround will require that socat command to be running any time you want to use docker from PyCharm.

If you wanted to do the same thing, but with TLS, you could set up certificates and make them available for both pycharm and socat, and use socat's OPENSSL-LISTEN instead of the TCP-LISTEN feature. I won't go into the details on that for this answer though.

like image 130
programmerq Avatar answered Sep 25 '22 21:09

programmerq