创建能SSH远程连接的Centos7镜像
官网拉取最新的Centos7镜像
官网地址
用改镜像启动容器
docker run -it centos:centos7.9.2009
进入容器
docker exec -it [ImageID] bash
一顿安装
### yum更新
yum update -y
### 安装net-tools
yum install -y net-tools.x86_64 vim
### 安装passwd openssl openssh-server
yum install passwd openssl openssh-server -y
### 生成公钥,私钥
ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N ''
ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ''
ssh-keygen -t dsa -f /etc/ssh/ssh_host_ed25519_key -N ''
/usr/sbin/sshd -D&
echo "/usr/sbin/sshd -D" >> /etc/rc.local
### 更新密码并退出
passwd root
exit
### 更改pip源
mkdir ~/.pip && vi pip.conf
### 填入
[global]
index-url = http://pypi.douban.com/simple
[install]
use-mirrors =true
mirrors =http://pypi.douban.com/simple/
trusted-host =pypi.douban.com
### 安装pip
yum install epel-release -y && yum install python-pip -y
提交生成新的镜像
OPTIONS说明:
-a :提交的镜像作者;
-c :使用Dockerfile指令来创建镜像;
-m :提交时的说明文字;
-p :在commit时,将容器暂停。
docker commit --change='CMD ["python", "main.py"]'-p a7e589406191 dockerhub.datagrand.com/rec_product/centos_base:1.0
--change 可以改变commit之后的command操作, (这个操作可以用docker inspect ImagesID查看)
再次运行, 并用SSH连接
docker run -p 10022:22 -d sshd-centos /usr/sbin/sshd -D
推送镜像到阿里云仓库
阿里云镜像仓库指导
基于官方的Python镜像, 添加ssh远程登陆的Dockerfile
FROM python:3.7.12
RUN sed -i 's/http:\/\/deb.debian.org/https:\/\/mirrors.ustc.edu.cn/g' /etc/apt/sources.list \
&& apt-get update --allow-releaseinfo-change \
&& apt-get -y install openssh-server vim curl gcc g++ passwd locales openssl net-tools \
&& mkdir /run/sshd \
&& mkdir -p /root/.pip/ \
&& echo root:root |chpasswd \
&& sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config \
&& cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& localedef -i zh_CN -c -f UTF-8 -A /usr/share/locale/locale.alias zh_CN.UTF-8 \
&& export LANG=zh_CN.utf8 \
&& export LC_ALL=zh_CN.UTF-8
COPY pip.conf /root/.pip/pip.conf
COPY .vimrc /root/.vimrc
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
cat > ~/.pip/pip.conf << EOF
[global]
timeout=6000
index-url = http://pypi.douban.com/simple/
[install]
use-mirrors=true
mirrors = http://pypi.douban.com/simple/
trusted-host = pypi.douban.com
EOF
cat > ~/.vimrc << EOF
if has('mouse')
set mouse-=a
endif
syntax on
set paste
set hlsearch
set showmatch
set completeopt=longest,preview,menu
set fileencodings=utf-8
set termencoding=utf-8
set langmenu=zh_CN.UTF-8
syntax enable
set helplang=cn
set expandtab
set tabstop=4
set si
set softtabstop=4
EOF
基于centos7自己搭建Python的Base镜像
FROM centos:7
ARG PYTHON_VERSION=3.8.12
RUN mkdir -p /tmp/python-src/Python-$PYTHON_VERSION
WORKDIR /tmp/python-src/Python-$PYTHON_VERSION
RUN yum check-update || true \
&& yum update -y \
&& yum -y groupinstall "Development tools" \
&& yum -y install wget zlib-devel bzip2-devel openssl-devel ncurses-devel libxml2-devel libxslt-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libjpeg-devel passwd openssh-server vim kde-l10n-Chinese telnet net-tools \
&& mkdir /run/sshd \
&& mkdir -p /root/.pip/ \
&& echo root:root |chpasswd \
&& sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config \
&& cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& localedef -i zh_CN -c -f UTF-8 -A /usr/share/locale/locale.alias zh_CN.UTF-8 \
&& wget --no-check-certificate https://www.python.org/ftp/python/$PYTHON_VERSION/Python-$PYTHON_VERSION.tgz -O /tmp/python.tgz \
&& tar -zxvf /tmp/python.tgz -C /tmp/python-src \
&& ./configure --prefix=/usr/local LDFLAGS="-Wl,-rpath /usr/local/lib" --with-ensurepip=install --enable-optimizations --with-lto \
&& make \
&& make altinstall \
&& rm -rf /tmp/python-src /tmp/python.tgz \
&& yum -y groupremove "Development tools" \
&& yum -y remove wget zlib-devel bzip2-devel openssl-devel ncurses-devel libxml2-devel libxslt-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libjpeg-devel \
&& yum clean all \
&& localedef -c -f UTF-8 -i zh_CN zh_CN.utf8 \
&& export LC_ALL=zh_CN.UTF-8
COPY pip.conf /root/.pip/pip.conf
COPY .vimrc /root/.vimrc
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]/
参考
vim配置
Docker 操作:基于 Centos7 构建 python3 镜像