How to run pycharm in a docker container?

I am very new to docker. I want to create a python application in a docker container. When I create an application, I want to test / run it in Pycharm and in the container that I create.

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

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

enter image description here

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

+5
source share
1 answer

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

If you try to add this to pycharm, you will receive the following message:

Only Linux supported

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

Thus, PyCharm really wants to connect to the docker daemon via a TCP socket and supports the recommended TLS protection for this socket. The default folder in the Certificates folder is the default folder for the machine-dock-machine certificate.

You can implement a workaround to open Docker for Mac through a TCP server if socat is installed on your OSX computer.

On my system, I installed it through homebrew:

brew install socat 

Now that it is 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 allow any process running as any user on your Mac to access your Mac Docking Server. The unix socket is protected by user rights, but 127.0.0.1 is not.

This socat command tells her to listen on 127.0.0.1:2376 and pass the connections to /var/run/docker.sock. The reuseaddr and fork parameters allow this command to serve multiple connections, not just the very first one.

I can verify 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 install the same tcp://127.0.0.1:2376 in place. I should return a "Connection successful" message:

connection successful

This workaround will require the socat command to be run anytime you want to use the docker from PyCharm.

If you want to do the same, but with TLS, you can configure the certificates and make them available for both pycharm and socat and use socat OPENSSL-LISTEN instead of the TCP-LISTEN function. However, I will not go into the details of this answer.

+4
source

Source: https://habr.com/ru/post/1258215/


All Articles