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 /]#
目录
相关文章
|
3天前
|
存储 Ubuntu
关于实体机安装Ubuntu 22.04.3-desktop-amd64遇见的一些问题
【10月更文挑战第2天】本文详细介绍了在使用 Ubuntu 过程中常见的五个问题及其解决方案:下载镜像文件速度慢或损坏,可更换镜像源或验证哈希值;制作启动盘失败,需检查 U 盘及设置;安装过程中的分区问题,需合理规划分区;安装后的驱动问题,可通过安装官方驱动解决;软件安装和更新问题,需检查仓库配置及依赖关系。
|
1月前
|
Ubuntu 数据安全/隐私保护
Ubuntu22.04LTS环境部署实战
这篇文章提供了Ubuntu 22.04 LTS操作系统的详细安装步骤,包括选择语言、键盘布局、网络配置、软件源设置、磁盘分区、安装OpenSSH服务以及完成安装和首次登录系统的过程。
81 6
Ubuntu22.04LTS环境部署实战
|
8天前
|
Docker 容器
Docker安装及镜像源修改
本文介绍了Docker的安装过程和如何修改Docker镜像源以加速下载。包括更新系统包、安装所需软件包、设置yum源、安装Docker以及验证安装是否成功。接着,提供了修改Docker镜像源的步骤,包括创建配置文件、编辑配置文件以设置镜像加速地址,并提供了几个常用的国内镜像源地址。最后,通过重启Docker服务和检查配置是否生效来完成镜像源的修改。
Docker安装及镜像源修改
|
9天前
|
Docker 容器
centos7.3之安装docker
centos7.3之安装docker
|
1月前
|
NoSQL 关系型数据库 Redis
mall在linux环境下的部署(基于Docker容器),Docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongo
mall在linux环境下的部署(基于Docker容器),docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongodb、minio详细教程,拉取镜像、运行容器
mall在linux环境下的部署(基于Docker容器),Docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongo
|
1月前
|
Linux Docker Windows
Docker配置https证书案例
本文介绍了如何为Docker的Harbor服务配置HTTPS证书,包括安装Docker和Harbor、修改配置文件以使用证书、生成自签名证书、配置证书以及验证配置的步骤。
35 2
Docker配置https证书案例
|
20天前
|
Prometheus 监控 Cloud Native
docker安装prometheus+Granfan并监控容器
【9月更文挑战第14天】本文介绍了在Docker中安装Prometheus与Grafana并监控容器的步骤,包括创建配置文件、运行Prometheus与Grafana容器,以及在Grafana中配置数据源和创建监控仪表盘,展示了如何通过Prometheus抓取数据并利用Grafana展示容器的CPU使用率等关键指标。
|
20天前
|
存储 Prometheus 监控
在Ubuntu系统上安装与配置Prometheus的步骤
通过以上步骤,您应该已经成功在Ubuntu系统上安装并配置了Prometheus。您现在可以开始使用Prometheus收集和分析您的系统和应用程序的指标数据了。
39 1
|
1月前
|
Ubuntu NoSQL
ubuntu上安装某个程序的符号表和源码包
ubuntu上安装某个程序的符号表和源码包
|
1月前
|
Kubernetes Ubuntu 网络安全
Ubuntu基于kubeadm快速部署K8S实战
关于如何在Ubuntu系统上使用kubeadm工具快速部署Kubernetes集群的详细实战指南。
114 2
下一篇
无影云桌面