ref: https://www.smslit.top/2018/12/20/docker_ubuntu_learn/
容器版本:
登陸 ubuntu 容器的 bash 中,執行命令
cat /etc/issue
可以查看系統版本root@ea66d7a89cfa:/home# cat /etc/issue
Ubuntu 18.04.2 LTS \n \l
安裝git
apt-get install git
The following packages have unmet dependencies:
git : Depends: perl but it is not going to be installed
Depends: liberror-perl but it is not going to be installed
E: Unable to correct problems, you have held broken packages.
apt-get install perl
The following packages have unmet dependencies:
perl : Depends: perl-base (= 5.26.1-6) but 5.26.1-6ubuntu0.3 is to be installed
Recommends: netbase but it is not going to be installed
E: Unable to correct problems, you have held broken packages.
apt-get install perl-base
Reading package lists... Done
Building dependency tree
Reading state information... Done
perl-base is already the newest version (5.26.1-6ubuntu0.3).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
apt-get install perl
The following packages have unmet dependencies:
perl : Depends: perl-base (= 5.26.1-6) but 5.26.1-6ubuntu0.3 is to be installed
Recommends: netbase but it is not going to be installed
E: Unable to correct problems, you have held broken packages.
解決辦法:
安裝指定版本的 perl-base
apt install -f perl-base=5.26.1-6 apt-get install perl apt-get install liberror-perl apt-get install git
root@ea66d7a89cfa:/home# git --version
git version 2.17.0
安裝VIM
apt-get install vim
安裝python3
apt-get install python3
root@ea66d7a89cfa:/home# python3
Python 3.6.5 (default, Apr 1 2018, 05:46:30)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit
Use exit() or Ctrl-D (i.e. EOF) to exit
>>>
配置 SSH
目標:讓mac 可以 ssh 連接 ubuntu 容器
安裝 openssh-server(用於開啟 ssh 服務供外部連接)
需要更改一下 sshd 的默認配置,編輯文件 /etc/ssh/sshd_config
,大概從 29 行開始主要更改三處,更改后內容如下:
PermitRootLogin yes # 可以登錄 root 用戶 PubkeyAuthentication yes # 可以使用 ssh 公鑰許可 AuthorizedKeysFile .ssh/authorized_keys # 公鑰信息保存到文件 .ssh/authorized_keys 中
重啟 sshd
因為 ubuntu 過於精簡,不能使用 service 命令方便的重啟 sshd,這里使用命令 /etc/init.d/ssh restart
進行重啟,重啟是為了讓上面的配置生效。
添加主機的 ssh 公鑰
- 在 HOME 目錄下創建
.ssh
目錄:mkdir ~/.ssh
- 新建文件
~/.ssh/authorized_keys
:touch ~/.ssh/authorized_keys
- 新開一個 macOS 下的終端窗口,執行命令
cat ~/.ssh/id_rsa.pub
,復制打印的一行公鑰信息 - 回到 ubuntu 容器中,將第 3 步復制的公鑰粘貼到
~/.ssh/authorized_keys
中保存。
如果希望使用ssh免密碼的登陸操作,使用ssh的密鑰生成方法。
此時完成了 SSH 訪問支持的添加,ctrl
+ d
退出容器。
提交修改到鏡像
目標:產生新的鏡像版本
獲取剛剛創建的容器的id
docker ps -l -q
返回 ea66d7a89cfa
或者使用命令:(根據容器名查找對應id)
docker ps -a
提交產生 ubuntu 新版本的鏡像
docker commit -m 'add ssh' -a 'dockergx' ea66d7a89cfa ubuntu-ssh
- -m,指定提交信息
- -a,指定提交者
- 容器的
CONTAINER ID
- ubuntu-ssh 是新鏡像的名稱,可以隨意指定
查看當前安裝的鏡像
docker image ls
上述操作正常的話就會看到 ubuntu-ssh
的鏡像信息
最終的 ubuntu 容器
創建新的 ubuntu 容器
docker run -d -p 26122:22 --name learn ubuntu-ssh /usr/sbin/sshd -D
參數 | 值 | 含義 |
---|---|---|
-d | 無 | 后台運行 |
-p | 26122:22 | 綁定主機的 26122 端口到ubuntu容器的 22 端口(ssh服務的默認端口為 22) |
–name | learn | 指定容器名稱為 learn |
ubuntu-ssh | 無 | 使用鏡像 ubuntu-ssh 創建容器 |
/usr/sbin/sshd -D | 無 | 指定容器啟動使用的應用及參數 |
在 macOS 的終端中執行命令 ssh -p 26122 root@localhost
即可連接已經啟動的 ubuntu 容器 learn
為了更方便的連接,可以為容器創建 ssh 連接的主機短名,往 macOS 的 ~/.ssh/config
中添加以下內容
Host learn HostName localhost User root Port 26122
此時就可以通過命令 ssh learn
連接 ubuntu 容器 learn 了。