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
相关推荐
索西引擎14 分钟前
基于 Docker 容器化的 MySQL 数据库部署与访问控制机制研究
数据库·mysql·docker
Ningcode_cloud22 分钟前
什么是CICD? GitLab + Jenkins 持续集成实战部署手册
运维·ci/cd·云原生·容器·gitlab·jenkins
好评12429 分钟前
【Linux】传输层协议UDP
linux·运维·udp
Csxyzj38 分钟前
基于Linux+Docker NAT的Ollama多实例负载均衡部署
运维·docker·容器
流光D42 分钟前
AI时代,搭建 web 站点并配置 nginx 反向代理流程
运维·服务器·前端·人工智能·nginx·ai·ai编程
GreatVicent1 小时前
AI Agent互操作标准化加速:A2A协议正式加入Linux基金会
linux·运维·人工智能·agent·aws·a2a
Huangjin007_1 小时前
【Linux 系统篇(十六)】进程(四) : 僵尸进程、孤儿进程、进程优先级
linux·运维·服务器
cui_hao_nan1 小时前
Git常用命令1
运维·git
瞬间&永恒~2 小时前
【Kubernetes】(十五)维护与升级、ETCD 备份和恢复
linux·运维·docker·云原生·kubernetes
LlmCraft|大模型工程实践2 小时前
12. Docker 日志管理与监控:ELK 日志收集 + Prometheus 性能监控
elk·docker·prometheus