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
相关推荐
m0_7381207210 小时前
Docker 环境下 Vulfocus 靶场搭建全流程(附镜像源问题解决方案)
运维·服务器·网络·安全·docker·容器
simeple10 小时前
记一次 Docker Compose 项目迁移:从 Windows Docker Desktop 迁移到 CentOS 服务器
docker
二等饼干~za89866810 小时前
2026 主流 GEO 源码厂商实测:云罗 GEO、摘星智能、棋引科技技术与落地能力对比
大数据·运维·科技
Championship.23.2410 小时前
Linux 3.0 音频机制深度解析:ALSA基础架构与传统音频驱动模型
linux·运维·音视频·alsa
哆啦A梦——11 小时前
Ubuntu 虚拟机 Docker 与 MySQL 8.0.42 部署指南
mysql·ubuntu·docker
无证驾驶梁嗖嗖11 小时前
ubuntu下测试nvme带宽和健康度
运维
Plastic garden11 小时前
K8s知识(3) Pod亲和性,调度
云原生·容器·kubernetes
HLC++11 小时前
Linux文件操作
linux·运维·服务器
InfraSense11 小时前
多门店运维闭环全景架构:监控+告警+工单+SLA+复盘,一套最小可用系统怎么串起来
运维·msp