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
相关推荐
java_logo38 分钟前
Apache Doris Docker 部署指南:实时分析数据库实战
数据库·docker·apache·doris·apache doris·轩辕镜像·docker部署doris
码农学院1 小时前
基于运维监控体系的网络品牌推广方案:从架构设计到技术实现
运维·网络
大耳朵-小飞象10 小时前
电力安全运维的智能密码:BACS如何破解设备全生命周期管理难题,让电网安全“看得见、管得住”?
运维·安全·智慧城市·能耗系统·楼宇智控·未来生活
极客侃科技10 小时前
制造企业 MES/APS 选型:SAP PP/DS 集成、ERP-MES 边界划分与一体化架构要点
运维·架构·制造
生活爱好者!13 小时前
我把NAS当作下载机,docker一键部署qb
运维·docker·容器
java_logo14 小时前
Docker Compose 部署 ClickHouse:快速搭建高性能列式数据库
数据库·clickhouse·docker·列式数据库·轩辕镜像·高性能数据库·clickhouse部署教程
MC皮蛋侠客14 小时前
uv 系列(七):CI/CD、Docker 与私有索引——生产级交付
python·ci/cd·docker·uv
尽兴-15 小时前
企业业务系统架构选型与渐进式演进
运维·系统架构·devops
Hui Baby15 小时前
K8S使用CRD和控制器简单应用
云原生·容器·kubernetes
通信小小昕15 小时前
Ubuntu 26.04 中文输入法安装
linux·运维·ubuntu