docker制作镜像

一·、为ubuntu镜像提供ssh服务

复制代码
1、拉取镜像
docker pull ubuntu:18.04 

2、配置软件源
docker run --name c1 -it --rm ubuntu:18.04 bash

root@1293dcba98b1:/# mv /etc/apt/sources.list{,.bak} 
root@1293dcba98b1:/# echo deb http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse > /etc/apt/sources.list.d/aliyun.list
echo deb http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse  >> /etc/apt/sources.list.d/aliyun.list
echo deb http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse  >> /etc/apt/sources.list.d/aliyun.list
echo deb http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse  >> /etc/apt/sources.list.d/aliyun.list
echo deb http://mirrors.163.com/ubuntu bionic-backports main restricted universe multiverse  >> /etc/apt/sources.list.d/aliyun.list
echo deb-src http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse >> /etc/apt/sources.list.d/aliyun.list
echo deb-src http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse  >> /etc/apt/sources.list.d/aliyun.list
echo deb-src http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse  >> /etc/apt/sources.list.d/aliyun.list
echo deb-src http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse  >> /etc/apt/sources.list.d/aliyun.list
echo deb-src http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse  >> /etc/apt/sources.list.d/aliyun.list

3、安装配置ssh服务
root@1293dcba98b1:/# apt update
root@1293dcba98b1:/# apt install -y openssh-server
root@1293dcba98b1:/# mkdir -p /var/run/sshd
root@1293dcba98b1:/# /usr/sbin/sshd -D &
root@1293dcba98b1:/# ps -ef | grep ssh
root@1293dcba98b1:/# cat  /etc/pam.d/sshd

取消pam登录权限
root@1293dcba98b1:/# sed -i 's/session    required     pam_loginuid.so/#session    required     pam_loginuid.so/' /etc/pam.d/sshd
root@1293dcba98b1:/# grep pam_loginuid /etc/pam.d/sshd

3、配置免密钥登录
[root@openEuler-2~]#ssh-keygen -f ~/.ssh/id_rsa -P '' -q  (复制在另一个窗口操作)
容器操作:公钥追加到容器的 ~/.ssh/autorized_keys
[root@openEuler-2~]#ls -a /root/

[root@openEuler-2 .ssh]# more  id_rsa.pub 

root@1293dcba98b1:/# mkdir ~/.ssh/
root@1293dcba98b1:/# echo 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCz3Jv18MMMg7P55G8qwaOt6mfqtfhbgKAthIb9fVWJ295a4ttoaauZXuCckq257oyNNcEe83VOoJKWiLN
CcFWFgfl9kVRm9pSqT9tIFDMYAVw/LH5y2oOAMGi9Snq4yardUkRohAPDO428ZEMmAFoYU5HqWyQKrckAIXP4NWXDpxUQEeCucn4dEprw7JhieUf8nnkeQo
AnufB4yxyGihTXhhUveXQ7ZUmq5SGN1oipKS4n59xBG9Cw3bGH4iLf9kavVeFbeIWaV+24J0Rwm1CVAf9fwpwg4PlsvYOkEj19n8EpHfVfXt7ICeizhBGT5
qHZjdmjT7EX2C7RNoNPNQJeAvJZ9kRzW7LPPNUM3DfoeBZ6A3S5WdxDF0kWckYwu1jnE19mOd35daX/uW5KWLZYszDDAiPoKlV1YDHlwzFCO/tbN9f0TeS1
IpiL+MeQQJStVwI9yMLQfNUJ0g1Rl18tQpjnvV5LS9BjIMJmyzoThh7wQEuHBlCiiUDYyptQH+E= root@openEuler-2' > /root/.ssh/authorized_keys

服务启动脚本:
root@1293dcba98b1:/# echo '#!/bin/bash' > /run.sh
root@1293dcba98b1:/# echo '/usr/sbin/sshd -D' >> /run.sh
root@1293dcba98b1:/# chmod  +x  /run.sh

5、提交镜像
[root@openEuler-2~]#docker commit c1 registry.openlab.cn/openlab/sshd:ubuntu_v1
[root@openEuler-2~]#docker push registry.openlab.cn/openlab/sshd:ubuntu_v1

6、验证镜像
[root@openEuler-2~]#docker run -d -p 10022:22 registry.openlab.cn/openlab/sshd:ubuntu_v1 /run.sh

[root@openEuler-2~]#ssh 192.168.183.11 -p 10022

二、基于ubuntu:18.04 构建一个宿主机免密钥登录

复制代码
1、创建目录
mkdir ubuntu
Cd ubuntu
2、编辑Dockerfile文件
[root@openEuler-2 ubuntu]# cat Dockerfile
FROM ubuntu:18.04
MAINTAINER openlab <openlab@123.com>

RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak
COPY 163.list /etc/apt/sources.list.d/
RUN apt update && apt install -y openssh-server && mkdir -p /var/run/ssh
RUN sed -ri 's/session    required     pam_loginuid.so/#session    required     pam_loginuid.so/' /etc/pam.d/sshd
COPY run.sh /run.sh
RUN chmod +x /run.sh && mkdir /root/.ssh
COPY autorized_keys /root/.ssh/autorized_keys

EXPOSE 22/TCP
CMD ["/run.sh"]

3、需要cope到容器里面的文件
[root@openEuler-2 ubuntu]# cat 163.list
deb http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu bionic-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse

4、构建
[root@openEuler-2 ubuntu]# docker build -t sshd:v2 .
[root@openEuler-2 ubuntu]# docker run -d --name c1 -p 11222:22 sshd:v2
[root@openEuler-2 ubuntu]# docker ps
[root@openEuler-2 ubuntu]# ssh 192.168.183.11 -p 11222
相关推荐
虎头金猫2 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
分布式存储与RustFS2 天前
MinIO 官方 Docker 镜像被移除:依赖它的项目该怎么办
docker·云原生·devops·对象存储·minio·分布式存储
AI职业加油站2 天前
AI智能体应用工程师证书:政策红利下的职业新风口
大数据·运维·人工智能·学习·职场发展
此冬歌咏2 天前
K8s 节点故障实战:优雅驱逐 31 秒,硬故障 331 秒,以及那个永远 Pending 的 Pod
运维·k8s
玉&心2 天前
通过Arthas在线诊断K8S中的内存及JVM等使用情况
docker·k8s·arthas
xing-xing2 天前
Docker容器中Nginx站点根目录网页配置访问
nginx·docker
-梅2 天前
linux(8) 软硬链接
linux·运维·服务器
赵文宇(温玉)2 天前
CoreDNS的hosts插件的缺行配置导致k8s集群异常
容器·kubernetes·rancher
guo_wen_qiang2 天前
上传本地镜像到harbor中
docker·容器·持续部署
张洛闻Eren2 天前
k8s云原生【第十课】:水平 Pod 自动扩缩容
运维·数据库·云原生·kubernetes·github