本文主要讲述如何在 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
软件包准备
环境设置
由于笔者电脑之前安装了Docker Desktop环境,开启了 Hyper-V 功能,因此在 VMware 中安装 Ubuntu 虚拟机时报如下的错误:
VMware Workstation 与 Device/Credential Guard 不兼容.在禁用 Device/Credenti
解决此错误可以按照此文中的方案进行设置,方可解决~
VMware workstation 安装
VMware workstation 的安装过程比较简单,网上的教程也比较多,笔者这边只讲重要步骤作详细阐述!
Ubuntu 虚拟机安装
开启虚拟机后虚拟机则开始进行安装设置过程,安装过程基本上可以按照默认设置安装,其中有两个比较重要的设置如下:
yishao@yishao:~$ uname -a
Linux yishao 5.4.0-73-generic #82-Ubuntu SMP Wed Apr 14 17:39:42 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
yishao@yishao:~$ cat /proc/version
Linux 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 虚拟机设置
时区设置
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) Europe
2) Americas 8) Indian Ocean
3) Antarctica 9) Pacific Ocean
4) 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
#? 4
Please select a country whose clocks agree with yours.
1) Afghanistan 10) Cyprus 19) Japan 28) Macau 37) Qatar 46) Turkmenistan
2) Armenia 11) East Timor 20) Jordan 29) Malaysia 38) Russia 47) United Arab Emirates
3) Azerbaijan 12) Georgia 21) Kazakhstan 30) Mongolia 39) Saudi Arabia 48) Uzbekistan
4) Bahrain 13) Hong Kong 22) Korea (North) 31) Myanmar (Burma) 40) Singapore 49) Vietnam
5) Bangladesh 14) India 23) Korea (South) 32) Nepal 41) Sri Lanka 50) Yemen
6) Bhutan 15) Indonesia 24) Kuwait 33) Oman 42) Syria
7) Brunei 16) Iran 25) Kyrgyzstan 34) Pakistan 43) Taiwan
8) Cambodia 17) Iraq 26) Laos 35) Palestine 44) Tajikistan
9) China 18) Israel 27) Lebanon 36) Philippines 45) Thailand
#? 9
Please select one of the following timezones.
1) Beijing Time
2) 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) Yes
2) No
#? 1
You can make this change permanent for yourself by appending the line
TZ='Asia/Shanghai'; export TZ
to 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 you
can 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 设置
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
复制代码
yishao@yishao:~$ sudo hostnamectl set-hostname node1
yishao@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
复制代码
yishao@yishao:~$ cat /etc/hosts
127.0.0.1 localhost
127.0.1.1 yishao #此处yishao需要修改为node1
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::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 localhost
127.0.1.1 node1 # 新的映射
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::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 apply
yishao@yishao:~$ sudo reboot
复制代码
Docker 安装
yishao@node1:~$ sudo apt-get remove docker docker-engine docker.io containerd runc
复制代码
yishao@node1:~$ sudo apt-get update
yishao@node1:~$ sudo apt-get install \
> apt-transport-https \
> ca-certificates \
> curl \
> gnupg \
> lsb-release
复制代码
yishao@node1:~$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
复制代码
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
复制代码
yishao@node1:~$ sudo apt-get update
yishao@node1:~$ sudo apt-get 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/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://9w2ypf5d.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
复制代码
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 环境设置
yishao@node1:~$ sudo groupadd docker
yishao@node1:~$ sudo usermod -aG docker yishao
复制代码
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
复制代码
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
复制代码
yishao@node1:~$ docker-compose --version
docker-compose version 1.29.2, build 5becea4c
复制代码
看到如上输出,表示 docker compose 环境已经安装成功~
克隆虚拟机
经过以上步骤完整的 Ubuntu 虚拟机+docker 环境已经安装设置完成,为了以后能够方便使用,建议对此 VM 镜像克隆备份,克隆有几个需要注意的地方如下:
克隆时请选择 创建完整克隆
克隆完成后请重新生成 MAC 地址 (重要)
参考:
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/
评论