写点什么

(VMware)ubuntu 环境下搭建 docker 环境

用户头像
逸少
关注
发布于: 2021 年 06 月 13 日
(VMware)ubuntu 环境下搭建docker环境

本文主要讲述如何在 win10 系统上,使用 vmware 安装 Ubuntu 虚拟机,并搭建 docker 环境


版本约定

  • 试验电脑系统为 Windows 10 专业版

  • VMware 版本为 VMware Workstation 16 Pro

  • Ubuntu 版本为 Ubuntu 20.04.2 LTS

  • Docker 版本为 Docker version 20.10.6

  • docker-compose 版本为 docker-compose version 1.29.2

软件包准备

  • 下载 VMware Workstation 16 Pro ,笔者下载的版本是:VMware-workstation-full-16.1.1-17801498.exe

  • 下载 ubuntu镜像 ,笔者下载的版本是:ubuntu-20.04.2-live-server-amd64.iso

环境设置

由于笔者电脑之前安装了Docker Desktop环境,开启了 Hyper-V 功能,因此在 VMware 中安装 Ubuntu 虚拟机时报如下的错误:


VMware Workstation 与 Device/Credential Guard 不兼容.在禁用 Device/Credenti


解决此错误可以按照此文中的方案进行设置,方可解决~

VMware workstation 安装

VMware workstation 的安装过程比较简单,网上的教程也比较多,笔者这边只讲重要步骤作详细阐述!

Ubuntu 虚拟机安装

  • 创建新的虚拟机


  • 选择典型(推荐)(T)配置即可


  • 设置 Ubuntu 镜像文件


  • 设置用户名密码信息


  • 设置虚拟机名称以及位置信息


  • 设置磁盘相关配置,默认即可

  • 选中创建后开启此虚拟机

开启虚拟机后虚拟机则开始进行安装设置过程,安装过程基本上可以按照默认设置安装,其中有两个比较重要的设置如下:

  • 设置 server name、username

  • 选择安装 OpenSSH server(方便后续使用 ssh 工具进行连接)


  • 查看系统版本信息

yishao@yishao:~$ uname -aLinux yishao 5.4.0-73-generic #82-Ubuntu SMP Wed Apr 14 17:39:42 UTC 2021 x86_64 x86_64 x86_64 GNU/Linuxyishao@yishao:~$ cat /proc/versionLinux version 5.4.0-73-generic (buildd@lcy01-amd64-019) (gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)) #82-Ubuntu SMP Wed Apr 14 17:39:42 UTC 2021
复制代码

Ubuntu 虚拟机设置

时区设置

  • 执行 sudo tzselect,并按照提示选择对应的设置项

yishao@yishao:~$ sudo tzselect[sudo] password for yishao:Please identify a location so that time zone rules can be set correctly.Please select a continent, ocean, "coord", or "TZ".1) Africa                                                            7) Europe2) Americas                                                          8) Indian Ocean3) Antarctica                                                        9) Pacific Ocean4) Asia                                                             10) coord - I want to use geographical coordinates.5) Atlantic Ocean                                                   11) TZ - I want to specify the timezone using the Posix TZ format.6) Australia#? 4Please select a country whose clocks agree with yours.1) Afghanistan            10) Cyprus                19) Japan                 28) Macau                 37) Qatar                 46) Turkmenistan2) Armenia                11) East Timor            20) Jordan                29) Malaysia              38) Russia                47) United Arab Emirates3) Azerbaijan             12) Georgia               21) Kazakhstan            30) Mongolia              39) Saudi Arabia          48) Uzbekistan4) Bahrain                13) Hong Kong             22) Korea (North)         31) Myanmar (Burma)       40) Singapore             49) Vietnam5) Bangladesh             14) India                 23) Korea (South)         32) Nepal                 41) Sri Lanka             50) Yemen6) Bhutan                 15) Indonesia             24) Kuwait                33) Oman                  42) Syria7) Brunei                 16) Iran                  25) Kyrgyzstan            34) Pakistan              43) Taiwan8) Cambodia               17) Iraq                  26) Laos                  35) Palestine             44) Tajikistan9) China                  18) Israel                27) Lebanon               36) Philippines           45) Thailand#? 9Please select one of the following timezones.1) Beijing Time2) Xinjiang Time#? 1
The following information has been given:
China Beijing Time
Therefore TZ='Asia/Shanghai' will be used.Selected time is now: Sun Jun 13 20:26:30 CST 2021.Universal Time is now: Sun Jun 13 12:26:30 UTC 2021.Is the above information OK?1) Yes2) No#? 1
You can make this change permanent for yourself by appending the line TZ='Asia/Shanghai'; export TZto the file '.profile' in your home directory; then log out and log in again.
Here is that TZ value again, this time on standard output so that youcan use the /usr/bin/tzselect command in shell scripts:Asia/Shanghai
复制代码
  • 创建时区软连接

yishao@yishao:~$ sudo ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
复制代码
  • 执行时区生效命令

yishao@yishao:~$ timedatectl               Local time: Sun 2021-06-13 20:28:42 CST           Universal time: Sun 2021-06-13 12:28:42 UTC                 RTC time: Sun 2021-06-13 12:28:41                Time zone: Asia/Shanghai (CST, +0800)System clock synchronized: yes              NTP service: active          RTC in local TZ: no
复制代码

hostname 设置

  • 查看当前 hostname

yishao@yishao:~$ hostnamectl   Static hostname: yishao         Icon name: computer-vm           Chassis: vm        Machine ID: 648933e419cc48babcfd31a53a7a6ad6           Boot ID: b84f37318ebc436c96c4ece89ae920dd    Virtualization: vmware  Operating System: Ubuntu 20.04.2 LTS            Kernel: Linux 5.4.0-73-generic      Architecture: x86-64
复制代码
  • 设置 hostname

yishao@yishao:~$ sudo hostnamectl set-hostname node1yishao@yishao:~$ hostnamectl   Static hostname: node1         Icon name: computer-vm           Chassis: vm        Machine ID: 648933e419cc48babcfd31a53a7a6ad6           Boot ID: b84f37318ebc436c96c4ece89ae920dd    Virtualization: vmware  Operating System: Ubuntu 20.04.2 LTS            Kernel: Linux 5.4.0-73-generic      Architecture: x86-64
复制代码
  • 查看/etc/hosts

yishao@yishao:~$ cat /etc/hosts127.0.0.1 localhost127.0.1.1 yishao         #此处yishao需要修改为node1
# The following lines are desirable for IPv6 capable hosts::1 ip6-localhost ip6-loopbackfe00::0 ip6-localnetff00::0 ip6-mcastprefixff02::1 ip6-allnodesff02::2 ip6-allrouters
复制代码
  • 设置新的映射

yishao@yishao:~$ sudo vi /etc/hosts
复制代码

将/etc/hosts 文件中原来 127.0.1.1 yishao 修改为 127.0.1.1 node1,如下:

127.0.0.1 localhost127.0.1.1 node1   # 新的映射
# The following lines are desirable for IPv6 capable hosts::1 ip6-localhost ip6-loopbackfe00::0 ip6-localnetff00::0 ip6-mcastprefixff02::1 ip6-allnodesff02::2 ip6-allrouters
复制代码

静态 IP 设置

修改/etc/netplan/00-installer-config.yaml,设置静态 IP

yishao@yishao:~$ sudo vi /etc/netplan/00-installer-config.yaml
复制代码

将文件编辑为如下内容(addresses、gateway4、nameservers 根据实际情况设置):

# This is the network config written by 'subiquity'network:  ethernets:    ens33:      dhcp4: false      addresses: [192.168.57.131/24]      gateway4: 192.168.57.2      nameservers:        addresses: [192.168.57.2,114.114.114.114,8.8.8.8]  version: 2
复制代码

生效配置,并重启虚拟机

yishao@yishao:~$ sudo netplan applyyishao@yishao:~$ sudo reboot
复制代码

Docker 安装

  • Uninstall old versions

yishao@node1:~$ sudo apt-get remove docker docker-engine docker.io containerd runc
复制代码
  • Update the apt package index and install packages to allow apt to use a repository over HTTPS

yishao@node1:~$ sudo apt-get updateyishao@node1:~$ sudo apt-get install \>     apt-transport-https \>     ca-certificates \>     curl \>     gnupg \>     lsb-release
复制代码
  • Add Docker’s official GPG key

yishao@node1:~$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
复制代码
  • Use the following command to set up the stable repository. To add the nightly or test repository, add the word nightly or test (or both) after the word stable in the commands below. Learn about nightly and test channels.

yishao@node1:~$ echo \>   "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \>   $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
复制代码
  • Update the apt package index, and install the latest version of Docker Engine and containerd, or go to the next step to install a specific version

yishao@node1:~$ sudo apt-get updateyishao@node1:~$ sudo apt-get install docker-ce docker-ce-cli containerd.io
复制代码
  • install docker-ce docker-ce-cli containerd.io

yishao@node1:~$ sudo apt-get install docker-ce docker-ce-cli containerd.io
复制代码
  • 执行如下命令配置镜像加速器(阿里)

sudo mkdir -p /etc/dockersudo tee /etc/docker/daemon.json <<-'EOF'{  "registry-mirrors": ["https://9w2ypf5d.mirror.aliyuncs.com"]}EOFsudo systemctl daemon-reloadsudo systemctl restart docker
复制代码
  • 运行hello-world 镜像,验证环境

yishao@node1:~$ sudo docker run hello-world
Hello from Docker!This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.
To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/
For more examples and ideas, visit: https://docs.docker.com/get-started/
复制代码


看到如上输出,表示 docker 环境已经安装成功~

Docker 环境设置

  • 添加当前用户到 docker 用户组,可以不用 sudo 运行 docker

yishao@node1:~$ sudo groupadd dockeryishao@node1:~$ sudo usermod -aG docker yishao
复制代码

Docker-compose 安装

  • Run this command to download the current stable release of Docker Compose

yishao@node1:~$ sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
复制代码
  • Apply executable permissions to the binary:

yishao@node1:~$ sudo chmod +x /usr/local/bin/docker-compose
复制代码


Note: If the command docker-compose fails after installation, check your path. You can also create a symbolic link to /usr/bin or any other directory in your path.


yishao@node1:~$ sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
复制代码
  • 测试 docker-compose 环境

yishao@node1:~$ docker-compose --versiondocker-compose version 1.29.2, build 5becea4c
复制代码

看到如上输出,表示 docker compose 环境已经安装成功~


克隆虚拟机

经过以上步骤完整的 Ubuntu 虚拟机+docker 环境已经安装设置完成,为了以后能够方便使用,建议对此 VM 镜像克隆备份,克隆有几个需要注意的地方如下:

  • 克隆时请选择 创建完整克隆

  • 克隆完成后请重新生成 MAC 地址 (重要)

  • 后续可以根据实际需要重新设置静态 IP 和 hostname


参考:

https://www.cnblogs.com/oneWhite/p/12522050.html

https://blog.csdn.net/fansnn/article/details/105931067

https://docs.docker.com/engine/install/ubuntu/

https://docs.docker.com/compose/install/


发布于: 2021 年 06 月 13 日阅读数: 39
用户头像

逸少

关注

逸少 2017.12.01 加入

一名不甘做咸鱼的码农; 人生没有太晚的开始,只有敢不敢全力前行;

评论

发布
暂无评论
(VMware)ubuntu 环境下搭建docker环境