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
相关推荐
gaize121315 小时前
服务器怎么选择与配置才能满足企业需求?
运维·服务器·架构
德育处主任15 小时前
『NAS』在群晖部署图片压缩工具-Squoosh
前端·javascript·docker
鸠摩智首席音效师15 小时前
如何安装和配置 Nginx 反向代理服务器 ?
运维·nginx
Mr. Cao code16 小时前
Docker数据管理:持久化存储最佳实践
java·docker·容器
Shanxun Liao17 小时前
CentOS 7.9 根分区 / 已经 100% 用满隐藏占用解决办法
linux·运维·centos
FOREVER-Q17 小时前
Windows 下通过 SSH 替代 Gitee OAuth Token 推送配置指南
运维·服务器
盛夏52017 小时前
Docker容器化部署SpringBoot+Vue项目:从零到一在阿里云宝塔面板的实践指南
阿里云·docker·云计算
Cyber4K17 小时前
【Kubernetes专项】DockerFile、数据持计划、网络模式及资源配额
运维·网络·云原生·容器·kubernetes
ba_pi18 小时前
每天写点什么2026-01-09-linux基础
linux·运维·服务器
少云清18 小时前
【性能测试】3_性能测试基础 _指标
运维·服务器·数据库·性能测试·性能测试指标