在macOS使用docker搭建ubuntu环境

ubuntu 虚拟机,一般就是在 mac 中 ssh 连接 ubuntu 虚拟机在终端下进行操作学习,安装一个包含完整GUI的 ubuntu 有点多余,还占用很多资源,所以想到了使用 docker 来创建 ubuntu 容器用来开发学习

  1. 下载离线安装版本:docker for mac:官网 阿里云

    image-20200831150033178

  2. 安装完成之后,在启动台打开它,之后就可以在状态栏找到它

    image-20200831150324874

  3. 终端输入docker,检查安装是否成功

    image-20200831151805174

  4. 拉取指定版本的ubuntu镜像,终端输入docker pull ubuntu:16.04

    image-20200831152933949

  5. 使用镜像新建容器,使用命令 docker run -it --name Ubuntu16 ubuntu bash 可以创建并运行一个可以使用终端交互的 ubuntu 容器,命令参数解释:

    参数 含义
    -i 可以输入进行交互
    -t 终端交互
    –name mineos 指定容器名称为 mineos
    ubuntu 指定使用镜像
    bash 指定容器启动使用的应用

    上面的命令执行后,就会登陆 ubuntu 容器的 bash 中,执行命令cat /etc/issue 可以查看系统版本,十里的ubuntu版本是 18.04。此时按快捷键组合 ctrl + d 就会退出 ubuntu 容器,此时就会停止容器运行。

  6. 查看目前本地的容器,终端输入docker ps -a

    image-20200831153311753

  7. 启动容器,docker start ubuntu16

  8. 进入容器,docker exec -it ubuntu16 bash

    image-20200831153559875

  9. 安装必须的Linux工具,如vim、net-tools、wget等,可使用apt-get安装

    1. 更新软件源信息:apt-get update

    2. 因为这个 ubuntu 的依赖镜像太精简了,所以好多工具没有安装,先安装一下 vim: apt-get install vim

    3. 可以看到安装挺慢的,之所以先安装 vim 是为了可以编辑 /etc/apt/sources.list 更换为国内访问更快的软件源,比如将文件中的内容替换为如下阿里云的:

      deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
      deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
      deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
      deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
      deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
      deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
      deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
      deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
      deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
      deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
      
    4. 重新更新软件源信息:apt-get update,会发现快很多

    5. 飞一般的安装 git 和 python3:apt-get install git python3

  10. 完结,撒花🎉


702 字