There are two methods for how pycharm connects to the docker container of the remote server:
The first: pycharm connects to the running docker container via ssh
The second type: pycharm connects to the docker image, pycharm runs the code and then automatically creates the container
This article is a tutorial for the first method. For the second method, please click the link above
condition:
(1) Prepare the professional version of pycharm, the community version does not have the function of connecting to a remote server
(2) The remote server ubuntu, docker has been installed
One, configure the docker container of the remote server
1. Start and run the interactive container
docker run -it --name pycharm_test -v /home/th/PycharmProjects/qa/:/workspace/qa -p 8080:22 --gpus all pytorch/pytorch:1.4-cuda10.1-cudnn7-runtime /bin/bash
Here you need to map the host port to the container port, and then the container is connected through the port.
-p 8080:22: Here the host port is 8080, and the container port is 22
2. Modify the root password of the container after entering the interactive
passwd
3. Install openssh-server and openssh-client for the container
apt-get install openssh-server
apt-get install openssh-client
If the following error occurs when installing the ssh service:
That's because the source in /etc/apt/source.list is relatively old and needs to be updated. The update command is as follows:
apt-get -y update
4. Modify the following options in the SSH configuration file
vim /etc/ssh/sshd_config
The above command opens the sshd_config file and adds the following at the end of the file.
PermitRootLogin yes #Allow root users to log in using ssh
5. Restart the ssh service
/etc/init.d/ssh restart
6. Exit the container and test the connection
Note: The above 2-5 steps are all operated inside the container.
ssh [email protected] -p 8080
root: The root account of the internal system of the container, not the user account of the server
127.0.0.1: server local ip
-p port number: the port here is the host port number 8080 mapped when we started the container in step 1, not the port 22 of the container
Run the above command on the server:
In this way, the test connection is successful, and the next step is to connect to the docker container in pycharm of our local windows system.
Second, configure pycharm and connect to the docker container
1. Configuration configure remote connection
Create the SFTP service docker_test, and fill in with the above screenshot.
Path mapping:
2. Upload local code data to the docker container and run
After uploading the code data, you need to choose to set the python compiler:
Set up the python interpreter to run the code
It should be noted here that the operating environment required by the code also needs to be installed and configured in the docker container. For example, I need the pytorch environment and the dependent libraries required for the python code to run have been installed in the docker container through pip.
Three, use Dockerfile to create a mirror that can be started by ssh
Dockerfile content:
————————————————————————————————————————
FROM pytorch/pytorch:1.4-cuda10.1-cudnn7-runtime
ADD tools/ /opt/tools/
RUN mkdir ~/.pip/ && cp /opt/tools/pip_sources_aliyun.txt ~/.pip/pip.conf && \
cp /opt/tools/sources.list /etc/apt/ && apt -y update && \
apt install -y vim zip && \
/opt/conda/bin/pip install -r /opt/tools/requirements.txt --trusted-host mirrors.aliyun.com
#The following is to set up the ssh service
RUN apt-get install -y openssh-server openssh-client && \
echo root:123456 | chpasswd && \
echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
ENV LANG C.UTF-8
CMD ["/usr/sbin/sshd", "-D"]
WORKDIR /work/
————————————————————————————————————————————————
In fact, you can add the following to your original Dockerfile
RUN apt-get install -y openssh-server openssh-client && \
echo root:123456 | chpasswd && \
echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
among them:
echo root:123456 | chpasswd
123456 is the password for setting step 2
Note here: After creating an image using Dockerfile and starting and running a container, you must manually start the ssh of the docker container:
/etc/init.d/ssh start
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.