Docker 搭建Pycharm ssh远程虚拟环境

换了新版MacBook Pro一堆环境重新配置,python3.9.1支持m1,同时新系统方面也有很多兼容性方面问题,有不兼容的情况,不想反复折腾,所以可以使用Docker安装需要的项目环境。

在这里插入图片描述

在Docker社区中,对于是否需要为Docker容器添加SSH服务一直存有争议。对于是否需要为Docker容器添加SSH服务,应取决于容器的具体应用场景:即作为应用容器还是作为系统容器或数据容器。应用容器行为围绕应用生命周期,较少简单,不需要人工的额外干预;而系统容器或数据容器则需要支持管理员的登录操作,这个时候容器对SSH服务的支持就变得十分有必要了,因为它对资源的需求不高,同时可以保障安全性。

步骤

  1. 编写Dockerfile:
FROM centos:8
# install related packages
RUN yum install -y wget passwd openssh-server sqlite-devel automake autoconf make gcc gcc-c++ python3-devel mysql-devel bzip2 libffi-devel epel-release  net-tools openssh \
    && yum clean all

# install python 3.7.0
RUN wget https://npm.taobao.org/mirrors/python/3.7.0/Python-3.7.0.tar.xz \
    && tar -xvf Python-3.7.0.tar.xz -C /usr/local/ \
    && rm -rf Python-3.7.0.tar.xz \
    && cd /usr/local/Python-3.7.0 \
    && ./configure && make && make install
  1. 构建Docker环境
    docker build -t medweb:new .
  2. 启动容器
    docker run -itd --privileged -v /Users/donghao/workspace:/workspace -p 8022:22 --name myspace myspace:new /usr/sbin/init
  3. 进入容器
  4. 修改root密码
sh-4.4 # passwd root
Changing password for user root.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.
  1. 生成秘钥(HostKey)
ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
ssh-keygen -t rsa -f /etc/ssh/ssh_host_ecdsa_key
ssh-keygen -t rsa -f /etc/ssh/ssh_host_ed25519_key
  1. 编辑ssh配置
#禁用 PAM
UsePAM no

# 禁止root用户登录.禁用掉 PermitRootLogin yes
# PermitRootLogin yes
  1. 后台启动ssh service
    /usr/sbin/sshd -D &
(ENV) sh-4.4# lsof -i:22
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sshd      83 root    3u  IPv4  37933      0t0  TCP *:ssh (LISTEN)
sshd      83 root    4u  IPv6  37944      0t0  TCP *:ssh (LISTEN)
sshd    4779 root    4u  IPv4  56656      0t0  TCP 224d11afe4bc:ssh->bogon:64498 (ESTABLISHED)
sshd    4781 root    4u  IPv4  56656      0t0  TCP 224d11afe4bc:ssh->bogon:64498 (ESTABLISHED)

然后可以尝试容器内连接ssh服务, 需要安装下(yum -y install openssh-clients

(ENV) sh-4.4# ssh root@localhost -p 22
The authenticity of host 'localhost (127.0.0.1)' can't be established.
RSA key fingerprint is SHA256:um7p2haq4oUvPtP8W2bgNOpeKfdgZR6RzksPl5yVe7A.
Are you sure you want to continue connecting (yes/no/[fingerprint])?

9.配置pycharm ssh服务
打开pycharm,选择Python Interpreter,然后选择SSH Interpreter
在这里插入图片描述
指定上传到服务器,不用勾选,因为启动容器时指定了挂载宿主机的一个目录,宿主机的改动自动同步到容器内。interpreter可以选择创建的虚拟环境地址:例如:/.virtualenvs/ENV/bin/python
在这里插入图片描述
在这里插入图片描述

为什么不直接选择Docker解释器?

pycharm中添加docker解释器
在这里插入图片描述
最开始尝试的是直接添加Docker解释器,不过当在容器中的装包操作,Pycharm并没有重新加载解释器。(只有指定在Dockerfile中的装包操作能加载出对应的包。同时指定虚拟环境无法成为interpreter path)

可以设置host为localhost,避免ip变化

测试我的用例配置,解释器选择刚配置的,注意要添加path-mapping:
在这里插入图片描述
当点击pycharm run、debug时,实际执行的代码如下:ssh://root@192.168.102.99:8022/.virtualenvs/ENV/bin/python -u /root/.pycharm_helpers/pycharm/django_test_manage.py test xxxxxx测试用例文件 /workspace/medweb

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值