Docker 基於Ubuntu鏡像添加SSH服務


這里我們用Ubuntu18.04作為基礎鏡像。

基於Commit命令創建:

1、准備工作

首先,獲取Ubuntu:18.04鏡像並創建一個容器:

docker pull ubuntu:18.04

docker run -it ubuntu:18.04 bash

root@09c5e8c04b11:~# 

2、配置軟件源 

root@09c5e8c04b11:~#  apt-get update

3、安裝和配置SSH服務

root@09c5e8c04b11:~#  apt-get install openssh-server

果需要正常啟動 SSH 服務, 目錄/var/run/sshd 必須存在 下面手動創建它,並啟動SSH 服務:

root@09c5e8c04b11:~#  mkdir -p /var/run/sshd

root@09c5e8c04b11:~#  /usr/sbin/sshd -D &

安裝net-tools查看容器22端口(SSH服務默認監聽端口),可見端口已經處於監聽狀態。

root@09c5e8c04b11:~#  apt-get install net-tools

root@09c5e8c04b11:~#  netstat -tunlp

Active Internet connections (only servers)
Proto Recv-Q   Send-Q         Local Address    Foreign   Address State   PID/Program name
tcp 0      0          0        0.0.0.0:22          0.0.0.0:*       LISTEN           1/sshd
tcp6      0           0       :::22             :::*       LISTEN      1/sshd

修改SSH服務的安全配置,取消pam登錄限制:

root@09c5e8c04b11:~# sed -ri 's/session required pam_loginuid.so/#session required pam_loginuid.so/g' /etc/pam.d/sshd

在root目錄下創建.ssh目錄,並使用ssh-keygen命令生成秘鑰。

給容器root設置密碼

root@09c5e8c04b11:~#passwd root

Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

修改配置讓root能登錄

root@09c5e8c04b11:~#vim /etc/ssh/sshd_config

注釋

# PermitRootLogin prohibit-password.

添加

PermitRootLogin yes

保存,退出,重啟SSH服務

root@09c5e8c04b11:~#/etc/init.d/ssh restart

到目前可以通過密碼ssh到容器

root@09c5e8c04b11:~# ifconfig

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.5 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:05 txqueuelen 0 (Ethernet)
RX packets 3184 bytes 349005 (349.0 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2908 bytes 341429 (341.4 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

root@09c5e8c04b11:~# exit

[root@5gd51 ~]#  ssh 172.17.0.5

root@172.17.0.5's password:輸入密碼可以進入

root@09c5e8c04b11:~# exit 

退出,復制需要登錄的公鑰信息到 authorized_keys文件中

[root@5gd51 ~]#ssh-copy-id -i /root/.ssh/id_rsa.pub 172.17.0.5

這種方式是為了保證文件權限准確

[root@5gd51 ~]#ssh 172.17.0.5

root@09c5e8c04b11:~#

OK,到此SSH服務成功配置。

然后,在容器中新建腳本/run.sh

root@09c5e8c04b11:~# apt-get install vim 

root@09c5e8c04b11:~#  vim /run.sh

run.sh腳本如下:

#!/bin/bash

/usr/sbin/sshd -D

root@09c5e8c04b11:~#chmod +x /run.sh

最后,退出容器

root@09c5e8c04b11:~# exit

4、保存鏡像

[root@5gd51 ~]#docker commit  09c5e8c04b11 sshd:ubuntu

sha256:5998511380c52caa60a54fa75a28dc258f387b72aea41ffaa9c8c2ad209c8399

[root@5gd51 ~]#docker images

REPOSITORY   TAG   IMAGE ID         CREATED     SIZE
sshd         ubuntu  5998511380c5       20 hours ago    245MB

5、使用鏡像

[root@5gd51 ~]#docker run -p 10022:22 -d  sshd:ubuntu  /run.sh

3bfc78853f924097ab494c11683cfc444402df1c370df02d1d791bb3dca736a8

[root@5gd51 ~]# ssh 101.33.22.44(公網IP) -p 10022

Welcome to Ubuntu 18.04.4 LTS (GNU/Linux 3.10.0-693.el7.x86_64 x86_64)

* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage

This system has been minimized by removing packages and content that are
not required on a system that users do not log into.

To restore this content, you can run the 'unminimize' command.
Last login: Fri Jul 24 05:59:55 2020 from 172.17.0.1

root@3bfc78853f92:~#

成功用SSH連接

參考:《Docker技術入門與實戰第三版》

博客:https://blog.csdn.net/qq_27068845/article/details/77015432


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM