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月前
|
存储 安全 C++
Odoo 安装方式选择:源码安装 vs Docker
Odoo部署常采用源码编译或Docker容器化,但分别面临依赖复杂、版本风险和服务化难题,以及镜像臃肿和扩展受限的问题。Websoft9提出混合方案,融合两者优势:通过智能环境适配、三阶段部署流程(环境预检、混合模式选择、持久化配置)及声明式YAML配置,实现高效、灵活的双模运行时。此方案显著降低依赖冲突解决时间(从83分钟至0),生产环境构建耗时缩短至8分钟,并达100% CVE漏洞修复率,适合ERP定制开发与规模化部署的企业需求。
|
1月前
|
存储 Ubuntu Docker
Ubuntu 安装 docker
本文介绍了在 Ubuntu 系统上安装 Docker 的详细步骤,包括卸载旧版本、设置 Docker 官方存储库、安装 Docker 包以及配置镜像源加速。首先通过移除旧版组件如 `docker.io` 和 `docker-compose` 等进行清理,然后添加官方 GPG 密钥并配置 APT 源,接着安装最新 Docker 组件。此外,还提供了阿里云和 1Panel 的镜像源配置方法以提升下载速度,并通过运行 `hello-world` 镜像测试安装是否成功。
914 3
Ubuntu 安装 docker
|
1月前
|
Linux iOS开发 Docker
MyEMS开源系统安装之Linux/macOS上的DOcker
本指南详细介绍了如何在Linux/macOS上使用Docker部署MyEMS系统。主要内容包括:前置条件(如安装Docker、npm和MySQL),以及分步骤部署各个组件(如myems-api、myems-admin、myems-modbus-tcp等)。每个步骤涵盖源代码复制、环境配置、镜像构建、容器运行及日志管理等操作,并提供了多平台构建的支持。最后,指南还说明了默认端口和登录凭据,帮助用户快速启动并访问MyEMS的管理界面和Web界面。
74 1
|
2月前
|
关系型数据库 MySQL 数据库
Docker Compose-实战
Docker Compose-实战
95 5
|
2月前
|
关系型数据库 MySQL 数据库
Docker 安装常用软件相关命令
本文介绍了在CentOS 7.9系统上安装Docker的详细步骤,包括添加阿里云镜像源、安装Docker及相关组件、启动服务以及配置镜像加速。同时,还展示了如何通过Docker安装MySQL 5.7版本数据库,涵盖拉取镜像、创建数据目录、运行容器及配置挂载点等操作,并提供验证安装成功的命令和截图。适合需要部署Docker与MySQL环境的用户参考。
211 5
|
2月前
|
存储 弹性计算 Linux
安装Docker
常见操作系统安装docker步骤
209 15
|
2月前
|
Docker 容器
在openEuler 22.03 LTS上安装Docker CE和Docker Compose
以上就是在openEuler 22.03 LTS上安装Docker CE和Docker Compose的过程。希望这个指南能帮助你顺利完成安装。
455 12
|
3月前
|
Prometheus 监控 Cloud Native
Prometheus+Grafana(docker安装)
本文档详细介绍了如何使用Docker容器快速部署Prometheus监控系统和Grafana数据可视化平台。该方案适用于需要快速搭建监控环境的开发测试场景,具备部署简单、资源占用低、易于维护等特点。
|
2月前
|
Ubuntu 网络协议 应用服务中间件
在 Ubuntu 上安装 Nginx
在 Ubuntu 上安装和配置 Nginx 非常简单。首先更新系统包,然后通过 `apt` 安装 Nginx,检查服务状态并配置防火墙规则。访问服务器 IP 测试是否成功显示默认页面。还可管理服务、创建虚拟主机及排查常见问题,适合新手快速上手部署高性能 Web 服务。
296 0
|
25天前
|
Ubuntu 机器人 定位技术
Loam在Ubuntu 18.04上的一站式安装指南
现在,你已经完成了Loam在Ubuntu 18.04上的一站式安装盛宴。从更新系统清洁,到搭建魔法环境的工作空间,再到召唤和激活Loam精髓的艺术——每步都妙不可言,每步都至关重要,让你在这场技术的饕餮盛宴中大显身手。
50 8