Ubuntu 18.04 安装Docker实战案例

简介: 关于如何在Ubuntu 18.04系统上安装Docker的实战案例,包括安装步骤、配置镜像加速以及下载和运行Docker镜像的过程。

作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.通过ubantu安装docker

1>.查看操作平台

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

root@docker101:~# apt-get update
Hit:1 http://mirrors.aliyun.com/ubuntu bionic InRelease
Hit:2 http://mirrors.aliyun.com/ubuntu bionic-security InRelease
Hit:3 http://mirrors.aliyun.com/ubuntu bionic-updates InRelease                  
Hit:4 http://mirrors.aliyun.com/ubuntu bionic-proposed InRelease                 
Hit:5 http://mirrors.aliyun.com/ubuntu bionic-backports InRelease               
Reading package lists... Done                                                   
root@docker101:~#

root@docker101:~# apt-get update

root@docker101:~# apt-get -y install apt-transport-https ca-certificates curl software-properties-common
Reading package lists... Done
Building dependency tree       
Reading state information... Done
ca-certificates is already the newest version (20180409).
ca-certificates set to manually installed.
curl is already the newest version (7.58.0-2ubuntu3.8).
curl set to manually installed.
The following additional packages will be installed:
  python3-software-properties
The following NEW packages will be installed:
  apt-transport-https
The following packages will be upgraded:
  python3-software-properties software-properties-common
2 upgraded, 1 newly installed, 0 to remove and 71 not upgraded.
Need to get 35.3 kB of archives.
After this operation, 153 kB of additional disk space will be used.
Get:1 http://mirrors.aliyun.com/ubuntu bionic-updates/universe amd64 apt-transport-https all 1.6.12 [1,692 B]
Get:2 http://mirrors.aliyun.com/ubuntu bionic-updates/main amd64 software-properties-common all 0.96.24.32.12 [10.0 kB]
Get:3 http://mirrors.aliyun.com/ubuntu bionic-updates/main amd64 python3-software-properties all 0.96.24.32.12 [23.6 kB]
Fetched 35.3 kB in 0s (126 kB/s)                  
Selecting previously unselected package apt-transport-https.
(Reading database ... 66944 files and directories currently installed.)
Preparing to unpack .../apt-transport-https_1.6.12_all.deb ...
Unpacking apt-transport-https (1.6.12) ...
Preparing to unpack .../software-properties-common_0.96.24.32.12_all.deb ...
Unpacking software-properties-common (0.96.24.32.12) over (0.96.24.32.9) ...
Preparing to unpack .../python3-software-properties_0.96.24.32.12_all.deb ...
Unpacking python3-software-properties (0.96.24.32.12) over (0.96.24.32.9) ...
Setting up apt-transport-https (1.6.12) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Setting up python3-software-properties (0.96.24.32.12) ...
Processing triggers for dbus (1.12.2-1ubuntu1.1) ...
Setting up software-properties-common (0.96.24.32.12) ...
root@docker101:~#

root@docker101:~# apt-get -y install apt-transport-https ca-certificates curl software-properties-common

3>.安装GPG证书

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

4>.写入软件源信息

root@docker101:~# add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
Hit:1 http://mirrors.aliyun.com/ubuntu bionic InRelease
Hit:2 http://mirrors.aliyun.com/ubuntu bionic-security InRelease                 
Hit:3 http://mirrors.aliyun.com/ubuntu bionic-updates InRelease                                        
Get:4 https://mirrors.aliyun.com/docker-ce/linux/ubuntu bionic InRelease [64.4 kB]                                                    
Hit:5 http://mirrors.aliyun.com/ubuntu bionic-proposed InRelease                                                                      
Hit:6 http://mirrors.aliyun.com/ubuntu bionic-backports InRelease                                                   
Get:7 https://mirrors.aliyun.com/docker-ce/linux/ubuntu bionic/stable amd64 Packages [9,852 B]
Fetched 74.3 kB in 1s (103 kB/s)      
Reading package lists... Done
root@docker101:~#

5>.更新并安装Docker-CE

root@docker101:~# apt-get -y update
Hit:1 http://mirrors.aliyun.com/ubuntu bionic InRelease
Hit:2 http://mirrors.aliyun.com/ubuntu bionic-security InRelease                                                                   
Hit:3 https://mirrors.aliyun.com/docker-ce/linux/ubuntu bionic InRelease                                                           
Hit:4 http://mirrors.aliyun.com/ubuntu bionic-updates InRelease                                                                    
Hit:5 http://mirrors.aliyun.com/ubuntu bionic-proposed InRelease                
Hit:6 http://mirrors.aliyun.com/ubuntu bionic-backports InRelease               
Reading package lists... Done                                                   
root@docker101:~#

root@docker101:~# apt-get -y update

root@docker101:~# apt-get -y install docker-ce
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  aufs-tools cgroupfs-mount containerd.io docker-ce-cli libltdl7 pigz
The following NEW packages will be installed:
  aufs-tools cgroupfs-mount containerd.io docker-ce docker-ce-cli libltdl7 pigz
0 upgraded, 7 newly installed, 0 to remove and 71 not upgraded.
Need to get 85.5 MB of archives.
After this operation, 384 MB of additional disk space will be used.
Get:1 http://mirrors.aliyun.com/ubuntu bionic/universe amd64 pigz amd64 2.4-1 [57.4 kB]
Get:2 https://mirrors.aliyun.com/docker-ce/linux/ubuntu bionic/stable amd64 containerd.io amd64 1.2.10-3 [20.0 MB]
Get:3 http://mirrors.aliyun.com/ubuntu bionic/universe amd64 aufs-tools amd64 1:4.9+20170918-1ubuntu1 [104 kB]
Get:4 http://mirrors.aliyun.com/ubuntu bionic/universe amd64 cgroupfs-mount all 1.4 [6,320 B]
Get:5 http://mirrors.aliyun.com/ubuntu bionic/main amd64 libltdl7 amd64 2.4.6-2 [38.8 kB]
Get:6 https://mirrors.aliyun.com/docker-ce/linux/ubuntu bionic/stable amd64 docker-ce-cli amd64 5:19.03.5~3-0~ubuntu-bionic [42.5 MB]
Get:7 https://mirrors.aliyun.com/docker-ce/linux/ubuntu bionic/stable amd64 docker-ce amd64 5:19.03.5~3-0~ubuntu-bionic [22.8 MB]
Fetched 85.5 MB in 8s (11.0 MB/s)                                                                                                                                                                                                                                            
Selecting previously unselected package pigz.
(Reading database ... 66948 files and directories currently installed.)
Preparing to unpack .../0-pigz_2.4-1_amd64.deb ...
Unpacking pigz (2.4-1) ...
Selecting previously unselected package aufs-tools.
Preparing to unpack .../1-aufs-tools_1%3a4.9+20170918-1ubuntu1_amd64.deb ...
Unpacking aufs-tools (1:4.9+20170918-1ubuntu1) ...
Selecting previously unselected package cgroupfs-mount.
Preparing to unpack .../2-cgroupfs-mount_1.4_all.deb ...
Unpacking cgroupfs-mount (1.4) ...
Selecting previously unselected package containerd.io.
Preparing to unpack .../3-containerd.io_1.2.10-3_amd64.deb ...
Unpacking containerd.io (1.2.10-3) ...
Selecting previously unselected package docker-ce-cli.
Preparing to unpack .../4-docker-ce-cli_5%3a19.03.5~3-0~ubuntu-bionic_amd64.deb ...
Unpacking docker-ce-cli (5:19.03.5~3-0~ubuntu-bionic) ...
Selecting previously unselected package docker-ce.
Preparing to unpack .../5-docker-ce_5%3a19.03.5~3-0~ubuntu-bionic_amd64.deb ...
Unpacking docker-ce (5:19.03.5~3-0~ubuntu-bionic) ...
Selecting previously unselected package libltdl7:amd64.
Preparing to unpack .../6-libltdl7_2.4.6-2_amd64.deb ...
Unpacking libltdl7:amd64 (2.4.6-2) ...
Setting up aufs-tools (1:4.9+20170918-1ubuntu1) ...
Setting up containerd.io (1.2.10-3) ...
Created symlink /etc/systemd/system/multi-user.target.wants/containerd.service → /lib/systemd/system/containerd.service.
Processing triggers for ureadahead (0.100.0-21) ...
Setting up cgroupfs-mount (1.4) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
Processing triggers for systemd (237-3ubuntu10.29) ...
Setting up libltdl7:amd64 (2.4.6-2) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Setting up docker-ce-cli (5:19.03.5~3-0~ubuntu-bionic) ...
Setting up pigz (2.4-1) ...
Setting up docker-ce (5:19.03.5~3-0~ubuntu-bionic) ...
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /lib/systemd/system/docker.service.
Created symlink /etc/systemd/system/sockets.target.wants/docker.socket → /lib/systemd/system/docker.socket.
Processing triggers for ureadahead (0.100.0-21) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
Processing triggers for systemd (237-3ubuntu10.29) ...
root@docker101:~# 
root@docker101:~#

root@docker101:~# apt-get -y install docker-ce

root@docker101:~# systemctl status docker                #安装成功后默认docker是启动的
● docker.service - Docker Application Container Engine
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2020-01-12 08:03:46 UTC; 2min 2s ago
     Docs: https://docs.docker.com
 Main PID: 5634 (dockerd)
    Tasks: 10
   CGroup: /system.slice/docker.service
           └─5634 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Jan 12 08:03:46 docker101.yinzhengjie.org.cn dockerd[5634]: time="2020-01-12T08:03:46.061865687Z" level=warning msg="Your kernel does not support swap memory limit"
Jan 12 08:03:46 docker101.yinzhengjie.org.cn dockerd[5634]: time="2020-01-12T08:03:46.061970486Z" level=warning msg="Your kernel does not support cgroup rt period"
Jan 12 08:03:46 docker101.yinzhengjie.org.cn dockerd[5634]: time="2020-01-12T08:03:46.062000817Z" level=warning msg="Your kernel does not support cgroup rt runtime"
Jan 12 08:03:46 docker101.yinzhengjie.org.cn dockerd[5634]: time="2020-01-12T08:03:46.062480825Z" level=info msg="Loading containers: start."
Jan 12 08:03:46 docker101.yinzhengjie.org.cn dockerd[5634]: time="2020-01-12T08:03:46.340909890Z" level=info msg="Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Daemon option --bip can be used to set a preferred IP address"
Jan 12 08:03:46 docker101.yinzhengjie.org.cn dockerd[5634]: time="2020-01-12T08:03:46.578008607Z" level=info msg="Loading containers: done."
Jan 12 08:03:46 docker101.yinzhengjie.org.cn dockerd[5634]: time="2020-01-12T08:03:46.670981992Z" level=info msg="Docker daemon" commit=633a0ea838 graphdriver(s)=overlay2 version=19.03.5
Jan 12 08:03:46 docker101.yinzhengjie.org.cn dockerd[5634]: time="2020-01-12T08:03:46.671108044Z" level=info msg="Daemon has completed initialization"
Jan 12 08:03:46 docker101.yinzhengjie.org.cn systemd[1]: Started Docker Application Container Engine.
Jan 12 08:03:46 docker101.yinzhengjie.org.cn dockerd[5634]: time="2020-01-12T08:03:46.756901996Z" level=info msg="API listen on /var/run/docker.sock"
root@docker101:~# 
root@docker101:~# systemctl enable docker            #咱们可以将docker设置为开启自启动
Synchronizing state of docker.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable docker
root@docker101:~#

6>.查看docker的版本

root@docker101:~# docker version 
Client: Docker Engine - Community
 Version:           19.03.5
 API version:       1.40
 Go version:        go1.12.12
 Git commit:        633a0ea838
 Built:             Wed Nov 13 07:29:52 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.5
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.12
  Git commit:       633a0ea838
  Built:            Wed Nov 13 07:28:22 2019
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.10
  GitCommit:        b34a5c8af56e510852c35414db4c1f4fa6172339
 runc:
  Version:          1.0.0-rc8+dev
  GitCommit:        3e425f80a8c931f88e6d94a8c831b9d5aa481657
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683
root@docker101:~#

二.docker镜像加速配置

1>.获取加速器(https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors)

2>.生成配置文件并重启docker服务

root@docker101:~# sudo mkdir -p /etc/docker
root@docker101:~# sudo tee /etc/docker/daemon.json <<-'EOF'
> {
>   "registry-mirrors": ["https://tuv7rqqq.mirror.aliyuncs.com"]
> }
> EOF
{
  "registry-mirrors": ["https://tuv7rqqq.mirror.aliyuncs.com"]
}
root@docker101:~# sudo systemctl daemon-reload
root@docker101:~# sudo systemctl restart docker
root@docker101:~#

三.下载docker镜像

1>.下载一个centos镜像

root@docker101:~# docker image ls          #查看当前的docker镜像为空
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
root@docker101:~# 
root@docker101:~# docker pull centos        #从官方下载一个docker镜像,本次我们下载的是官方提供的centos镜像。
Using default tag: latest
latest: Pulling from library/centos
729ec3a6ada3: Pull complete 
Digest: sha256:f94c1d992c193b3dc09e297ffd54d8a4f1dc946c37cbeceb26d35ce1647f88d9
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
root@docker101:~# 
root@docker101:~# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              0f3e07c0138f        3 months ago        220MB
root@docker101:~# 
root@docker101:~#

2>.基于刚刚下载的docker镜像启动一个容器

root@docker101:~# docker ps        #查看当前容器情况
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
root@docker101:~# 
root@docker101:~# docker run -d -it centos      #在后端基于centos镜像运行一个可交互的容器
968e7ecc39f277a3d3f98b658f8f496de622edccfa4ef45d8ec64c46f5012d4c
root@docker101:~# 
root@docker101:~# docker ps        #再次查看当前运行的容器
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
968e7ecc39f2        centos              "/bin/bash"         3 seconds ago       Up 2 seconds                            keen_meitner
root@docker101:~# 
root@docker101:~#

3>.查看已经创建的容器所在的默认路径

root@docker101:~# ll /var/lib/containerd/io.containerd.runtime.v1.linux/moby/        #注意,这个“moby”就是docker项目名称。
total 12
drwx--x--x 3 root root 4096 Jan 12 08:24 ./
drwx--x--x 3 root root 4096 Jan 12 08:24 ../
drwx--x--x 2 root root 4096 Jan 12 08:24 968e7ecc39f277a3d3f98b658f8f496de622edccfa4ef45d8ec64c46f5012d4c/          #这个数字就是上面我们创建的容器编号。
root@docker101:~# 
root@docker101:~# 
root@docker101:~# ll /var/lib/containerd/io.containerd.runtime.v1.linux/moby/968e7ecc39f277a3d3f98b658f8f496de622edccfa4ef45d8ec64c46f5012d4c/
total 8
drwx--x--x 2 root root 4096 Jan 12 08:24 ./
drwx--x--x 3 root root 4096 Jan 12 08:24 ../
prwx------ 1 root root    0 Jan 12 08:24 shim.stderr.log|
prwx------ 1 root root    0 Jan 12 08:24 shim.stdout.log|
root@docker101:~# 
root@docker101:~# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
968e7ecc39f2        centos              "/bin/bash"         About a minute ago   Up About a minute                       keen_meitner
root@docker101:~# 
[root@968e7ecc39f2 /]# ls -l
total 48
lrwxrwxrwx   1 root root    7 May 11  2019 bin -> usr/bin
drwxr-xr-x   5 root root  360 Jan 12 08:24 dev
drwxr-xr-x   1 root root 4096 Jan 12 08:24 etc
drwxr-xr-x   2 root root 4096 May 11  2019 home
lrwxrwxrwx   1 root root    7 May 11  2019 lib -> usr/lib
lrwxrwxrwx   1 root root    9 May 11  2019 lib64 -> usr/lib64
drwx------   2 root root 4096 Sep 27 17:13 lost+found
drwxr-xr-x   2 root root 4096 May 11  2019 media
drwxr-xr-x   2 root root 4096 May 11  2019 mnt
drwxr-xr-x   2 root root 4096 May 11  2019 opt
dr-xr-xr-x 184 root root    0 Jan 12 08:24 proc
dr-xr-x---   2 root root 4096 Sep 27 17:13 root
drwxr-xr-x  11 root root 4096 Sep 27 17:13 run
lrwxrwxrwx   1 root root    8 May 11  2019 sbin -> usr/sbin
drwxr-xr-x   2 root root 4096 May 11  2019 srv
dr-xr-xr-x  13 root root    0 Jan 12 08:24 sys
drwxrwxrwt   7 root root 4096 Sep 27 17:13 tmp
drwxr-xr-x  12 root root 4096 Sep 27 17:13 usr
drwxr-xr-x  20 root root 4096 Sep 27 17:13 var
[root@968e7ecc39f2 /]# 
[root@968e7ecc39f2 /]#
目录
相关文章
|
19天前
|
Ubuntu 开发工具 git
Ubuntu安装homebrew的完整教程
本文介绍了如何在没有公网的情况下安装 Homebrew。首先访问 Homebrew 官网,然后通过阿里云的镜像克隆安装脚本,并创建普通用户进行安装。接着修改 `install.sh` 文件指向国内镜像,执行安装命令。最后配置环境变量并更换 Homebrew 源为国内镜像,确保安装顺利。
143 50
|
29天前
|
Prometheus 监控 Cloud Native
基于Docker安装Grafana和Prometheus
Grafana 是一款用 Go 语言开发的开源数据可视化工具,支持数据监控和统计,并具备告警功能。通过 Docker 部署 Grafana 和 Prometheus,可实现系统数据的采集、展示和告警。默认登录用户名和密码均为 admin。配置 Prometheus 数据源后,可导入主机监控模板(ID 8919)进行数据展示。
70 2
|
1月前
|
消息中间件 Linux RocketMQ
在Red Hat Enterprise Linux 9上使用Docker快速安装并部署
通过以上步骤,你可以在Red Hat Enterprise Linux 9上使用Docker快速安装并部署RocketMQ。这种方法不仅简化了安装过程,还提供了一个灵活的环境来管理和扩展消息队列系统。RocketMQ作为一款高性能的分布式消息系统,通过Docker可以实现快速部署和高效管理。
58 2
|
19天前
|
Docker 容器
【赵渝强老师】使用二进制包方式安装Docker
本文介绍了在企业生产环境中无法直接访问外网时,如何使用Docker官方提供的二进制包进行Docker的离线安装。文章详细列出了从安装wget、下载Docker安装包、解压、复制命令到启动Docker服务的具体步骤,并提供了相关命令和示例图片。最后,还介绍了如何设置Docker为开机自启模式。
|
19天前
|
缓存 Ubuntu Linux
如何安装Docker
如何安装Docker
100 0
|
28天前
|
消息中间件 Ubuntu Java
Ubuntu系统上安装Apache Kafka
Ubuntu系统上安装Apache Kafka
|
存储 Ubuntu Linux
【云原生 | 02】分别在CentOS、Ubuntu、macOS、win7、win8、win10等不同操作系统下安装Docker详细教程
Docker引擎是使用Docker容器的核心组件,可以在主流的操作系统和云平台上使用, 包括 Linux 操作系统(如 Ubuntu、Debian、CentOS、Redhat 等),macOS 和 Windows 操作系统, 以及IBM、亚马逊、微软等知名云平台.........
444 0
【云原生 | 02】分别在CentOS、Ubuntu、macOS、win7、win8、win10等不同操作系统下安装Docker详细教程
|
7月前
|
Ubuntu Docker 索引
ubuntu 20.04 安装docker教程和安装中遇到的问题解决方案(超详细 附加图文教程)
ubuntu 20.04 安装docker教程和安装中遇到的问题解决方案(超详细 附加图文教程)
1130 0
|
Linux 开发工具 Docker
Ubuntu18安装docker详细教程
Ubuntu18安装docker详细教程
576 0
Ubuntu18安装docker详细教程