PyCharm ssh to docker container (PyTorch)

1 Run and start the container to specify the port number mapping -p 6666:22

# 由镜像启动容器 容器22端口(即ssh连接端口)映射到主机的6666端口,该端口自定
docker run --gpus all --name mydocker_pytorch -id -p 6666:22 [images_id]
# 如果已经搭建好一个容器环境,停止该容器,存储为镜像,再运行该镜像,可以保留已搭建的环境(已存储在磁盘上的会保留)
docker stop [yourdocker]
docker commit [yourdocker] [your image_name]:[your tag_name]
# docker save [yourdocker] [your image_name]
# docker load [your image_name]
docker run --gpus all -p 6666:22 -itd -v [own_path]:[container_path] [images]:[TAG]
# --privileged=true     /usr/sbin/init
systemctl restart sshd.service //重启命令
systemctl start sshd.service //启动命令

2 Install the ssh service and configure

# 更新安装源
apt-get update
# 安装ssh服务
apt-get install openssh-server
# 更改容器ubuntu的密码,会输入两次
passwd
# 安装vim
apt-get install vim
# 修改ssh配置文件
vim /etc/ssh/sshd_config

	增加下面三行:
		PubkeyAuthentication yes #启用公钥私钥配对认证方式 
		PermitRootLogin yes #允许root用户使用ssh登录 
		PasswordAuthentication yes

# 重启ssh服务
/etc/init.d/ssh restart

# 个人计算机上cmd命令测试一下,输入下面命令后会要输入在docker容器里的ubuntu系统密码
ssh root@[服务器ip地址] -p [之前docker端口映射的主机端口号] 

3 PyCharm configuration

1 Create an ssh connection (this step can also be done simultaneously in) 2

Host (host): Server IP address
Username (user): The account used in the docker container, usually root
Port number: According to the configuration above, it is 6666
Password: The password of the docker container

2 Configure ssh python interpreter

# 创建一个项目后files-->settings-->Project-->python Interpreter

2.1 Select an interpreter

insert image description here

2.2 Select the ssh interpreter (the ssh connection can also be established in the new server configuration here)

insert image description here

2.3 Obtain the storage location of the python interpreter in the container

whereis python

python: /usr/bin/python3.6 /usr/bin/python3.6m /usr/lib/python3.6 /etc/python3.6 /usr/local/lib/python3.6 /opt/conda/bin/python3.8 /opt/conda/bin/python /opt/conda/bin/python3.8-config
# 可知正确版本的python解释器在/opt/conda/bin/python3.8或者/opt/conda/bin/python (两个都可以)

2.4 Configure python interpreter

insert image description here

2.5 After the above is completed, you can use the docker environment programming locally in pycharm

个人计算机与docker容器文件同步在
tools(工具)-->deployment(部署)-->与部署在......上的版本同步

Guess you like

Origin blog.csdn.net/weixin_42529594/article/details/125604192