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 -v
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/dockerrm -rf /var/lib/containerd
阿里云镜像加速(如果感觉下载镜像慢,添加一下镜像加速)
[root@VM-8-13-centos ~]# mkdir -p /etc/docker
[root@VM-8-13-centos ~]# tee /etc/docker/daemon.json <<-'EOF'
> {
> "registry-mirrors": ["https://12sotewv.mirror.aliyuncs.com"]
> }
> EOF
{"registry-mirrors": ["https://12sotewv.mirror.aliyuncs.com"]
}
[root@VM-8-13-centos ~]# systemctl daemon-reload
[root@VM-8-13-centos ~]# systemctl restart docker
5、Docker基本命令
5.1、镜像基本命令
5.1.1、查看镜像
# 查看镜像
[root@VM-8-13-centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
#--------------------------------------参数--------------------------------------
# -a 显示所有镜像
# -q 只显示id
- REPOSITORY 镜像的仓库源
- TAG 镜像的标签
- IMAGE ID 镜像的id
- CREATED 镜像的创建时间
- SIZE 镜像的大
5.1.2、查找镜像
Docker Hub:https://hub.docker.com/
[root@VM-8-13-centos ~]# docker search mysql
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
#--------------------------------------参数--------------------------------------
# --filter=STARS=3000 搜索镜像STARS大于3000的
5.1.3、下载镜像
# docker pull mysql不加版本默认安装最新版得
[root@VM-8-13-centos ~]# docker pull mysql:5.7
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 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 6c20ffa54f86 2 days ago 448MB
5.1.4、删除容器
[root@VM-8-13-centos ~]# docker images
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 ~]# docker rmi 266d1269bb29
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
#--------------------------------------参数--------------------------------------
# 删除多个容器
# docker rmi -f 容器id 容器id 容器id 容器id
# 删除全部的容器
# docker rmi -f $(docker images -aq)
5.2、容器命令
安装一个centos镜像
[root@VM-8-13-centos ~]# docker pull 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 /]# docker run -it centos /bin/bash
[root@ff0c9c6e6547 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@ff0c9c6e6547 /]# exit
exit
[root@VM-8-13-centos /]# ls
bin boot data dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
#--------------------------------------参数--------------------------------------
# --name="Name" 指定容器名称
# -d 后台方式运行
# -it 使用交互方式运行,进入容器查看内容
# -p 指定容器的端口
# -p ip:主机端口:容器端口
# -p 主机端口:容器端口(常用)
# p 容器端口
#----------------------------------centos内部命令---------------------------------
# exit 退出容器(停止运行)
# ctrl+p+q退出容器(不停止运行)
5.2.2、查看容器
# 查看运行中的容器
[root@VM-8-13-centos /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# 查看所有的容器
[root@VM-8-13-centos /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ff0c9c6e6547 centos "/bin/bash" 3 minutes ago Exited (0) 2 minutes ago focused_hamilton
#--------------------------------------参数--------------------------------------
# -a 列出当前正在运行的容器+历史运行的容器
# -n=x 列出最近的x个容器
# -q 只显示容器编号
5.2.3、删除容器
# docker rm -f $(docker ps -aq) 强制删除所有容器
[root@VM-8-13-centos /]# docker ps -a
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 /]# docker rm ff0c9c6e6547
ff0c9c6e6547
[root@VM-8-13-centos /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5.2.4、容器启动与停止
# 启动容器
docker start 容器id
# 重启容器
docker restart 容器id
# 停止容器
docker stop 容器id
# 强制停止容器
docker ki11 容器id
# 后台启动容器,只加-d仍会停止,因为没有前台进程
docker run -id centos /bin/bash
5.2.5、查看容器进程信息
[root@VM-8-13-centos /]# docker run -id centos /bin/bash
0134a448bfff7da25e3b0afe3b7b188ae2eba643148fb7eb0da9f5ac4ddead8c
[root@VM-8-13-centos /]# docker ps
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 /]# docker top 0134a448bfff
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 /]# docker inspect 0134a448bfff
[{"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、进入运行中的容器
# docker exec -it 容器id /bin/bash 开启新的终端
[root@VM-8-13-centos /]# docker ps
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 /]# docker attach 0134a448bfff
exit
5.2.8、文件拷贝
[root@VM-8-13-centos ~]# docker ps
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 ~]# docker exec -it 0134a448bfff /bin/bash
[root@0134a448bfff /]# cat > docker.txt << eof
> hello,docker!
> eof
[root@0134a448bfff /]# exit
exit
[root@VM-8-13-centos ~]# docker cp 0134a448bfff:/docker.txt /root
[root@VM-8-13-centos ~]# ls
docker.txt
[root@VM-8-13-centos ~]# cat docker.txt
hello,docker!
5.3、docker安装nginx
# 拉取nginx镜像
[root@VM-8-13-centos ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
e1acddbe380c: Already exists
e21006f71c6f: Pull complete
f3341cc17e58: Pull complete
2a53fa598ee2: Pull complete
12455f71a9b5: Pull complete
b86f2ba62d17: Pull complete
Digest: sha256:4d4d96ac750af48c6a551d757c1cbfc071692309b491b70b2b8976e102dd3fef
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
# 查看镜像
[root@VM-8-13-centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest dd34e67e3371 2 days ago 133MB
mysql 5.7 6c20ffa54f86 2 days ago 448MB
centos latest 300e315adb2f 8 months ago 209MB
# 启动nginx
[root@VM-8-13-centos ~]# docker run -d -p 3344:80 nginx
0f2ee7ab13bd59420897df035c3c96049854bebb0f0a33f2030cd750c52d8201
[root@VM-8-13-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0f2ee7ab13bd nginx "/docker-entrypoint.…" 7 seconds ago Up 6 seconds 0.0.0.0:3344->80/tcp, :::3344->80/tcp
0134a448bfff centos "/bin/bash" 11 hours ago Up 11 hours
# 本机访问nginx
[root@VM-8-13-centos ~]# curl localhost:3344
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>body {width: 35em;margin: 0 auto;font-family: Tahoma, Verdana, Arial, sans-serif;}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p><p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@VM-8-13-centos ~]# docker exec -it 0f2ee7ab13bd /bin/bash
root@0f2ee7ab13bd:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@0f2ee7ab13bd:/# cd /etc/nginx
root@0f2ee7ab13bd:/etc/nginx# ls
conf.d fastcgi_params mime.types modules nginx.conf scgi_params uwsgi_params
root@0f2ee7ab13bd:/etc/nginx# exit
exit
5.4、docker安装tomcat
[root@VM-8-13-centos ~]# docker pull tomcat
Using default tag: latest
latest: Pulling from library/tomcat
1cfaf5c6f756: Pull complete
c4099a935a96: Pull complete
f6e2960d8365: Pull complete
dffd4e638592: Pull complete
a60431b16af7: Pull complete
4869c4e8de8d: Pull complete
9815a275e5d0: Pull complete
c36aa3d16702: Pull complete
cc2e74b6c3db: Pull complete
1827dd5c8bb0: Pull complete
Digest: sha256:1af502b6fd35c1d4ab6f24dc9bd36b58678a068ff1206c25acc129fb90b2a76a
Status: Downloaded newer image for tomcat:latest
docker.io/library/tomcat:latest
[root@VM-8-13-centos ~]# docker run -id -p 3355:8080 tomcat
c62eb23e8881826a5628e252398e1478731d9a7aa8cfa63de466b70f27500f43
官方tomcat是精简版的,webapps下是没有内容的
[root@VM-8-13-centos ~]# docker exec -it c62eb23e8881 /bin/bash
root@c62eb23e8881:/usr/local/tomcat# whereis tomcat
tomcat: /usr/local/tomcat
root@c62eb23e8881:/usr/local/tomcat# cd /usr/local/tomcat/webapps
root@c62eb23e8881:/usr/local/tomcat/webapps# ls
root@c62eb23e8881:/usr/local/tomcat/webapps# cd ..
root@c62eb23e8881:/usr/local/tomcat# ls
BUILDING.txt CONTRIBUTING.md LICENSE NOTICE README.md RELEASE-NOTES RUNNING.txt bin conf lib logs native-jni-lib temp webapps webapps.dist work
root@c62eb23e8881:/usr/local/tomcat# cp -r webapps.dist/* webapps/
root@c62eb23e8881:/usr/local/tomcat# exit
exit
5.5、docker安装es和kibana
-e环境参数设置
[root@VM-8-13-centos ~]# docker run -id -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms256m -Xmx512m" elasticsearch
76efafc25cd98352266580104be3616c3820b19da4283598f3ec8e0c20d1bd5f
[root@VM-8-13-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
76efafc25cd9 elasticsearch "/docker-entrypoint.…" 6 seconds ago Up 5 seconds 0.0.0.0:9200->9200/tcp, :::9200->9200/tcp, 0.0.0.0:9300->9300/tcp, :::9300->9300/tcp inspiring_napier
[root@VM-8-13-centos ~]# curl localhost:9200
{"name" : "GjyKC7_","cluster_name" : "elasticsearch","cluster_uuid" : "iiOQUawST7ugDJfajVaEUg","version" : {"number" : "5.6.12","build_hash" : "cfe3d9f","build_date" : "2018-09-10T20:12:43.732Z","build_snapshot" : false,"lucene_version" : "6.6.1"},"tagline" : "You Know, for Search"
}
5.6、提交镜像
[root@VM-8-13-centos ~]# docker commit -m="add webapps" -a="winkto" c62eb23e8881 tomcat-plus:1.0
sha256:e6c378b49281c3f156ac25711c86aa5cf2d85c12a10038d013c26d423bd0e8a5
[root@VM-8-13-centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat-plus 1.0 e6c378b49281 23 seconds ago 673MB
tomcat latest 266d1269bb29 36 hours ago 668MB
nginx latest dd34e67e3371 2 days ago 133MB
mysql 5.7 6c20ffa54f86 2 days ago 448MB
centos latest 300e315adb2f 8 months ago 209MB
elasticsearch latest 5acf0e8da90b 2 years ago 486MB
6、数据卷
当删除容器时,容器里的数据也随之丢失,但有时候希望数据持久化(同步到本地)
第一个连接
[root@VM-8-13-centos ~]# docker run -it --name Vcentos -v /home/Vcentos:/home centos
[root@3e48cef6a24f /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@3e48cef6a24f /]# cd /home
[root@3e48cef6a24f home]# ls
[root@3e48cef6a24f home]# echo > centos.txt << eof
> hello,centos!
> eof
第二个连接
[root@VM-8-13-centos ~]# cd /home/
[root@VM-8-13-centos home]# ls
lighthouse Vcentos
[root@VM-8-13-centos home]# cd Vcentos/
[root@VM-8-13-centos Vcentos]# ls
centos.txt
容器会直接操作宿主机得文件,删除容器数据也不会丢失
6.1、使用数据卷将mysql数据放在宿主机
[root@VM-8-13-centos ~]# docker run -id --name Vmysql -e MYSQL_ROOT_PASSWORD=123456 -p 3366:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql mysql:5.7
dbc17238076b1ad37fa885bfb922b9ca2fa045eea046265060d3d1bc5f63c07a
6.2、匿名挂载、具名挂载
匿名挂载(在-v只写了容器内的路径,没有写容器外的路径)
docker run -id -p 3344:80 -v /ect/nginx nginx
# 查看对应的随机挂载目录 docker volumes ls
# 默认挂载路径为/var/lib/docker/volumes/xxx/_data
具名挂载
docker run -id -p 3344:80 -v nginx-config: /etc/nginx nginx
# 默认挂载路径为/var/lib/docker/volumes/nginx-config/_data
6.3、数据卷容器
多个容器数据互通
第一个连接
[root@VM-8-13-centos ~]# docker run -id --name centos1 -v /home/contos-data:/home centos
c648a76683917ecaf5787588a17264f14f9902c86f73ceec2c6ec42e4ca41b97
[root@VM-8-13-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c648a7668391 centos "/bin/bash" 25 seconds ago Up 25 seconds centos1
dbc17238076b mysql:5.7 "docker-entrypoint.s…" 38 minutes ago Up 38 minutes 33060/tcp, 0.0.0.0:3366->3306/tcp, :::3366->3306/tcp Vmysql
76efafc25cd9 elasticsearch "/docker-entrypoint.…" 2 hours ago Up 2 hours 0.0.0.0:9200->9200/tcp, :::9200->9200/tcp, 0.0.0.0:9300->9300/tcp, :::9300->9300/tcp inspiring_napier
[root@VM-8-13-centos ~]# docker run -id --name centos2 --volumes-from centos1 centos
232c7289bef9935fa1ed11143ca730a6150809a57c484e169e9938112852e1bf
第二个连接
[root@VM-8-13-centos ~]# docker exec -it 232c7289bef9 /bin/bash
[root@232c7289bef9 /]# cd /home
[root@232c7289bef9 home]# touch centos2.txt
第一个连接
[root@VM-8-13-centos ~]# docker exec -it c648a7668391 /bin/bash
[root@c648a7668391 /]# cd /home
[root@c648a7668391 home]# ls
centos2.txt
7、DockerFile
构建自己的镜像
7.1、DockerFile指令(关键词)
- FROM:基础镜像
- MAINTAINER:作者
- RUN:镜像执行时需要执行的命令
- ADD:添加文件
- WORKDRIR:工作目录
- VOLUMES:挂载卷
- EXPOSE:暴露端口
- CMD:指定这个镜像运行要运行的命令(只有最后一个会生效)
- ENV:构建的时候设置环境变量
7.2、构建自己的centos
# 书写DockerFile
[root@VM-8-13-centos dockerfile]# cat Icentos
FROM centos
MAINTAINER winkto<winkto@qq.com>ENV IPATH /usr/local
WORKDIR IPATHRUN yum -y install vimEXPOSE 80CMD echo "---winkto:complete!---"
CMD /bin/bash
# 构建镜像
[root@VM-8-13-centos dockerfile]# docker build -f Icentos -t icentos:0.1 .
Sending build context to Docker daemon 2.048kB
Step 1/8 : FROM centos---> 300e315adb2f
Step 2/8 : MAINTAINER winkto<winkto@qq.com>---> Running in c159f259a790
Removing intermediate container c159f259a790---> 2305fb8e4c88
Step 3/8 : ENV IPATH /usr/local---> Running in 235f6d28da20
Removing intermediate container 235f6d28da20---> c039552d931c
Step 4/8 : WORKDIR IPATH---> Running in fc54ce7608ee
Removing intermediate container fc54ce7608ee---> 078ebb1c0351
Step 5/8 : RUN yum -y install vim---> Running in 80dd1b25308a
CentOS Linux 8 - AppStream 6.3 MB/s | 8.8 MB 00:01
CentOS Linux 8 - BaseOS 261 kB/s | 5.6 MB 00:21
CentOS Linux 8 - Extras 19 kB/s | 10 kB 00:00
Dependencies resolved.
================================================================================Package Arch Version Repository Size
================================================================================
Installing:vim-enhanced x86_64 2:8.0.1763-15.el8 appstream 1.4 M
Installing dependencies:gpm-libs x86_64 1.20.7-17.el8 appstream 39 kvim-common x86_64 2:8.0.1763-15.el8 appstream 6.3 Mvim-filesystem noarch 2:8.0.1763-15.el8 appstream 48 kwhich x86_64 2.21-12.el8 baseos 49 kTransaction Summary
================================================================================
Install 5 PackagesTotal download size: 7.8 M
Installed size: 30 M
Downloading Packages:
(1/5): gpm-libs-1.20.7-17.el8.x86_64.rpm 324 kB/s | 39 kB 00:00
(2/5): vim-filesystem-8.0.1763-15.el8.noarch.rp 1.5 MB/s | 48 kB 00:00
(3/5): vim-enhanced-8.0.1763-15.el8.x86_64.rpm 4.4 MB/s | 1.4 MB 00:00
(4/5): which-2.21-12.el8.x86_64.rpm 86 kB/s | 49 kB 00:00
(5/5): vim-common-8.0.1763-15.el8.x86_64.rpm 4.9 MB/s | 6.3 MB 00:01
--------------------------------------------------------------------------------
Total 2.3 MB/s | 7.8 MB 00:03
warning: /var/cache/dnf/appstream-02e86d1c976ab532/packages/gpm-libs-1.20.7-17.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
CentOS Linux 8 - AppStream 1.6 MB/s | 1.6 kB 00:00
Importing GPG key 0x8483C65D:Userid : "CentOS (CentOS Official Signing Key) <security@centos.org>"Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65DFrom : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transactionPreparing : 1/1 Installing : which-2.21-12.el8.x86_64 1/5 Installing : vim-filesystem-2:8.0.1763-15.el8.noarch 2/5 Installing : vim-common-2:8.0.1763-15.el8.x86_64 3/5 Installing : gpm-libs-1.20.7-17.el8.x86_64 4/5 Running scriptlet: gpm-libs-1.20.7-17.el8.x86_64 4/5 Installing : vim-enhanced-2:8.0.1763-15.el8.x86_64 5/5 Running scriptlet: vim-enhanced-2:8.0.1763-15.el8.x86_64 5/5 Running scriptlet: vim-common-2:8.0.1763-15.el8.x86_64 5/5 Verifying : gpm-libs-1.20.7-17.el8.x86_64 1/5 Verifying : vim-common-2:8.0.1763-15.el8.x86_64 2/5 Verifying : vim-enhanced-2:8.0.1763-15.el8.x86_64 3/5 Verifying : vim-filesystem-2:8.0.1763-15.el8.noarch 4/5 Verifying : which-2.21-12.el8.x86_64 5/5 Installed:gpm-libs-1.20.7-17.el8.x86_64 vim-common-2:8.0.1763-15.el8.x86_64 vim-enhanced-2:8.0.1763-15.el8.x86_64 vim-filesystem-2:8.0.1763-15.el8.noarchwhich-2.21-12.el8.x86_64 Complete!
Removing intermediate container 80dd1b25308a---> cbbc9e3738e0
Step 6/8 : EXPOSE 80---> Running in 45c778622ace
Removing intermediate container 45c778622ace---> e04a1aee8eb1
Step 7/8 : CMD echo "---winkto:complete!---"---> Running in 68116951c5e5
Removing intermediate container 68116951c5e5---> 7d5eeac94d01
Step 8/8 : CMD /bin/bash---> Running in 04463e1a3598
Removing intermediate container 04463e1a3598---> cdcd8761b747
Successfully built cdcd8761b747
Successfully tagged icentos:0.1
[root@VM-8-13-centos dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
icentos 0.1 cdcd8761b747 2 minutes ago 277MB
tomcat-plus 1.0 e6c378b49281 2 hours ago 673MB
tomcat latest 266d1269bb29 38 hours ago 668MB
nginx latest dd34e67e3371 2 days ago 133MB
mysql 5.7 6c20ffa54f86 2 days ago 448MB
centos latest 300e315adb2f 8 months ago 209MB
elasticsearch latest 5acf0e8da90b 2 years ago 486MB
# 测试icentos
[root@VM-8-13-centos dockerfile]# docker run -it icentos:0.1
# 使用vim命令,发现可以成功
[root@6507509d51b9 IPATH]# vim icentos.txt
[root@6507509d51b9 IPATH]# cat icentos.txt
111
[root@6507509d51b9 IPATH]# exit
exit
7.3、构建jdk+tomcat
[root@VM-8-13-centos diyimage]# ls
apache-tomcat-9.0.50.tar.gz Dockerfile jdk-8u221-linux-x64.tar.gz readme.txt
[root@VM-8-13-centos diyimage]# cat Dockerfile
FROM centos
MAINTAINER winto<winkto@qq.com>COPY readme.txt /usr/local/readme.txtADD jdk-8u221-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-9.0.50.tar.gz /usr/local/RUN yum -y install vimENV IPATH /usr/local
WORKDIR $IPATHENV JAVA_HOME /usr/local/jdk1.8.0_221
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.50
ENV CATALINA_BASE /usr/local/apache-tomcat-9.0.50
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/binEXPOSE 8080CMD /usr/local/apache-tomcat-9.0.50/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.50/bin/logs.catalina.out
[root@VM-8-13-centos diyimage]# docker build -t diyimages .
# 构建过程这里省略了
[root@VM-8-13-centos diyimage]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
diyimages latest 15a7d733dde5 12 seconds ago 700MB
icentos 0.1 cdcd8761b747 4 hours ago 277MB
tomcat-plus 1.0 e6c378b49281 6 hours ago 673MB
tomcat latest 266d1269bb29 41 hours ago 668MB
nginx latest dd34e67e3371 3 days ago 133MB
mysql 5.7 6c20ffa54f86 3 days ago 448MB
centos latest 300e315adb2f 8 months ago 209MB
elasticsearch latest 5acf0e8da90b 2 years ago 486MB
[root@VM-8-13-centos home]# docker run -d -p 9090:8080 -v /home/diyimages/tomcat/webapps:/usr/local/apache-tomcat-9.0.50/webapps -v /home/diyimages/tomcat/logs:/usr/local/apache-tomcat-9.0.50/logs diyimages
3a4bab95f9af52b5f02b3eb50199e0a2f0f4f083f6aac2efa604ec74fad3c7f9
[root@VM-8-13-centos home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3a4bab95f9af diyimages "/bin/sh -c '/usr/lo…" 5 seconds ago Up 4 seconds 0.0.0.0:9090->8080/tcp, :::9090->8080/tcp wonderful_wu
dbc17238076b mysql:5.7 "docker-entrypoint.s…" 5 hours ago Up 5 hours 33060/tcp, 0.0.0.0:3366->3306/tcp, :::3366->3306/tcp Vmysql
76efafc25cd9 elasticsearch "/docker-entrypoint.…" 6 hours ago Up 6 hours 0.0.0.0:9200->9200/tcp, :::9200->9200/tcp, 0.0.0.0:9300->9300/tcp, :::9300->9300/tcp inspiring_napier
在webapps下放置tomcat得主页,此时就看到了那只熟悉的猫
8、提交镜像
8.1、Docker Hub
# 需要先登录
[root@VM-8-13-centos webapps]# docker login -uwinkto
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-storeLogin Succeeded
# 修改tag
[root@VM-8-13-centos ~]# docker tag diyimages winkto/diyimages:1.0
# 上传到docker hub
[root@VM-8-13-centos ~]# docker push winkto/diyimages:1.0
The push refers to repository [docker.io/winkto/diyimages]
d57623a43b66: Pushed
9f00b041ca41: Pushed
ba560509a0e1: Pushed
8f9d0419adbb: Pushed
2653d992f4ef: Pushed
1.0: digest: sha256:b62bc8e77cbc3608d4d4fd39fa27d0473c32ab038214418ff5f2c475165a16a1 size: 1373
8.2、阿里云镜像
# 登录到阿里云仓库
[root@VM-8-13-centos ~]# docker login --username=烈焰绿柳 registry.cn-hangzhou.aliyuncs.com
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-storeLogin Succeeded
# 上传到仓库(这里改了下仓库)
[root@VM-8-13-centos ~]# docker push registry.cn-hangzhou.aliyuncs.com/winkto/diyimages:1.0
The push refers to repository [registry.cn-hangzhou.aliyuncs.com/winkto/diyimages]
d57623a43b66: Pushed
9f00b041ca41: Pushed
ba560509a0e1: Pushed
8f9d0419adbb: Pushed
2653d992f4ef: Pushed
1.0: digest: sha256:b62bc8e77cbc3608d4d4fd39fa27d0473c32ab038214418ff5f2c475165a16a1 size: 1373