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