mac 使用 Docker 搭建 ubuntu 环境

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

扫描二维码关注公众号,回复: 6788660 查看本文章

安装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 公钥

  1. 在 HOME 目录下创建 .ssh 目录:mkdir ~/.ssh
  2. 新建文件 ~/.ssh/authorized_keys :touch ~/.ssh/authorized_keys
  3. 新开一个 macOS 下的终端窗口,执行命令 cat ~/.ssh/id_rsa.pub,复制打印的一行公钥信息
  4. 回到 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 了。

猜你喜欢

转载自www.cnblogs.com/GuoXinxin/p/11162581.html
今日推荐