hugo-teek is loading...

实战-ubuntu在线安装docker-成功测试-20220804

最后更新于:

实战:ubuntu上安装docker-2022.8.4(成功测试)

img

目录

[toc]

前言

  • 本人博客宗旨
11.本人提供的实战文档过程超详细,拿着我的博客,每个人最终都可以成功测试出实验现象,完全避免网上的一些埋着很多坑的博客(特别浪费时间,个人深有感触)22.博客排版美观、组织语言简明扼要;
33.提供实验涉及软件、源码;
44.实验过程如有遇到任何问题或者文章中有任何不足之处,欢迎留言或随时联系鄙人,微信:x2675263825,QQ:2675263825,在it学习的道路上,让我们一起共同成长!

实验环境

0、前期环境准备

系统版本主机名ip
ubuntu1804-dockerubuntu1804-docker172.29.9.9
1备注:
21台linux虚机,ubuntu系统版本:18.04.5 LTS (Bionic Beaver) #cat /etc/os-release
  • 配置ip:虚机网络为nat,保证可以ping通百度;
 1root@ubuntu1804-docker:~# vim /etc/netplan/01-netcfg.yaml
 2# This file describes the network interfaces available on your system
 3# For more information, see netplan(5).
 4network:
 5  version: 2
 6  renderer: networkd
 7  ethernets:
 8    eth0:
 9      dhcp4: no
10      dhcp6: no
11      addresses: [172.29.9.9/16]
12      gateway4: 172.29.0.254
13      nameservers:
14        addresses: [223.6.6.6]

img

可通百度:

img

  • 配置软件源为国内阿里云的
 1root@ubuntu1804-docker:~# vim /etc/apt/sources.list
 2deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
 3deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
 4
 5deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
 6deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
 7
 8deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
 9deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
10
11deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
12deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
13
14deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
15deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

img

  • 主机名配置
1root@ubuntu1804-template:~# hostnamectl --static set-hostname ubuntu1804-docker
2root@ubuntu1804-template:~# exec bash
3root@ubuntu1804-docker:~#
  • 注意:此时对做好初始化的虚机做一个快照!

img

1、卸载旧版本docker(如果存在的话)

1root@ubuntu1804-docker:~# apt-get remove docker docker-engine docker.io containerd runc

img

2、安装必要的一些系统工具

1root@ubuntu1804-docker:~# sudo apt-get update
2root@ubuntu1804-docker:~# sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common

3、安装GPG证书

1root@ubuntu1804-docker:~# curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

img

4、写入软件源信息

1root@ubuntu1804-docker:~# sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"

img

img

5、更新并安装Docker-CE

1root@ubuntu1804-docker:~# sudo apt-get update
2
3#开始安装docker-ce
4#这里需要注意:如果要装固定版本的docker,改如何装呢?(不指定版本的话,默认是安装的是最新版本)
5#注意:docker-ce是服务端,docker-ce-cli是客户端;

(1)默认是安装的是最新版本

1root@ubuntu1804-docker:~# sudo apt-get -y install docker-ce docker-ce-cli

(2)安装固定版本的docker

 1step1:查找Docker-CE的版本
 2root@ubuntu1804-docker:~# apt-cache madison docker-ce
 3amd64 Packages
 4 docker-ce | 5:19.03.0~3-0~ubuntu-bionic | https://mirrors.aliyun.com/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 5 docker-ce | 5:18.09.9~3-0~ubuntu-bionic | https://mirrors.aliyun.com/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 6 docker-ce | 5:18.09.8~3-0~ubuntu-bionic | https://mirrors.aliyun.com/docker-ce/linux/ubuntu bionic/stable 
 7……
 8root@ubuntu1804-docker:~# apt-cache madison docker-ce-cli
 9docker-ce-cli | 5:19.03.1~3-0~ubuntu-bionic | https://mirrors.aliyun.com/docker-ce/linux/ubuntu bionic/stable amd64 Packages
10docker-ce-cli | 5:19.03.0~3-0~ubuntu-bionic | https://mirrors.aliyun.com/docker-ce/linux/ubuntu bionic/stable amd64 Packages
11docker-ce-cli | 5:18.09.9~3-0~ubuntu-bionic | https://mirrors.aliyun.com/docker-ce/linux/ubuntu bionic/stable amd64 Packages
12docker-ce-cli | 5:18.09.8~3-0~ubuntu-bionic | https://mirrors.aliyun.com/docker-ce/linux/ubuntu bionic/stable
13……
14
15
16step2:安装指定版本的Docker-CE
17#本次安装如下版本的docker-ce:
18docker-ce | 5:19.03.9~3-0~ubuntu-bionic
19docker-ce-cli | 5:19.03.9~3-0~ubuntu-bionic
20root@ubuntu1804-docker:~# apt install docker-ce=5:19.03.9~3-0~ubuntu-bionic docker-ce-cli=5:19.03.9~3-0~ubuntu-bionic

6、安装验证

 1root@ubuntu1804-docker:~# docker version
 2Client: Docker Engine - Community
 3 Version:           19.03.9 #
 4 API version:       1.40
 5 Go version:        go1.13.10
 6 Git commit:        9d988398e7
 7 Built:             Fri May 15 00:25:18 2020
 8 OS/Arch:           linux/amd64
 9 Experimental:      false
10
11Server: Docker Engine - Community
12 Engine:
13  Version:          19.03.9 #
14  API version:      1.40 (minimum version 1.12)
15  Go version:       go1.13.10
16  Git commit:       9d988398e7
17  Built:            Fri May 15 00:23:50 2020
18  OS/Arch:          linux/amd64
19  Experimental:     false
20 containerd:
21  Version:          1.4.3
22  GitCommit:        269548fa27e0089a8b8278fc4fc781d7f65a939b
23 runc:
24  Version:          1.0.0-rc92
25  GitCommit:        ff819c7e9184c13b7c2607fe6c30ae19403a7aff
26 docker-init:
27  Version:          0.18.0
28  GitCommit:        fec3683
29root@ubuntu1804-docker:~#

7、开启docker,并设置开机自启

1root@ubuntu1804-docker:~# systemctl enable --now docker
2Synchronizing state of docker.service with SysV service script with /lib/systemd/systemd-sysv-install.
3Executing: /lib/systemd/systemd-sysv-install enable docker
4root@ubuntu1804-docker:~#

8、配置阿里云 docker 镜像加速器

 1sudo mkdir -p /etc/docker
 2sudo tee /etc/docker/daemon.json <<EOF
 3{
 4    "registry-mirrors": [
 5        "https://hub.uuuadc.top",
 6        "https://docker.anyhub.us.kg",
 7        "https://dockerhub.jobcher.com",
 8        "https://dockerhub.icu",
 9        "https://docker.ckyl.me",
10        "https://docker.awsl9527.cn"
11    ]
12}
13EOF
14sudo systemctl daemon-reload
15sudo systemctl restart docker

9、实例:拉取一个nginx镜像运行起来并观看效果

hub.docker.com网站,输入nginx,点击Tag,寻找相应版本的nginx镜像,找到后点击复制,直接粘贴在linux下即可拉取nginx镜像:

https://hub.docker.com/ 本次安装nginx:1.16.1版本:

img

img

docker pull nginx:1.16.1

img

 1root@ubuntu1804-docker:~# docker pull nginx:1.16.1 #这个是从官方下载镜像的(下载速度还是可以接受的,但有时候就很慢。。。。),后期可以采取方法加速从官方dockerhub下载慢的问题的。 
 21.16.1: Pulling from library/nginx
 354fec2fa59d0: Pull complete 
 45546cfc92772: Pull complete 
 550f62e3cdaf7: Pull complete 
 6Digest: sha256:d20aa6d1cae56fd17cd458f4807e0de462caf2336f0b70b5eeb69fcaaf30dd9c
 7Status: Downloaded newer image for nginx:1.16.1
 8docker.io/library/nginx:1.16.1
 9root@ubuntu1804-docker:~# 
10
11root@ubuntu1804-docker:~# docker images #查看下载好的nginx镜像
12REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
13nginx               1.16.1              dfcfd8e9a5d3        10 months ago       127MB
14
15root@ubuntu1804-docker:~#docker run -d -p 80:80 nginx:1.16.1 #-d代表后台,宿主机端口:容器端口 先在本地找,本地不存在的话,就去官网下载(docker.hub.com) 
165d5e1cb592166d07155813b237b49d832ee8f13c1d30c060d826da91205d704c
17root@ubuntu1804-docker:~# 
18
19root@ubuntu1804-docker:~# docker ps #查看正在运行的容器
20CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
215d5e1cb59216        nginx:1.16.1        "nginx -g 'daemon of…"   27 seconds ago      Up 25 seconds       0.0.0.0:80->80/tcp   elastic_bardeen
22root@ubuntu1804-docker:~#

此时在宿主机上访问这个地址,查看效果:

img

此时,如何知道这个是我们的容器呢? 可以通过进入刚才创建的容器,命令修改nginx index.html文件,来验证:

 1root@ubuntu1804-docker:~# docker ps
 2CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
 35d5e1cb59216        nginx:1.16.1        "nginx -g 'daemon of…"   3 minutes ago       Up 3 minutes        0.0.0.0:80->80/tcp   elastic_bardeen
 4root@ubuntu1804-docker:~# docker exec -it 5d5e1cb59216 bash #-i代表标准输入,t代表tty
 5root@5d5e1cb59216:/# 
 6root@5d5e1cb59216:/# pwd
 7/
 8root@5d5e1cb59216:/# cat /etc/issue #查看容器系统是debaian的
 9Debian GNU/Linux 10 \n \l
10
11root@5d5e1cb59216:/# apt update #更新一下软件源 (这个有点费时间,且后面这个操作不需要更新软件源的,此时直接ctrl c杀死刚才那个进程就好)
12root@5d5e1cb59216:/# pwd
13/
14root@5d5e1cb59216:/# cd /usr/share/nginx/html/
15root@5d5e1cb59216:/usr/share/nginx/html# ls -l
16total 8
17-rw-r--r-- 1 root root 494 Aug 13  2019 50x.html
18-rw-r--r-- 1 root root 612 Aug 13  2019 index.html
19root@5d5e1cb59216:/usr/share/nginx/html# cat index.html 
20<!DOCTYPE html>
21<html>
22<head>
23<title>Welcome to nginx!</title>
24<style>
25    body {
26        width: 35em;
27        margin: 0 auto;
28        font-family: Tahoma, Verdana, Arial, sans-serif;
29    }
30</style>
31</head>
32<body>
33<h1>Welcome to nginx!</h1>
34<p>If you see this page, the nginx web server is successfully installed and
35working. Further configuration is required.</p>
36
37<p>For online documentation and support please refer to
38<a href="http://nginx.org/">nginx.org</a>.<br/>
39Commercial support is available at
40<a href="http://nginx.com/">nginx.com</a>.</p>
41
42<p><em>Thank you for using nginx.</em></p>
43</body>
44</html>
45root@5d5e1cb59216:/usr/share/nginx/html# 
46root@5d5e1cb59216:/usr/share/nginx/html# echo "Linux39" > index.html #修改nginx index.html文件

此时再次在宿主机上刷新网页观看效果(符合预期):

img

  • 此时,再对安装好docker环境的虚机做一个快照!

img

实验到此结束!

总结

以上就是关于如何在ubuntu1804上安装docker的方法了,感谢阅读。

img

推荐使用微信支付
微信支付二维码
推荐使用支付宝
支付宝二维码
最新文章

文档导航