Java微服务篇5——Docker
1、虚拟化技术
虚拟化技术是一种计算机资源管理技术,是将计算机的各种实体资源,如服务器、网络、内存及存储 等,予以抽象、转换后呈现出来。虚拟化技术打破了计算机实体结构间的,不可切割的障碍。使用户可 以比原本的组态更好的方式,来应用这些资源
- 高性能的物理硬件产能过剩和老的旧的硬件产能过低的重组重用,透明化底层物理硬件
- 软件跨环境迁移问题(代码的水土不服)
2、Docker简介
官网: https://www.docker.com
文档地址: https://docs.docker.com
仓库地址: https://hub.docker.com
“Docker is the world’s leading software container platform.“,Docker的定位是一 个应用容器平台
Docker 可以让开发者打包他们的应用,以及依赖包到一个轻量级、可移植的容器中,然后发布到任何 流行的 Linux 机器上。Docker容器是完全使用沙箱机制,相互隔离,性能开销也极低,通俗的讲是服务器中高性能的虚拟机,可以将一台物理机虚拟N多台虚拟机的机器,互相之间隔离,互不影响
特点
- 标准化交付:Docker将应用打包成标准化单元,用于交付、部署
- 轻量级:容器及包含了软件运行所需的所有环境,而且非常轻量级
- 高可靠:容器化的应用程序,可以在任何Linux环境中始终如一的运行
- 隔离性:容器化的应用程序,具备隔离性,这样多团队可以共享同一Linux系统资源
容器与虚拟机得对比
特性 | 容器 | 虚拟机 |
---|
启动 | 秒级 | 分钟级 | 硬盘使用 | 一般为MB | 一般为GB | 性能 | 接近原生硬件 | 弱鸡 | 系统支持量 | 单机可跑几十个容器 | 单机几个虚拟OS | 运行环境 | 主要在Linux | 主要在window |
3、Docker基本概念
- 宿主机:安装Docker守护进程的Linux服务器,称之为宿主机
- 镜像(Image):Docker 镜像,就相当于是一个 root 文件系统。除了提供容器运行时所需的程序、 库、资源、配置等文件外,还包含了一些为运行时准备的一些配置参数
- 容器(Container):镜像运行之后的实体,镜像和容器的关系,就像是面向对象程序设计中的类和对 象一样,镜像是静态的定义,容器是镜像运行时的实体。容器可以被创建、启动、停止、删除、暂停等
- 仓库(Repository):仓库可看成一个镜像控制中心,用来保存镜像
4、Docker安装
移除之前版本的依赖
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
安装依赖库
yum install -y yum-utils
设置镜像
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
更新软件包索引
yum makecache fast
安装docker
yum install docker-ce docker-ce-cli containerd.io
测试
[root@VM-8-13-centos ~]
Docker version 20.10.8, build 3967b7d
启动docker
systemctl start docker
docker卸载
卸载docker
yum remove docker-ce docker-ce-cli containerd.io
删除镜像
rm -rf /var/lib/docker
rm -rf /var/lib/containerd
阿里云镜像加速(如果感觉下载镜像慢,添加一下镜像加速)
[root@VM-8-13-centos ~]
[root@VM-8-13-centos ~]
> {
> "registry-mirrors": ["https://12sotewv.mirror.aliyuncs.com"]
> }
> EOF
{
"registry-mirrors": ["https://12sotewv.mirror.aliyuncs.com"]
}
[root@VM-8-13-centos ~]
[root@VM-8-13-centos ~]
5、Docker基本命令
5.1、镜像基本命令
5.1.1、查看镜像
[root@VM-8-13-centos ~]
REPOSITORY TAG IMAGE ID CREATED SIZE
- REPOSITORY 镜像的仓库源
- TAG 镜像的标签
- IMAGE ID 镜像的id
- CREATED 镜像的创建时间
- SIZE 镜像的大
5.1.2、查找镜像
Docker Hub:https://hub.docker.com/
[root@VM-8-13-centos ~]
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11296 [OK]
mariadb MariaDB Server is a high performing open sou… 4291 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 837 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 91
mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 88
centurylink/mysql Image containing mysql. Optimized to be link… 59 [OK]
databack/mysql-backup Back up mysql databases to... anywhere! 46
prom/mysqld-exporter 42 [OK]
deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r… 41 [OK]
tutum/mysql Base docker image to run a MySQL database se… 35
linuxserver/mysql A Mysql container, brought to you by LinuxSe… 31
schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 30 [OK]
mysql/mysql-router MySQL Router provides transparent routing be… 21
centos/mysql-56-centos7 MySQL 5.6 SQL database server 20
arey/mysql-client Run a MySQL client from a docker container 18 [OK]
fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron tas… 16 [OK]
genschsa/mysql-employees MySQL Employee Sample Database 7 [OK]
yloeffler/mysql-backup This image runs mysqldump to backup data usi… 7 [OK]
openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 image… 6
cytopia/mysql-5.5 MySQL 5.5 on CentOS 7 4 [OK]
devilbox/mysql Retagged MySQL, MariaDB and PerconaDB offici… 3
ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 2 [OK]
jelastic/mysql An image of the MySQL database server mainta… 2
widdpim/mysql-client Dockerized MySQL Client (5.7) including Curl… 1 [OK]
monasca/mysql-init A minimal decoupled init container for mysql 0
5.1.3、下载镜像
[root@VM-8-13-centos ~]
5.7: Pulling from library/mysql
e1acddbe380c: Pull complete
bed879327370: Pull complete
03285f80bafd: Pull complete
ccc17412a00a: Pull complete
1f556ecc09d1: Pull complete
adc5528e468d: Pull complete
1afc286d5d53: Pull complete
4d2d9261e3ad: Pull complete
ac609d7b31f8: Pull complete
53ee1339bc3a: Pull complete
b0c0a831a707: Pull complete
Digest: sha256:7cf2e7d7ff876f93c8601406a5aa17484e6623875e64e7acc71432ad8e0a3d7e
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
[root@VM-8-13-centos ~]
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 6c20ffa54f86 2 days ago 448MB
5.1.4、删除容器
[root@VM-8-13-centos ~]
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat latest 266d1269bb29 19 hours ago 668MB
mysql 5.7 6c20ffa54f86 2 days ago 448MB
[root@VM-8-13-centos ~]
Untagged: tomcat:latest
Untagged: tomcat@sha256:1af502b6fd35c1d4ab6f24dc9bd36b58678a068ff1206c25acc129fb90b2a76a
Deleted: sha256:266d1269bb298d6a3259fc2c2a9deaedf8be945482a2d596b64f73343289a56c
Deleted: sha256:439d07aeab2ffb9df1addbc02dc38a0fa9464f6021ffcf2b98c4a1a124e69b27
Deleted: sha256:75141848bd8c6035bd8718875b209c25246548d6e157291e516ff8b1f24c2ce4
Deleted: sha256:4574961f407fe7376fdda09ea06f117f747fc97fdb99f688a7f05a0541a14628
Deleted: sha256:ea393406b6bdae6926be0e47ccd9567ceef45fbe4df33b411eb227596ca94efa
Deleted: sha256:7852f1c402046c585e34532fdf1f88ec5fa0fa24e502b0b6a05b6019c29215a6
Deleted: sha256:12c634660480a3693741588dd6e0792ac0954eef0fcad48e7c4c6781e4b9248b
Deleted: sha256:a84b4e945affc390a49fe16b1e47251671dfc628c111de3a4c5b2e0b09d2649a
Deleted: sha256:4bc4490d37ab1d9664581bcbb9c198cd5c4e7eaf0c5b1607c5ec44a11eaf922f
Deleted: sha256:21b5c6b4b2751d9e70a183749330b4b9e11687a0cb4995f33ee5812846128294
Deleted: sha256:c2ddc1bc2645ab5d982c60434d8bbc6aecee1bd4e8eee0df7fd08c96df2d58bb
5.2、容器命令
安装一个centos镜像
[root@VM-8-13-centos ~]
Using default tag: latest
latest: Pulling from library/centos
7a0437f04f83: Pull complete
Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
5.2.1、新建容器并启动
[root@VM-8-13-centos /]
[root@ff0c9c6e6547 /]
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@ff0c9c6e6547 /]
exit
[root@VM-8-13-centos /]
bin boot data dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
5.2.2、查看容器
[root@VM-8-13-centos /]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@VM-8-13-centos /]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ff0c9c6e6547 centos "/bin/bash" 3 minutes ago Exited (0) 2 minutes ago focused_hamilton
5.2.3、删除容器
[root@VM-8-13-centos /]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ff0c9c6e6547 centos "/bin/bash" 3 minutes ago Exited (0) 2 minutes ago focused_hamilton
[root@VM-8-13-centos /]
ff0c9c6e6547
[root@VM-8-13-centos /]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5.2.4、容器启动与停止
docker start 容器id
docker restart 容器id
docker stop 容器id
docker ki11 容器id
docker run -id centos /bin/bash
5.2.5、查看容器进程信息
[root@VM-8-13-centos /]
0134a448bfff7da25e3b0afe3b7b188ae2eba643148fb7eb0da9f5ac4ddead8c
[root@VM-8-13-centos /]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0134a448bfff centos "/bin/bash" 12 seconds ago Up 11 seconds upbeat_ishizaka
[root@VM-8-13-centos /]
UID PID PPID C STIME TTY TIME CMD
root 12561 12543 0 23:56 ? 00:00:00 /bin/bash
5.2.6、查看容器元数据
[root@VM-8-13-centos /]
[
{
"Id": "0134a448bfff7da25e3b0afe3b7b188ae2eba643148fb7eb0da9f5ac4ddead8c",
"Created": "2021-08-19T15:56:01.562379627Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 12561,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-08-19T15:56:01.850514701Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
"ResolvConfPath": "/var/lib/docker/containers/0134a448bfff7da25e3b0afe3b7b188ae2eba643148fb7eb0da9f5ac4ddead8c/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/0134a448bfff7da25e3b0afe3b7b188ae2eba643148fb7eb0da9f5ac4ddead8c/hostname",
"HostsPath": "/var/lib/docker/containers/0134a448bfff7da25e3b0afe3b7b188ae2eba643148fb7eb0da9f5ac4ddead8c/hosts",
"LogPath": "/var/lib/docker/containers/0134a448bfff7da25e3b0afe3b7b188ae2eba643148fb7eb0da9f5ac4ddead8c/0134a448bfff7da25e3b0afe3b7b188ae2eba643148fb7eb0da9f5ac4ddead8c-json.log",
"Name": "/upbeat_ishizaka",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"CgroupnsMode": "host",
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"KernelMemory": 0,
"KernelMemoryTCP": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/011bb6cfa81323e3f8be510b63adf11dcf308d20853e0816d7578e280d83bc45-init/diff:/var/lib/docker/overlay2/1b7f009ac9f6a4e0335ccf29ece6e8bca4690c893c704dab67ecd558cf10040a/diff",
"MergedDir": "/var/lib/docker/overlay2/011bb6cfa81323e3f8be510b63adf11dcf308d20853e0816d7578e280d83bc45/merged",
"UpperDir": "/var/lib/docker/overlay2/011bb6cfa81323e3f8be510b63adf11dcf308d20853e0816d7578e280d83bc45/diff",
"WorkDir": "/var/lib/docker/overlay2/011bb6cfa81323e3f8be510b63adf11dcf308d20853e0816d7578e280d83bc45/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "0134a448bfff",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": true,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20201204",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "0fcfc03c26b1cfb22f585e9e9bb4d03f1e81cf00d458ec855ba90759b8583f9b",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/0fcfc03c26b1",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "70eb870344f07dc82ed4e6139dbb773dd037da4405aaa5b13e08d8290a3fafcd",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "e37ffb6ce5ddd26628b1a0adf06e212600041884b1ef544cba16278db6acfb07",
"EndpointID": "70eb870344f07dc82ed4e6139dbb773dd037da4405aaa5b13e08d8290a3fafcd",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
]
5.2.7、进入运行中的容器
[root@VM-8-13-centos /]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0134a448bfff centos "/bin/bash" 9 minutes ago Up 9 minutes upbeat_ishizaka
[root@VM-8-13-centos /]
exit
5.2.8、文件拷贝
[root@VM-8-13-centos ~]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0134a448bfff centos "/bin/bash" 22 minutes ago Up 12 minutes upbeat_ishizaka
[root@VM-8-13-centos ~]
[root@0134a448bfff /]
> hello,docker!
> eof
[root@0134a448bfff /]
exit
[root@VM-8-13-centos ~]
[root@VM-8-13-centos ~]
docker.txt
[root@VM-8-13-centos ~]
hello,docker!
|