首页 文章详情

Pycharm SSH 容器中的python环境

小詹学Python | 324 2021-11-19 04:15 0 0 0
UniSMS (合一短信)

前提

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

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

镜像打包

选择基础镜像

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

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

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

编写dockerfile

  1. FROM python:3


  2. # 阿里云加速

  3. RUN echo "deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse \n\

  4. deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse \n\

  5. deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse \n\

  6. deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse \n\

  7. deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse \n\

  8. deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse \n\

  9. deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse \n\

  10. deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse \n\

  11. deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse \n\

  12. deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse \n"\

  13. > /etc/apt/sources.list


  14. RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3B4FE6ACC0B21F32

  15. RUN apt-get update && \

  16. apt-get remove openssh-client -y && \

  17. apt-get install openssh-server vim -y && \

  18. apt-get clean


  19. # 设置root密码

  20. RUN echo "root:111111" | chpasswd


  21. # 设置root可登陆

  22. RUN echo "PermitRootLogin yes" >> /etc/ssh/sshd_config


  23. RUN mkdir -p /app


  24. # 将需要的python依赖写入到requirments中

  25. COPY requirements.txt /app


  26. WORKDIR /app


  27. # 阿里云加速

  28. RUN pip3 install --no-cache-dir -r requirements.txt -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com


  29. EXPOSE 22


  30. # ssh服务 daemon方式运行

  31. CMD /etc/init.d/ssh start -D

build

  1. docker build -t python-env:1.0 .

挂载使用

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

验证使用

  1. docker exec -it 609ed1fbbee5 bash

  2. root@609ed1fbbee5:/app# python3 --version

  3. Python 3.8.3

  4. root@609ed1fbbee5:/app# ps -ef |grep ssh

  5. root 1 0 0 07:49 ? 00:00:00 /bin/sh -c /etc/init.d/ssh start -D

  6. root 8 1 0 07:49 ? 00:00:00 /bin/sh /etc/init.d/ssh start -D

  7. root 17 8 0 07:49 ? 00:00:00 /usr/sbin/sshd -D

  8. root 31 18 0 07:51 ? 00:00:00 grep ssh

  9. root@609ed1fbbee5:/app#


各位伙伴们好,詹帅本帅搭建了一个个人博客和小程序,汇集各种干货和资源,也方便大家阅读,感兴趣的小伙伴请移步小程序体验一下哦!(欢迎提建议)

推荐阅读


牛逼!Python常用数据类型的基本操作(长文系列第①篇)

牛逼!Python的判断、循环和各种表达式(长文系列第②篇)

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

牛逼!Python错误、异常和模块(长文系列第④篇)


good-icon 0
favorite-icon 0
收藏
回复数量: 0
    暂无评论~~
    Ctrl+Enter