提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
官方资料查讯 https://docs.docker.com/engine/install/centos/
1 Docker要求:
Docker 引擎可 通过 Docker 桌面在各种Linux 平台、 macOS和Windows 10 上使用,并作为静态二进制安装;
我们先从 centos7学起吧。
Docker是一个进程,一启动就两个进程,一个服务,一个守护进程。占用资源就非常少,启动速度非常快,1s。
一台机器上vm,3到10个实例。docker 100到10000。
1.1.核心概念:
1)镜像images,事先做好一些基础功能(软件),可以在镜像上在来安装新的内容。
2)容器container
3)数据卷volume,把数据映射到linux磁盘目录,业务把数据写入这个目录,容器重新启动时,自己加载数据卷,数据又恢复了。有问题的。(争论,数据库这种方式需求适不适合docker)
4)仓库repository,maven仓库概念一样
1.2. java
类(只读) 镜像(只读,不允许修改)
对象(写临时的内容) 容器,在镜像基础上允许用户写数据,临时数据
docker
部署后台系统,
1)linux Centos7
2)安装jdk
3)安装tomcat
4)部署war包
5)启动tomcat,设置端口,访问
docker可以产生基础镜像,每加一层新的内容也形成新的镜像。每个镜像都可以去在加新的内容。变化无穷,复用资源。
1.3 安装前环境准备
加载虚拟机的vm镜像,先不要打开。
先设置好虚拟机的 网络
NAT模式查询网关配置,上网(注意,设置请根据自己的ip信息进行设置。目的是为了保持网关、子网掩码一致,ip地址网段一致,ip地址不一样)
修改IP地址
# cd /etc/sysconfig/network-scripts/
# ls ifcfg*
ifcfg-ens33 ifcfg-lo
# vi ifcfg-ens33
设置OK后,: wq 保存退出,使用其使设置生效。
重载一下网络配置
# ip addr //查看IP地址
# service network restart //重启网络
Restarting network (via systemctl): [ 确定 ]
# vi /etc/hosts #127.0.0.1 centSVR
# hostname centSVR #注意必须修改机器名hostname
# ping www.baidu.com //测试下网络是否正常连网
**
2 安装Docker
2.1. 第一步:安装工具包
[root@localhost Packages]# yum install -y yum-utils
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
- base: mirrors.tuna.tsinghua.edu.cn
- epel: epel.01link.h
- extras: mirrors.njupt.edu.cn
- updates: mirrors.njupt.edu.cn
软件包 yum-utils-1.1.31-54.el7_8.noarch 已安装并且是最新版本
无须任何处理
2.2第二步:设置远程仓库
[root@localhost Packages]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
已加载插件:fastestmirror, langpacks
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
2.3第三步:安装
[root@localhost Packages]# yum install docker-ce
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
epel/x86_64/metalink | 11 kB 00:00
* base: mirrors.njupt.edu.cn
* epel: fedora.ipserverone.com
* extras: mirrors.njupt.edu.cny
* updates: mirrors.njupt.edu.cn
base | 3.6 kB 00:00
docker-ce-stable | 3.5 kB 00:00
extras | 2.9 kB 00:00
updates | 2.9 kB 00:00
(1/2): docker-ce-stable/7/x86_64/updateinfo | 55 B 00:00
(2/2): docker-ce-stable/7/x86_64/primary_db | 60 kB 00:01
正在解决依赖关系
--> 正在检查事务
---> 软件包 docker-ce.x86_64.3.20.10.6-3.el7 将被 安装
--> 正在处理依赖关系 container-selinux >= 2:2.74,它被软件包 3:docker-ce-20.10.6-3.el7.x86_64 需要
--> 正在处理依赖关系 containerd.io >= 1.4.1,它被软件包 3:docker-ce-20.10.6-3.el7.x86_64 需要
--> 正在处理依赖关系 docker-ce-cli,它被软件包 3:docker-ce-20.10.6-3.el7.x86_64 需要
--> 正在处理依赖关系 docker-ce-rootless-extras,它被软件包 3:docker-ce-20.10.6-3.el7.x86_64 需要
--> 正在检查事务
---> 软件包 container-selinux.noarch.2.2.119.2-1.911c772.el7_8 将被 安装
---> 软件包 containerd.io.x86_64.0.1.4.6-3.1.el7 将被 安装
---> 软件包 docker-ce-cli.x86_64.1.20.10.6-3.el7 将被 安装
--> 正在处理依赖关系 docker-scan-plugin(x86-64),它被软件包 1:docker-ce-cli-20.10.6-3.el7.x86_64 需要
---> 软件包 docker-ce-rootless-extras.x86_64.0.20.10.6-3.el7 将被 安装
--> 正在处理依赖关系 fuse-overlayfs >= 0.7,它被软件包 docker-ce-rootless-extras-20.10.6-3.el7.x86_64 需要
--> 正在处理依赖关系 slirp4netns >= 0.4,它被软件包 docker-ce-rootless-extras-20.10.6-3.el7.x86_64 需要
--> 正在检查事务
---> 软件包 docker-scan-plugin.x86_64.0.0.7.0-3.el7 将被 安装
---> 软件包 fuse-overlayfs.x86_64.0.0.7.2-6.el7_8 将被 安装
--> 正在处理依赖关系 libfuse3.so.3(FUSE_3.2)(64bit),它被软件包 fuse-overlayfs-0.7.2-6.el7_8.x86_64 需要
--> 正在处理依赖关系 libfuse3.so.3(FUSE_3.0)(64bit),它被软件包 fuse-overlayfs-0.7.2-6.el7_8.x86_64 需要
--> 正在处理依赖关系 libfuse3.so.3()(64bit),它被软件包 fuse-overlayfs-0.7.2-6.el7_8.x86_64 需要
---> 软件包 slirp4netns.x86_64.0.0.4.3-4.el7_8 将被 安装
--> 正在检查事务
---> 软件包 fuse3-libs.x86_64.0.3.6.1-4.el7 将被 安装
--> 解决依赖关系完成
依赖关系解决
================================================================================
Package 架构 版本 源 大小
================================================================================
正在安装:
docker-ce x86_64 3:20.10.6-3.el7 docker-ce-stable 27 M
为依赖而安装:
container-selinux noarch 2:2.119.2-1.911c772.el7_8 extras 40 k
containerd.io x86_64 1.4.6-3.1.el7 docker-ce-stable 34 M
docker-ce-cli x86_64 1:20.10.6-3.el7 docker-ce-stable 33 M
docker-ce-rootless-extras
x86_64 20.10.6-3.el7 docker-ce-stable 9.2 M
docker-scan-plugin x86_64 0.7.0-3.el7 docker-ce-stable 4.2 M
fuse-overlayfs x86_64 0.7.2-6.el7_8 extras 54 k
fuse3-libs x86_64 3.6.1-4.el7 extras 82 k
slirp4netns x86_64 0.4.3-4.el7_8 extras 81 k
事务概要
================================================================================
安装 1 软件包 (+8 依赖软件包)
总下载量:107 M
安装大小:438 M
Is this ok [y/d/N]: y
Downloading packages:
(1/9): container-selinux-2.119.2-1.911c772.el7_8.noarch.rp | 40 kB 00:01
warning: /var/cache/yum/x86_64/7/docker-ce-stable/packages/docker-ce-20.10.6-3.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY
docker-ce-20.10.6-3.el7.x86_64.rpm 的公钥尚未安装
(2/9): docker-ce-20.10.6-3.el7.x86_64.rpm | 27 MB 03:35
(3/9): containerd.io-1.4.6-3.1.el7.x86_64.rpm | 34 MB 04:28
(4/9): docker-ce-rootless-extras-20.10.6-3.el7.x86_64.rpm | 9.2 MB 01:33
(5/9): fuse-overlayfs-0.7.2-6.el7_8.x86_64.rpm | 54 kB 00:02
(6/9): fuse3-libs-3.6.1-4.el7.x86_64.rpm | 82 kB 00:02
(7/9): slirp4netns-0.4.3-4.el7_8.x86_64.rpm | 81 kB 00:00
(8/9): docker-scan-plugin-0.7.0-3.el7.x86_64.rpm | 4.2 MB 00:48
(9/9): docker-ce-cli-20.10.6-3.el7.x86_64.rpm | 33 MB 04:19
--------------------------------------------------------------------------------
总计 230 kB/s | 107 MB 07:55
从 https://download.docker.com/linux/centos/gpg 检索密钥
导入 GPG key 0x621E9F35:
用户ID : "Docker Release (CE rpm) <docker@docker.com>"
指纹 : 060a 61c5 1b55 8a7f 742b 77aa c52f eb6b 621e 9f35
来自 : https://download.docker.com/linux/centos/gpg
是否继续?[y/N]:y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
警告:RPM 数据库已被非 yum 程序修改。
** 发现 10 个已存在的 RPM 数据库问题, 'yum check' 输出如下:
elfutils-libelf-devel-0.176-5.el7.x86_64 有缺少的需求 pkgconfig(zlib)
gcc-4.8.5-44.el7.x86_64 有缺少的需求 cpp = ('0', '4.8.5', '44.el7')
libXp-devel-1.0.2-2.1.el7.x86_64 有缺少的需求 pkgconfig(xext)
motif-2.3.4-14.el7_5.x86_64 有缺少的需求 xorg-x11-xbitmaps
motif-devel-2.3.4-14.el7_5.x86_64 有缺少的需求 libXext-devel(x86-64)
motif-devel-2.3.4-14.el7_5.x86_64 有缺少的需求 libXft-devel(x86-64)
motif-devel-2.3.4-14.el7_5.x86_64 有缺少的需求 libXmu-devel(x86-64)
motif-devel-2.3.4-14.el7_5.x86_64 有缺少的需求 libXt-devel(x86-64)
motif-devel-2.3.4-14.el7_5.x86_64 有缺少的需求 libjpeg-devel(x86-64)
motif-devel-2.3.4-14.el7_5.x86_64 有缺少的需求 libpng-devel(x86-64)
正在安装 : 2:container-selinux-2.119.2-1.911c772.el7_8.noarch 1/9
正在安装 : containerd.io-1.4.6-3.1.el7.x86_64 2/9
正在安装 : 1:docker-ce-cli-20.10.6-3.el7.x86_64 3/9
正在安装 : docker-scan-plugin-0.7.0-3.el7.x86_64 4/9
正在安装 : slirp4netns-0.4.3-4.el7_8.x86_64 5/9
正在安装 : fuse3-libs-3.6.1-4.el7.x86_64 6/9
正在安装 : fuse-overlayfs-0.7.2-6.el7_8.x86_64 7/9
正在安装 : docker-ce-rootless-extras-20.10.6-3.el7.x86_64 8/9
正在安装 : 3:docker-ce-20.10.6-3.el7.x86_64 9/9
验证中 : containerd.io-1.4.6-3.1.el7.x86_64 1/9
验证中 : fuse3-libs-3.6.1-4.el7.x86_64 2/9
验证中 : docker-ce-rootless-extras-20.10.6-3.el7.x86_64 3/9
验证中 : fuse-overlayfs-0.7.2-6.el7_8.x86_64 4/9
验证中 : 3:docker-ce-20.10.6-3.el7.x86_64 5/9
验证中 : docker-scan-plugin-0.7.0-3.el7.x86_64 6/9
验证中 : slirp4netns-0.4.3-4.el7_8.x86_64 7/9
验证中 : 2:container-selinux-2.119.2-1.911c772.el7_8.noarch 8/9
验证中 : 1:docker-ce-cli-20.10.6-3.el7.x86_64 9/9
已安装:
docker-ce.x86_64 3:20.10.6-3.el7
作为依赖被安装:
container-selinux.noarch 2:2.119.2-1.911c772.el7_8
containerd.io.x86_64 0:1.4.6-3.1.el7
docker-ce-cli.x86_64 1:20.10.6-3.el7
docker-ce-rootless-extras.x86_64 0:20.10.6-3.el7
docker-scan-plugin.x86_64 0:0.7.0-3.el7
fuse-overlayfs.x86_64 0:0.7.2-6.el7_8
fuse3-libs.x86_64 0:3.6.1-4.el7
slirp4netns.x86_64 0:0.4.3-4.el7_8
2.4第四步:启动
$ **sudo systemctl start docker**
或者
$ **sudo service docker start
service docker start** #启动docker
**chkconfig docker on** #加入开机启动
第五步:查看版本
[root@localhost Packages]# docker version
Client: Docker Engine - Community
Version: 20.10.6
API version: 1.41
Go version: go1.13.15
Git commit: 370c289
Built: Fri Apr 9 22:45:33 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.6
API version: 1.41 (minimum version 1.12)
Go version: go1.13.15
Git commit: 8728dd2
Built: Fri Apr 9 22:43:57 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.6
GitCommit: d71fcd7d8303cbf684402823e425e9dd2e99285d
runc:
Version: 1.0.0-rc95
GitCommit: b9ee9c6314599f1b4a7f497e1f1f856fe433d3b7
docker-init:
Version: 0.19.0
GitCommit: de40ad0
2.6 第六步:校验
[root@localhost Packages]# docker run hello-world
=====这里有点慢
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:5122f6204b6a3596e048758cabba3c46b1c937a46b5be6225b835d091b90e46c
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
2.6.2查看Docker版本
docker --help #帮助
docker –v #简单查看版本
docker version #查看版本
docker info #查看信息
Client:
Context: default
Debug Mode: false
Plugins:
app: Docker App (Docker Inc., v0.9.1-beta3)
buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)
scan: Docker Scan (Docker Inc.)
Server:
Containers: 1
Running: 0
Paused: 0
Stopped: 1
Images: 1
Server Version: 20.10.6
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc io.containerd.runc.v2 io.containerd.runtime.v1.linux
Default Runtime: runc
Init Binary: docker-init
containerd version: d71fcd7d8303cbf684402823e425e9dd2e99285d
runc version: b9ee9c6314599f1b4a7f497e1f1f856fe433d3b7
init version: de40ad0
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-1160.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 2.761GiB
Name: localhost.localdomain
ID: LTDB:O3ZY:PCM5:WYAW:JQHU:MSJS:ZKT2:VNGX:ATNY:FKQP:7WA5:SM6Y
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
3.1安装其它产品的两种方式
docker安装其它产品有两种方式
方式一:通过拉取事先做好的镜像,例如下面oracle的安装方式
Docker 拉取 oracle 11g镜像配置 https://blog.csdn.net/qq_38380025/article/details/80647620
方式二:通过Dockerfile来构建新的镜像,例如下面tomcat的安装方式
参考:
https://blog.csdn.net/laughing1997/article/details/84305615
离线安装
1.下载安装包
下载地址:
https://download.docker.com/linux/static/stable/x86_64/
解压
复制docker到服务器上,解压:
tar -zxvf docker-20.10.9.tgz
mv docker/* /usr/bin/
编辑自启动配置文件
vim /etc/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
[Service]
Type=notify
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=infinity
LimitNPROC=infinity
TimeoutStartSec=0
Delegate=yes
KillMode=process
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target
# chmod +x /etc/systemd/system/docker.service
root@debian9:/home/ives# systemctl daemon-reload
root@debian9:/home/ives# systemctl enable docker.service
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /etc/systemd/system/docker.service.
root@debian9:/home/ives# systemctl start docker.service
root@debian9:/home/ives# systemctl status docker.service
● docker.service - Docker Application Container Engine
Loaded: loaded (/etc/systemd/system/docker.service; enabled; vendor preset:
Active: active (running) since Tue 2022-08-23 15:54:57 CST; 4s ago
Docs: https://docs.docker.com
# docker -v
Docker version 20.10.9, build c2ea9bc
Docker已安装好的情况下, 安装Docker-Compose快速编排工具
下载地址: https://github.com/docker/compose/releases/
# curl -L https://get.daocloud.io/docker/compose/releases/download/1.29.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
//`uname -s` 返回Linux,可能会引起个别版本无法正确下载时,用如下命令
curl -SL https://github.com/docker/compose/releases/download/v2.18.1/docker-compose-linux-x86_64 > /usr/local/bin/docker-compose
# chmod +x /usr/local/bin/docker-compose ## 添加执行权限
# docker-compose -version ##查看版本
Docker-Compose常用方法可以参考 :https://blog.csdn.net/qq_33204709/article/details/122176867