基于Dockerfile制作基于ubuntu启动ssh服务的镜像

1.  创建目录并编写Dockerfile文件:

[root@node1 ~]# mkdir ubuntu_ssh

[root@node1 ~]# cd ubuntu_ssh/

[root@node1 ubuntu_ssh]# vim Dockerfile

FROM ubuntu:18.04
MAINTAINER <xxq@123.com>

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

EXPOSE 22/tcp  ##暴露22号端口
CMD ["/run.sh"]  ##容器启动命令

2.  准备Dockerfile中需要复制到容器中文件:

## 准备容器中镜像文件:
[root@node1 ubuntu_ssh]# vim 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

## 准备公钥文件:
[root@node1 ubuntu_ssh]# cp ~/.ssh/id_rsa.pub authorized_keys

## 准备服务启动文件:
[root@node1 ubuntu_ssh]# vim run.sh 
#!/bin/bash
/usr/sbin/sshd -D

3.  构建镜像:

## Dockerfile构建镜像时,如果文件名为"Dockerfile"就不用使用-f选项指定文件,直接写"./'即可
## 如果不是"Dockerfile",就需要-f选项指定文件
[root@node1 ubuntu_ssh]# docker build -t node2.openlab.com/demo/ssh:ubuntu_v2 ./

4.  查看镜像:

[root@node1 ubuntu_ssh]# docker images

REPOSITORY                       TAG           IMAGE ID       CREATED          SIZE
node2.openlab.com/demo/ssh       ubuntu_v2     3729d72faf3e   32 seconds ago   247MB

5.  将镜像推送到Harbor上:

[root@node1 ubuntu_ssh]# docker push node2.openlab.com/demo/ssh:ubuntu_v2

 

6.  运行镜像:

[root@node1 ubuntu_ssh]# docker run -d -p 10023:22 node2.openlab.com/demo/ssh:ubuntu_v2

7.  验证ssh服务是否配置成功:

## 查看10023端口是否监听
[root@node1 ubuntu_ssh]# ss -lntup | grep 10023
tcp    LISTEN     0      128       *:10023                 *:*                   users:(("docker-proxy",pid=11368,fd=4))
tcp    LISTEN     0      128    [::]:10023              [::]:*                   users:(("docker-proxy",pid=11372,fd=4))

## 验证ssh是否可以免密钥链接10023
[root@node1 ubuntu_ssh]# ssh 192.168.188.111 -p 10023

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值