(云原生之使用Docker部署ubuntu测试环境

一、检查系统版本

[root@node ~]# cat /etc/os-release 
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

二、检查docker状态

[root@node ~]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2022-11-04 09:12:32 CST; 1 day 1h ago
     Docs: https://docs.docker.com
 Main PID: 10162 (dockerd)
    Tasks: 62
   Memory: 3.2G
   CGroup: /system.slice/docker.service
           ├─10162 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
           ├─21803 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 27217 -container-ip 172.17.0.2 -container-port 27017
           ├─21808 /usr/bin/docker-proxy -proto tcp -host-ip :: -host-port 27217 -container-ip 172.17.0.2 -container-port 27017
           ├─33495 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 3548 -container-ip 172.17.0.3 -container-port 3306
           ├─33501 /usr/bin/docker-proxy -proto tcp -host-ip :: -host-port 3548 -container-ip 172.17.0.3 -container-port 3306
           ├─43634 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 8080 -container-ip 172.17.0.4 -container-port 80
           ├─43640 /usr/bin/docker-proxy -proto tcp -host-ip :: -host-port 8080 -container-ip 172.17.0.4 -container-port 80
           ├─59700 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 8098 -container-ip 172.17.0.5 -container-port 80
           └─59705 /usr/bin/docker-proxy -proto tcp -host-ip :: -host-port 8098 -container-ip 172.17.0.5 -container-port 80

Nov 05 03:44:37 node dockerd[10162]: time="2022-11-05T03:44:37.995861090+08:00" level=info msg="ignoring event" container=97a5b36bf69698...kDelete"
Nov 05 03:44:51 node dockerd[10162]: time="2022-11-05T03:44:51.129058317+08:00" level=info msg="ignoring event" container=97a5b36bf69698...kDelete"
Nov 05 03:45:17 node dockerd[10162]: time="2022-11-05T03:45:17.340127808+08:00" level=info msg="ignoring event" container=97a5b36bf69698...kDelete"
Nov 05 03:46:08 node dockerd[10162]: time="2022-11-05T03:46:08.908345312+08:00" level=info msg="ignoring event" container=97a5b36bf69698...kDelete"
Nov 05 03:47:09 node dockerd[10162]: time="2022-11-05T03:47:09.266745922+08:00" level=info msg="ignoring event" container=97a5b36bf69698...kDelete"
Nov 05 03:48:09 node dockerd[10162]: time="2022-11-05T03:48:09.607608731+08:00" level=info msg="ignoring event" container=97a5b36bf69698...kDelete"
Nov 05 03:49:10 node dockerd[10162]: time="2022-11-05T03:49:10.011818478+08:00" level=info msg="ignoring event" container=97a5b36bf69698...kDelete"
Nov 05 03:50:10 node dockerd[10162]: time="2022-11-05T03:50:10.319825711+08:00" level=info msg="ignoring event" container=97a5b36bf69698...kDelete"
Nov 05 03:51:29 node dockerd[10162]: time="2022-11-05T03:51:29.588349358+08:00" level=error msg="Not continuing with pull after error: m...unknown"
Nov 05 03:51:29 node dockerd[10162]: time="2022-11-05T03:51:29.588490207+08:00" level=error msg="Handler for POST /v1.41/images/create r...unknown"
Hint: Some lines were ellipsized, use -l to show in full.

三、下载ubuntu镜像

[root@node ~]# docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
7b1a6ab2e44d: Already exists 
Digest: sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest

四、运行ubuntu容器

[root@node ~]# docker run -it --name ubuntu_test ubuntu:latest /bin/bash
root@834be480431a:/# ls
bin  boot  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@834be480431a:/# pwd
/

五、查看ubuntu容器内环境

1.检查系统版本

root@834be480431a:/# cat /etc/os-release 
NAME="Ubuntu"
VERSION="20.04.3 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.3 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

2.更新仓库源

root@834be480431a:/# apt-get update
Get:1 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:2 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB]
Get:3 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [2267 kB]
Get:4 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:5 http://archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
Get:6 http://archive.ubuntu.com/ubuntu focal/main amd64 Packages [1275 kB]
Get:7 http://security.ubuntu.com/ubuntu focal-security/multiverse amd64 Packages [27.5 kB]
Get:8 http://security.ubuntu.com/ubuntu focal-security/universe amd64 Packages [926 kB]
Get:9 http://security.ubuntu.com/ubuntu focal-security/restricted amd64 Packages [1655 kB]
Get:10 http://archive.ubuntu.com/ubuntu focal/universe amd64 Packages [11.3 MB]            
Get:11 http://archive.ubuntu.com/ubuntu focal/multiverse amd64 Packages [177 kB]          
Get:12 http://archive.ubuntu.com/ubuntu focal/restricted amd64 Packages [33.4 kB]
Get:13 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [2737 kB]
Get:14 http://archive.ubuntu.com/ubuntu focal-updates/multiverse amd64 Packages [37.7 kB]
Get:15 http://archive.ubuntu.com/ubuntu focal-updates/restricted amd64 Packages [1772 kB]                                                         
Get:16 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [1224 kB]                                                           
Get:17 http://archive.ubuntu.com/ubuntu focal-backports/universe amd64 Packages [27.5 kB]                                                         
Get:18 http://archive.ubuntu.com/ubuntu focal-backports/main amd64 Packages [55.2 kB]                                                             
Fetched 24.2 MB in 7s (3553 kB/s)                                                                                                                 
Reading package lists... Done

3.搜索软件包

root@834be480431a:/# apt search net-tool
Sorting... Done
Full Text Search... Done
atm-tools/focal 1:2.5.1-4 amd64
  Base programs for ATM in Linux, the net-tools for ATM

ddnet-tools/focal 12.9.2-1ubuntu1 amd64
  Tools for DDNet

net-tools/focal,now 1.60+git20180626.aebd88e-1ubuntu1 amd64 [installed]
  NET-3 networking toolkit


4.安装软件

root@834be480431a:/# apt install net-tools -y
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  net-tools
0 upgraded, 1 newly installed, 0 to remove and 36 not upgraded.
Need to get 196 kB of archives.
After this operation, 864 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu focal/main amd64 net-tools amd64 1.60+git20180626.aebd88e-1ubuntu1 [196 kB]
Fetched 196 kB in 3s (71.0 kB/s)    
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package net-tools.
(Reading database ... 7452 files and directories currently installed.)
Preparing to unpack .../net-tools_1.60+git20180626.aebd88e-1ubuntu1_amd64.deb ...
Unpacking net-tools (1.60+git20180626.aebd88e-1ubuntu1) ...
Setting up net-tools (1.60+git20180626.aebd88e-1ubuntu1) ...

5.查看系统ip

root@834be480431a:/# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.7  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:07  txqueuelen 0  (Ethernet)
        RX packets 11729  bytes 59032437 (59.0 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 9576  bytes 648839 (648.8 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

六.安装nginx

1.重新打开一个测试容器

[root@node ~]# docker run -it --name ubuntu_test ubuntu:latest /bin/bash
root@e65222c1a0c7:/# apt update
Get:1 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB]
Get:2 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:3 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:4 http://archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
Get:5 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [2267 kB]
Get:6 http://archive.ubuntu.com/ubuntu focal/main amd64 Packages [1275 kB]
Get:7 http://archive.ubuntu.com/ubuntu focal/multiverse amd64 Packages [177 kB]
Get:8 http://archive.ubuntu.com/ubuntu focal/universe amd64 Packages [11.3 MB]            
Get:9 http://security.ubuntu.com/ubuntu focal-security/multiverse amd64 Packages [27.5 kB]
Get:10 http://security.ubuntu.com/ubuntu focal-security/universe amd64 Packages [926 kB]
Get:11 http://security.ubuntu.com/ubuntu focal-security/restricted amd64 Packages [1655 kB]
Get:12 http://archive.ubuntu.com/ubuntu focal/restricted amd64 Packages [33.4 kB]           
Get:13 http://archive.ubuntu.com/ubuntu focal-updates/restricted amd64 Packages [1772 kB] 
Get:14 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [2737 kB]        
Get:15 http://archive.ubuntu.com/ubuntu focal-updates/multiverse amd64 Packages [37.7 kB]
Get:16 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [1224 kB]
Get:17 http://archive.ubuntu.com/ubuntu focal-backports/universe amd64 Packages [27.5 kB]
Get:18 http://archive.ubuntu.com/ubuntu focal-backports/main amd64 Packages [55.2 kB]
Fetched 24.2 MB in 6s (4059 kB/s)                           
Reading package lists... Done
Building dependency tree       
Reading state information... Done
37 packages can be upgraded. Run 'apt list --upgradable' to see them.
root@e65222c1a0c7:/# apt install systemctl

2.安装nginx

 apt install -y  nginx

3.启动服务

root@e65222c1a0c7:/# systemctl enable --now nginx
root@e65222c1a0c7:/# 

4.查看nginx状态

root@e65222c1a0c7:/# systemctl status nginx
nginx.service - A high performance web server and a reverse proxy server
    Loaded: loaded (/usr/lib/systemd/system/nginx.service, enabled)
    Active: active (running)

5.测试网页内容

root@e65222c1a0c7:~# echo hello > /var/www/html/index.html
root@e65222c1a0c7:~# curl 127.0.0.1
hello