How does pycharm connect to the docker container of the remote server to run and debug the code (2)

There are two methods for how pycharm connects to the docker container of the remote server:

The first type: pycharm connects to the running docker container via ssh 

The second: pycharm connects to the docker image, pycharm runs the code and then automatically creates the container

This article is a tutorial for the second method. For the first method, please click the link above

condition:

(1) pycharm professional edition, community edition does not have this function

(2) The remote server where docekr service is installed, can be ubuntu and CenterOS

(3) The docker server has prepared the docker image of the environment

1. Configure docker remote service on the remote server

1. Modify the configuration file of the docker service (the file name may be different, but there is only one similar file)

sudo vim /lib/systemd/system/docker.service

# Find "ExecStart" and comment out ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock:

Then add: ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:2375

Save and exit.

If you did not comment out the line in the red box, add ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:2375 at the end, and restart the docker service in the following step 3 to make an error:

My server here is ubuntu 16. 04, other versions may be different, ubuntu 18. 04 can refer to this https://www.cnblogs.com/kaishirenshi/p/11214704.html

2. Reload

sudo systemctl daemon-reload

3. Restart the docker service

sudo service docker restart

Or sudo systemctl start docker This command can also start the docker service

4. Check if port 2375 is open

netstat -nlp | grep docker

Second, create a good docker image

Here we use Dockerfile to create our own mirror, it is best to have installed various python libraries when creating the mirror

sudo docker build -t pytorch:pycharm_test .

Three, pycharm configures docker service

1. Configure Python Interpreter

 

2. Configure run

Set the docker container startup parameters:

So far, all the configurations are OK, and you can debug the code in pycharm.

Note: Regardless of the first or second method, when you connect to the container to run code in pycharm, you must write absolute paths in all relevant paths in the code. Otherwise, it will report an error when you run the code that the file or folder cannot be found.

四、Reference

[1] https://blog.csdn.net/qq_21768483/article/details/108830325

[2] https://www.cnblogs.com/jclian91/p/12099239.html

Guess you like

Origin blog.csdn.net/Thanours/article/details/109271836