Pycharm SSH 容器中的python环境

前提

  • Pycharm专业版提供ssh配置环境,可以在本地使用远端的python环境,这样就不需要重复配置环境,而且也能保证客户端与服务器端环境一致性。

  • 物理机需要满足多个python环境(多人开发),可以使用conda解决,但是更好的方式是通过容器化的方式来满足不同开发者,不同依赖环境

镜像打包

选择基础镜像

  • 目前,常用的基础镜像可以选择python容器镜像 或者unbutun系统镜像

  • 系统镜像需要在上面安装python环境,这样下来会导致打包出来的镜像比加大

  • python:3的基础镜像是debian系统,相比ubuntu更小,更便捷

编写dockerfile

FROM python:3
# 阿里云加速
RUN echo "deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse \n\
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse \n\
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse \n\
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse \n\
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse \n\
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse \n\
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse \n\
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse \n\
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse \n\
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse \n"\
 > /etc/apt/sources.list
RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3B4FE6ACC0B21F32
RUN apt-get update && \
    apt-get remove openssh-client -y && \
    apt-get install openssh-server vim -y && \
    apt-get clean
# 设置root密码
RUN echo "root:111111" | chpasswd
# 设置root可登陆
RUN echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
RUN mkdir -p /app
# 将需要的python依赖写入到requirments中
COPY requirements.txt /app
WORKDIR /app
# 阿里云加速
RUN pip3 install --no-cache-dir -r requirements.txt  -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
EXPOSE 22
# ssh服务 daemon方式运行
CMD /etc/init.d/ssh start -D

build

docker build  -t python-env:1.0 .

挂载使用

docker run -itd -p 2222:22 -v /your/python/code/path/:/app python-env:1.0

验证使用

docker exec -it 609ed1fbbee5 bash
root@609ed1fbbee5:/app# python3 --version
Python 3.8.3
root@609ed1fbbee5:/app# ps -ef |grep ssh
root         1     0  0 07:49 ?        00:00:00 /bin/sh -c /etc/init.d/ssh start -D
root         8     1  0 07:49 ?        00:00:00 /bin/sh /etc/init.d/ssh start -D
root        17     8  0 07:49 ?        00:00:00 /usr/sbin/sshd -D
root        31    18  0 07:51 ?        00:00:00 grep ssh
root@609ed1fbbee5:/app#
各位伙伴们好,詹帅本帅搭建了一个个人博客和小程序,汇集各种干货和资源,也方便大家阅读,感兴趣的小伙伴请移步小程序体验一下哦!(欢迎提建议)

推荐阅读

牛逼!Python常用数据类型的基本操作(长文系列第①篇)
牛逼!Python的判断、循环和各种表达式(长文系列第②篇)

牛逼!Python函数和文件操作(长文系列第③篇)

牛逼!Python错误、异常和模块(长文系列第④篇)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值