docker安装卸载以及使用
# 1、卸载旧版的docker
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
# 2、需要的安装包
yum install -y yum-utils
# 3、设置镜像的仓库
yum-config-manager \
--add-repo \
https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
# 更新yum软件包索引
yum makecache fast
# 4、安装docker docker-ce 社区版 ee 企业版
yum install -y docker-ce docker-ce-cli containerd.io
# 5、启动docker
systemctl start docker
# 6、测试docker是否安装成功
docker version
# 7、hello-world
docker run hello-world
# 8、查看一下下载的这个 hello-world 镜像
[root@iZ2ze261ew0wzy5zenjh2xZ local]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 3 weeks ago 13.3kB
了解:卸载docker
# 先把docker服务停止
systemctl stop docker
# 1、卸载依赖
yum remove docker-ce docker-ce-cli containerd.io
# 2、删除资源
rm -rf /var/lib/docker
# /var/lib/docker docker的默认工作路径!
镜像命令
docker images 查看所有本地的主机上的镜像
[root@iZ2ze261ew0wzy5zenjh2xZ docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 3 weeks ago 13.3kB
# 解释
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的ID
CREATED 镜像的创建时间
SIZE 镜像的大小
# 可选项
-a, --all #列出所有镜像
-q, --quiet #只显示镜像的ID
docker search 搜索镜像
[root@iZ2ze261ew0wzy5zenjh2xZ docker]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10676 [OK]
mariadb MariaDB Server is a high performing open sou… 4010 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 780 [OK]
# 可选项,过滤
--filter=STARS=3000 #搜索出来的镜像就是STARS大于3000的
[root@iZ2ze261ew0wzy5zenjh2xZ docker]# docker search mysql --filter=stars=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10676 [OK]
mariadb MariaDB Server is a high performing open sou… 4010 [OK]
docker pull 下载镜像
# 下载镜像 docker pull 镜像名[:tag]
[root@iZ2ze261ew0wzy5zenjh2xZ docker]# docker pull mysql #默认下载的是最新版的mysql
# 指定版本下载
[root@iZ2ze261ew0wzy5zenjh2xZ docker]# docker pull mysql:5.7 #下载的是5.7版的mysql
# 可以使用 docker images 查看下载的镜像
docker rmi 删除镜像
[root@iZ2ze261ew0wzy5zenjh2xZ docker]# docker rmi -f d1165f221234
容器命令
说明:有镜像才可以创建容器,Linux,下载一个centos镜像来测试学习
docker pull centos
新建容器并启动
docker run [可选参数] image
# 参数说明
--name="Name" 容器名字 tomcat01 tomcat02,用来区分容器
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口 -p 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口(常用)
-p 容器端口
-P 随机指定端口
退出容器
exit # 直接容器停止并退出
ctrl + p + q #容器不停止退出 或者Ctrl+d
删除容器
docker rm 容器id # 删除指定的容器,不能删除正在运行的容器,如果要强制删除 rm -f
docker rm -f $(docker ps -aq) # 删除所有的容器
docker rm -a -q|xargs docker rm #删除所有的容器
启动和停止容器的操作
docker start 容器id # 启动容器
docker restart 容器id # 重启容器
docker stop 容器id # 停止当前正在运行的容器
docker kill 容器id # 强制停止当前容器
常用其他命令
后台启动容器
# 命令 docker run -d 镜像名
# docker run -d centos
# 问题 docker ps ,发现centos停止了
# 常见的坑,docker容器使用后台运行,就必须要有一个前台进程,docker发现没有应用就会自动停止
# nginx,容器启动后,发现自己没哟提供服务,就会立刻停止。就是没有程序了
查看日志
docker logs -f -t --tail [number] 容器id,没有日志,因为没有操作
# docker run -it centos /bin/bash , 进入容器有操作就有日志
或者
# docker run -d centos /bin/bash -c "while true;do echo www;sleep 3;done" ,
每隔3秒写一个www,产生日志,再用
# docker logs -tf --tail 10 容器ID
查看产生的日志 --tail [参数] 用来显示多少行数
-tf #显示日志
查看镜像的元数据
[root@iZ2ze261ew0wzy5zenjh2xZ ~]# docker inspect 862ed1bc55a7
[
{
"Id": "862ed1bc55a7d026a17f0b1a4a261c0ff76ebb88e31ac619d0ee81e4d67232c7",
"Created": "2021-04-01T02:41:22.562883069Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 55765,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-04-01T03:05:06.111738395Z",
"FinishedAt": "2021-04-01T02:47:42.224360066Z"
},
"Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
"ResolvConfPath": "/var/lib/docker/containers/862ed1bc55a7d026a17f0b1a4a261c0ff76ebb88e31ac619d0ee81e4d67232c7/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/862ed1bc55a7d026a17f0b1a4a261c0ff76ebb88e31ac619d0ee81e4d67232c7/hostname",
"HostsPath": "/var/lib/docker/containers/862ed1bc55a7d026a17f0b1a4a261c0ff76ebb88e31ac619d0ee81e4d67232c7/hosts",
"LogPath": "/var/lib/docker/containers/862ed1bc55a7d026a17f0b1a4a261c0ff76ebb88e31ac619d0ee81e4d67232c7/862ed1bc55a7d026a17f0b1a4a261c0ff76ebb88e31ac619d0ee81e4d67232c7-json.log",
"Name": "/epic_shannon",
"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/6b7891313697fd320e1355bb72bd9889ef263932149a823c6d79556debdb0fb7-init/diff:/var/lib/docker/overlay2/c1d31f0d19365fe68b75826b1f17fc0bb5eb9fc29fe886019023c65717b37b47/diff",
"MergedDir": "/var/lib/docker/overlay2/6b7891313697fd320e1355bb72bd9889ef263932149a823c6d79556debdb0fb7/merged",
"UpperDir": "/var/lib/docker/overlay2/6b7891313697fd320e1355bb72bd9889ef263932149a823c6d79556debdb0fb7/diff",
"WorkDir": "/var/lib/docker/overlay2/6b7891313697fd320e1355bb72bd9889ef263932149a823c6d79556debdb0fb7/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "862ed1bc55a7",
"Domainname": "",
"User": "",
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"Tty": true,
"OpenStdin": true,
"StdinOnce": true,
"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": "f5e19523fa10dbe27f6ac6df65921b90261759af1d54658f673ba7d55cc9e6cf",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/f5e19523fa10",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "acb1d725b711d2d73432cb9b66c82b9216e434617d18bf4d7d1e901a73a28b95",
"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": "203a54de0f1ce79f9bc8159b3af851168210981fd8d286b123eb1def0b0d76bf",
"EndpointID": "acb1d725b711d2d73432cb9b66c82b9216e434617d18bf4d7d1e901a73a28b95",
"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
}
}
}
}
]
进入当前正在运行的容器
# docker exec -it 容器id /bin/bash
进入容器后,开启一个新的终端,可以在里面操作
# docker attach 容器id /bin/bash
进入容器后,进到正在执行的终端,不会启动新的终端
从容器内拷贝文件到主机上
# docker cp 容器id:/文件地址 /容器外要拷贝到的地址
docker 安装nginx
# 1、搜索镜像 search
# 2、下载镜像 pull
# 3、运行测试
[root@iZ2ze261ew0wzy5zenjh2xZ /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 3 weeks ago 13.3kB
nginx latest f6d0b4767a6c 2 months ago 133MB
centos latest 300e315adb2f 3 months ago 209MB
# -d 后台运行
# --name 给容器命名
# -p 宿主机端口,容器内部端口
[root@iZ2ze261ew0wzy5zenjh2xZ /]# docker run -d --name nginx01 -p 8081:80 nginx
46d7baf86853094fadd6fdb1a4ccb85fd9216277bebab9904abb3157b75df3f7
[root@iZ2ze261ew0wzy5zenjh2xZ /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
46d7baf86853 nginx "/docker-entrypoint.…" 29 minutes ago Up 24 minutes 0.0.0.0:8081->80/tcp nginx01
862ed1bc55a7 centos "/bin/bash" 4 hours ago Up 4 hours epic_shannon
[root@iZ2ze261ew0wzy5zenjh2xZ /]# curl localhost:8081
<!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>
docker安装tomcat
# 官方使用--用来测试
docker run -it --rm tomcat:9.0
# 用完即删
# 下载
docker pull tomcat --默认最新版本
# 启动运行
docker run -d -p 1433:8080 --name tomcat01 tomcat
# 进入容器
docker exec -it tomcat01 /bin/bash
# cd /usr/local/tomcat 把 webapps.list 下的文件拷贝到webapps下才能正确访问到tomcat
# cp -r webapps.list/* webapps
# curl localhost:1433
docker 镜像的特点
commit
容器数据卷
什么是容器数据卷
docker的理念回顾
将应用和环境打包成一个镜像!
数据?如果数据都在容器中,那么我么容器删除,数据就会丢失!需求:数据可以持久化
mysql,容器删了!删库跑路! 需求:mysql数据可以存储在本地!
容器之间可以有一个数据共享的技术!docker容器中产生的数据,同步到本地!
这就 是卷技术!目录的挂载,将我们容器内的目录,挂载到Linux上面!
总结一句话:容器的持久化和同步操作!容器建也是可以数据共享的!
实战:安装mysql并挂载
具名和匿名挂载
# 匿名挂载
-v 容器内路径
-P 随机指定宿主机端口和容器内端口映射
docker run -d -P --name nginx01 -v /etc/nginx nginx
#查看卷的所有情况
# docker volume ls
[root@iZ2ze261ew0wzy5zenjh2xZ data]# docker volume ls
DRIVER VOLUME NAME
local 6c8d91314d0c23b08c6f6e8bd41bd1733fcfef904cb916c42c2d4fa7f4af2b6c
# 这里发现,这种就是匿名挂载,我们只在-v 只写了容器内的路径,没有写容器外的路径!
# 具名挂载
[root@iZ2ze261ew0wzy5zenjh2xZ /]# docker run -d -P -v juming-nginx04:/etc/nginx --name nginx04 nginx
eb83e3e7b71c4ff21ee462aa23564d1ff1e799ee9bb60ec1974b113d3bf0ed84
[root@iZ2ze261ew0wzy5zenjh2xZ /]# docker volume ls
DRIVER VOLUME NAME
local 2c425f8cd07e8435b03af6039b57dab0e86d7b2ba682d01d5d9f1710ceb1642c
local 6c8d91314d0c23b08c6f6e8bd41bd1733fcfef904cb916c42c2d4fa7f4af2b6c
local juming-nginx04
# 通过 -v 卷名:容器内路径
# 查看一下这个卷
[root@iZ2ze261ew0wzy5zenjh2xZ /]# docker volume inspect juming-nginx04
[
{
"CreatedAt": "2021-04-02T12:59:41+08:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/juming-nginx04/_data",
"Name": "juming-nginx04",
"Options": null,
"Scope": "local"
}
]
# 所有的docker容器内的卷,没有指定目录的情况下都在/var/lib/docker/volumes/xxxx/_data
我们通过具名挂载可以方便的找到我们的一个卷,大多数情况在使用的 ==具名挂载==
# 如何确定是具名挂载还是匿名挂载,还是指定路径挂载!
-v 容器内路径 #匿名挂载
-v 卷名:容器内路径 #具名挂载
-v /宿主机路径:/容器内路径 # 指定路径挂载
拓展
# 通过 -v 容器内路径:ro rw 改变读写权限
ro readonly # 只读
rw readwrite # 可读可写
#一旦这个设置了容器权限,容器对我们挂载出来的内容就有限定了!
[root@iZ2ze261ew0wzy5zenjh2xZ /]# docker run -d -P -v juming-nginx04:/etc/nginx:ro --name nginx04 nginx
[root@iZ2ze261ew0wzy5zenjh2xZ /]# docker run -d -P -v juming-nginx04:/etc/nginx:rw --name nginx04 nginx
# ro 只要看到ro就说明这个路径只能通过宿主机来操作,容器内部是无法操作的!
ro 容器内部无法操作!!!
数据卷容器
[root@iZ2ze261ew0wzy5zenjh2xZ ~]# docker run -d -p 3310:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql02 mysql:5.7
# 创建一个容器-和mysql01公用一个数据卷(宿主机和mysql01和mysql02都可以同步数据)
[root@iZ2ze261ew0wzy5zenjh2xZ ~]# docker run -d -p 3311:3306 -e MYSQL_ROOT_PASSWORD=111111 --name mysql02 --volumes-from mysql01 mysql:5.7
6f72801eea425be5c51b8c47bbfedfd6d150ae104893ed681441be07f8f0f9a9
-----------------------------------------------------------------------------
mysql数据库好像不太好使的样子
多个MySQL实例共享数据卷,要看MySQL Server之间是怎么处理共享存储上文件并发读写问题的。如果MySQL用的是文件锁,数据卷需要支持文件锁。
推荐的方式是每个MySQL实例独占一个数据卷,采用主从复制或者多Master。
也就是,mysql数据文件不能同时被多个文件读写
-----------------------------------------------------------------------------
--volumes-from 实现两个容器数据同步!
结论:
容器之间配置信息的传递,数据卷容器的生命周期一直持续到没有容器使用为止。
但是一旦你持久化到了本地,这个时候,本地的数据是不会删除的!
dockerfile
dockerfile是用来构建docker镜像的文件!命令参数脚本!
构建步骤:
1、编写一个dockerfile文件
2、docker build构建成为一个镜像
3、docker run 运行镜像
4、docker push 发布镜像(dockerhub、阿里云镜像仓库!)
dockerfile构建过程
基础知识:
1、每个保留关键字(指令)都是必须是大写字母
2、执行从上到下顺序执行
3、# 表示注释
4、每一个指令都会创建提交一个新的镜像层,并提交!
dockerfile是面向开发的,我们以后要发布项目,做镜像,就需要编写dockerfile文件,这个文件十分简单
docker镜像 逐渐成为企业交付的标准,必须要掌握!
dockerfile:构建文件,定义了一切的步骤,源代码
dockerimages:通过dockerfile构建生成的镜像,最终发布和运行的产品
docker容器:容器就是镜像运行起来提供服务器
dockerfile的指令
FROM # 基础镜像,一切从这开始构建
MAINTAINER # 镜像是谁写的,姓名+邮箱
RUN # 镜像构建的时候要运行的命令
ADD # 步骤,tomcat镜像,这个tomcat压缩包!添加内容
WORKDIR # 镜像的工作目录
VOLUME # 挂载的目录
EXPOST # 保留端口配置
CMD # 指定这个容器启动的时候要运行的命令
ENTRYPOINT # 指定这个容器启动的时候要运行的命令,可以追加命令
ONBUILD # 当构建一个被继承 dockerfile 这个时候就会运行ONBUILD 的指令,出发指令
COPY # 类似ADD,将我们文件拷贝到镜像中
ENV # 构建的时候设置环境变量!
ENV MYPATH # 指定当进入容器时所处的位置
实战测试
docker hub 中99%镜像都是从这个基础镜像过来的FROM 的scratch,然后配置需要的软件和配置来进行的构建
创建一个自己的centos
# 1、编写dockerfile的文件
[root@iZ2ze261ew0wzy5zenjh2xZ dockerfile]# cat dockerfile-centos
FROM centos
MAINTAINER wuguofeng<123@qq.com>
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
RUN echo $MYPATH
RUN echo "-----end------"
CMD /bin/bash
# 2、通过这个文件构建镜像
# 命令 docker build -f dockerfile-centos -t textcentos:0.1 .
--注意后面的.点
# 3、测试运行
[root@iZ2ze261ew0wzy5zenjh2xZ ~]# docker run -it textcentos:0.1
[root@213e211b2acc local]# pwd
/usr/local
[root@213e211b2acc local]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.4 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:04 txqueuelen 0 (Ethernet)
RX packets 13 bytes 1165 (1.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@iZ2ze261ew0wzy5zenjh2xZ ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
textcentos 0.1 9dfecdb872e1 About an hour ago 242MB
mysql 5.7 a70d36bc331a 2 months ago 449MB
nginx latest f6d0b4767a6c 2 months ago 133MB
centos latest 300e315adb2f 3 months ago 209MB
# 看看构建过程
[root@iZ2ze261ew0wzy5zenjh2xZ ~]# docker history 9dfecdb872e1
IMAGE CREATED CREATED BY SIZE COMMENT
9dfecdb872e1 About an hour ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "/bin… 0B
c37d66fb8239 About an hour ago /bin/sh -c echo "-----end------" 0B
39d281b56e62 About an hour ago /bin/sh -c echo $MYPATH 0B
8fe1356f0df5 About an hour ago /bin/sh -c #(nop) EXPOSE 80 0B
323f59102150 About an hour ago /bin/sh -c yum -y install net-tools 32.5MB
80800f78e54a 2 hours ago /bin/sh -c #(nop) WORKDIR /usr/local 0B
a878a31893c0 2 hours ago /bin/sh -c #(nop) ENV MYPATH=/usr/local 0B
d49d25fed779 2 hours ago /bin/sh -c #(nop) MAINTAINER wuguofeng 0B
300e315adb2f 3 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 3 months ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> 3 months ago /bin/sh -c #(nop) ADD file:bd7a2aed6ede423b7… 209MB
CMD 和ENTRYPOINT的区别
CMD # 指定这个容器启动的时候要运行的命令,只有最后一个会生效,可被替代
ENTRYPOINT # 指定这个容器启动的时候要运行的命令,可以追加命令
测试CMD
# 编写cmd
[root@iZ2ze261ew0wzy5zenjh2xZ dockerfile]# vim dockerfile-entrypoint
FROM centos
CMD ["ls","-a"]
# 构建镜像
[root@iZ2ze261ew0wzy5zenjh2xZ dockerfile]# docker build -f dockerfile-entrypoint -t centos-entrypoint .
Sending build context to Docker daemon 3.072kB
Step 1/2 : FROM centos
---> 300e315adb2f
Step 2/2 : CMD ["ls","-a"]
---> Running in 17a8e6cc90db
Removing intermediate container 17a8e6cc90db
---> a99fd74544ec
Successfully built a99fd74544ec
Successfully tagged centos-entrypoint:latest
# run运行,发现我们的ls -a 命令生效
[root@iZ2ze261ew0wzy5zenjh2xZ dockerfile]# docker run a99fd74544ec
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
# 想要追加一个命令 -l 也就是ls -al
[root@iZ2ze261ew0wzy5zenjh2xZ dockerfile]# docker run a99fd74544ec -l
docker: Error response from daemon: OCI runtime create failed: container_linux.go:367: starting container process caused: exec: "-l": executable file not found in $PATH: unknown.
# cmd的清理下 -l 替换了CMD["ls","-a"]命令,-l不是命令,所以报错!
测试ENTRYPOINT
# 编写ENTRYPOINT
[root@iZ2ze261ew0wzy5zenjh2xZ dockerfile]# vim dockerfile-entrypoint
FROM centos
ENTRYPOINT ["ls","-a"]
# 构建镜像
[root@iZ2ze261ew0wzy5zenjh2xZ dockerfile]# docker build -f dockerfile-entrypoint -t centos-l .
Sending build context to Docker daemon 3.072kB
Step 1/2 : FROM centos
---> 300e315adb2f
Step 2/2 : ENTRYPOINT ["ls","-a"]
---> Running in f1b9ebba1240
Removing intermediate container f1b9ebba1240
---> 6bb9564028ed
Successfully built 6bb9564028ed
Successfully tagged centos-l:latest
# run运行,发现我们的ls -a 命令生效
# 想要追加一个命令 -l 也就是ls -al
[root@iZ2ze261ew0wzy5zenjh2xZ dockerfile]# docker run 6bb9564028ed -l
total 0
drwxr-xr-x 1 root root 6 Apr 3 05:56 .
drwxr-xr-x 1 root root 6 Apr 3 05:56 ..
-rwxr-xr-x 1 root root 0 Apr 3 05:56 .dockerenv
lrwxrwxrwx 1 root root 7 Nov 3 15:22 bin -> usr/bin
drwxr-xr-x 5 root root 340 Apr 3 05:56 dev
drwxr-xr-x 1 root root 66 Apr 3 05:56 etc
drwxr-xr-x 2 root root 6 Nov 3 15:22 home
lrwxrwxrwx 1 root root 7 Nov 3 15:22 lib -> usr/lib
lrwxrwxrwx 1 root root 9 Nov 3 15:22 lib64 -> usr/lib64
drwx------ 2 root root 6 Dec 4 17:37 lost+found
drwxr-xr-x 2 root root 6 Nov 3 15:22 media
drwxr-xr-x 2 root root 6 Nov 3 15:22 mnt
drwxr-xr-x 2 root root 6 Nov 3 15:22 opt
dr-xr-xr-x 124 root root 0 Apr 3 05:56 proc
dr-xr-x--- 2 root root 162 Dec 4 17:37 root
drwxr-xr-x 11 root root 163 Dec 4 17:37 run
lrwxrwxrwx 1 root root 8 Nov 3 15:22 sbin -> usr/sbin
drwxr-xr-x 2 root root 6 Nov 3 15:22 srv
dr-xr-xr-x 13 root root 0 Apr 3 05:56 sys
drwxrwxrwt 7 root root 145 Dec 4 17:37 tmp
drwxr-xr-x 12 root root 144 Dec 4 17:37 usr
drwxr-xr-x 20 root root 262 Dec 4 17:37 var
# 发现我们的追加命令,是直接拼接在我们的ENTRYPOINT 命令后面的!
实战:tomcat镜像
1、准备镜像文件 tomcat压缩包,jdk的压缩包!注意:要放在一个目录下
2、编写dockerfile文件,官方命名 Dockerfile
,build会自动寻找这个文件,就不需要-f指定了!
[root@iZ2ze261ew0wzy5zenjh2xZ home]# cat Dockerfile
FROM centos
MAINTAINER wuguofeng
COPY readme.txt /usr/local/readme.txt
ADD jdk-8u221-linux-x64.tar.gz /usr/local
ADD apache-tomcat-9.0.34.tar.gz /usr/local
ENV MYPATH /usr/local
WORKDIR $MYPATH
ENV JAVA_HOME /usr/local/jdk1.8.0_221
ENV CLASSPATH $JAVA_HOME/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.34
ENV CATALINA_BASH /usr/local/apache-tomcat-9.0.34
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
EXPOSE 8080
CMD /usr/local/apache-tomcat-9.0.34/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.34/logs/catalina.out
# 开始构建
[root@iZ2ze261ew0wzy5zenjh2xZ home]# docker build -t tomcatjiayou .
Sending build context to Docker daemon 413.4MB
Step 1/14 : FROM centos
---> 300e315adb2f
Step 2/14 : MAINTAINER wuguofeng
---> Using cache
---> d49d25fed779
Step 3/14 : COPY readme.txt /usr/local/readme.txt
---> Using cache
---> ae5cb51076a2
Step 4/14 : ADD jdk-8u221-linux-x64.tar.gz /usr/local
---> Using cache
---> af8fb1fb00e4
Step 5/14 : ADD apache-tomcat-9.0.34.tar.gz /usr/local
---> Using cache
---> 1fe61ee51ab8
Step 6/14 : ENV MYPATH /usr/local
---> Running in c46a6f652437
Removing intermediate container c46a6f652437
---> 0f8031dfe2f3
Step 7/14 : WORKDIR $MYPATH
---> Running in 93e66ec574cc
Removing intermediate container 93e66ec574cc
---> 7498459bedd9
Step 8/14 : ENV JAVA_HOME /usr/local/jdk1.8.0_221
---> Running in f71b8b05273c
Removing intermediate container f71b8b05273c
---> 2773439c6b10
Step 9/14 : ENV CLASSPATH $JAVA_HOME/dt.jar:$JAVA_HOME/lib/tools.jar
---> Running in ea4a3b60f8d9
Removing intermediate container ea4a3b60f8d9
---> 8cc4d635c0b9
Step 10/14 : ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.34
---> Running in cbca5e4c0d01
Removing intermediate container cbca5e4c0d01
---> b739796fc78c
Step 11/14 : ENV CATALINA_BASH /usr/local/apache-tomcat-9.0.34
---> Running in 6804a1289d14
Removing intermediate container 6804a1289d14
---> 0463052c7019
Step 12/14 : ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
---> Running in f8d00e4e44da
Removing intermediate container f8d00e4e44da
---> 6f1f5a55d7f3
Step 13/14 : EXPOSE 8080
---> Running in f2b220f241e4
Removing intermediate container f2b220f241e4
---> 12965b9ae89d
Step 14/14 : CMD /usr/local/apache-tomcat-9.0.34/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.34/logs/catalina.out
---> Running in be94f2c3a63c
Removing intermediate container be94f2c3a63c
---> fb2bc4e0a568
Successfully built fb2bc4e0a568
Successfully tagged tomcatjiayou:latest
# 查看一下构建的镜像
[root@iZ2ze261ew0wzy5zenjh2xZ home]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcatjiayou latest fb2bc4e0a568 15 seconds ago 632MB
tomcat01 latest e3787290ae16 21 minutes ago 690MB
centos-l latest 6bb9564028ed 3 hours ago 209MB
centos-entrypoint latest a99fd74544ec 3 hours ago 209MB
textcentos 0.1 9dfecdb872e1 4 hours ago 242MB
mysql 5.7 a70d36bc331a 2 months ago 449MB
nginx latest f6d0b4767a6c 2 months ago 133MB
centos latest 300e315adb2f 3 months ago 209MB
# 启动镜像
[root@iZ2ze261ew0wzy5zenjh2xZ home]# docker run -d -p 7070:8080 -v /home/wenjian:/usr/local/apache-tomcat-9.0.34/webapps/test -v /home/zhizhi:/usr/local/apache-tomcat-9.0.34/logs --name huanjing02 fb2bc4e0a568
d54392bd56180b80b8c86131372f2aeba056ac11b06e9319073aadfa550831d6
# 本地查看是否目录中已挂载
[root@iZ2ze261ew0wzy5zenjh2xZ home]# ls
apache-tomcat-9.0.34.tar.gz dockerfile Dockerfile jdk-8u221-linux-x64.tar.gz loglog mysql readme.txt rizhi webapps wenjian wu zhizhi
# 这样就可以直接把项目放在wenjian这个目录里,然后通过 地址:7070/test/项目名 来访问页面了
发布自己的镜像
# docker login -u账号
# 密码
# 登录上去,提交一定要有版本号
[root@iZ2ze261ew0wzy5zenjh2xZ home]# docker login -uchenhongguang
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-store
Login Succeeded
[root@iZ2ze261ew0wzy5zenjh2xZ home]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcatjiayou latest fb2bc4e0a568 38 minutes ago 632MB
tomcat01 latest e3787290ae16 59 minutes ago 690MB
centos-l latest 6bb9564028ed 3 hours ago 209MB
centos-entrypoint latest a99fd74544ec 3 hours ago 209MB
textcentos 0.1 9dfecdb872e1 5 hours ago 242MB
mysql 5.7 a70d36bc331a 2 months ago 449MB
nginx latest f6d0b4767a6c 2 months ago 133MB
centos latest 300e315adb2f 3 months ago 209MB
[root@iZ2ze261ew0wzy5zenjh2xZ home]# docker tag fb2bc4e0a568 chenhongguang/tomcat:0.1
[root@iZ2ze261ew0wzy5zenjh2xZ home]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
chenhongguang/tomcat 0.1 fb2bc4e0a568 40 minutes ago 632MB
tomcatjiayou latest fb2bc4e0a568 40 minutes ago 632MB
tomcat01 latest e3787290ae16 About an hour ago 690MB
centos-l latest 6bb9564028ed 3 hours ago 209MB
centos-entrypoint latest a99fd74544ec 3 hours ago 209MB
textcentos 0.1 9dfecdb872e1 5 hours ago 242MB
mysql 5.7 a70d36bc331a 2 months ago 449MB
nginx latest f6d0b4767a6c 2 months ago 133MB
centos latest 300e315adb2f 3 months ago 209MB
[root@iZ2ze261ew0wzy5zenjh2xZ home]# docker push chenhongguang/tomcat:0.1
The push refers to repository [docker.io/chenhongguang/tomcat]
254c39ac9769: Pushed
430320a63cd0: Pushed
444a5b764b49: Pushed
2653d992f4ef: Mounted from library/centos
0.1: digest: sha256:32bb8a539c3cf885f06ae729853e2025b7a93b90f20baef57750220476909bb9 size: 1161
自定义网络
容器互联
查看所有的docker网络
[root@iZ2ze261ew0wzy5zenjh2xZ home]# docker network ls
NETWORK ID NAME DRIVER SCOPE
4d0255a86b75 bridge bridge local
a62b564db56a host host local
642d353ee256 none null local
网络模式
bridge:桥接 docker(默认,自己创建也使用bridge模式)
none:不配置网络
host:和宿主机共享网络
container:容器网络连通(用的少!局限很大)
测试
# 我们直接启动的命令 --net bridge, 而这个就是我们的docker0
docker run -d -P --name tomcat01 tomcat
docker run -d -P --name tomcat01 --net bridge tomcat
#docker0特点,默认,域名不能访问,--link可以打通链接!
# 我们可以自己定义一个网络!
下面是重点
创建了一个网络,启动时就可以使用这个网络启动,就可以保证在一个指定的网段了
# docker run -d -P --name tomcat-net-01 --net mynet tomcat
# docker run -d -P --name tomcat-net-02 --net mynet tomcat
# 运行了两个容器,都指定了用之前创建的网段,再查看元数据
# docker network inspect mynet
也能ping名字了
我们自定义的网络docker都已经帮我们维护好了对应的关系,推荐我们平时这样使用网络!
好处:
redis:不同的集群使用不同的网络,保证集群时安全和健康的
mysql:不同的集群使用不同的网络,保证集群是安全和健康的
相互隔离,但是也是可以打通的
网络连通
容器跟容器是不能打通的,但是可以跟所在的网络打通,这样就都通了
建了一个tomcat01,一个tomcat02 --默认网络是docker0
tomcat-net-01,tomcat-net-02 --这两个用的自建的网络
所以两个网络不互通
现在通过打通 容器 tomcat01 和 网络mynet ,就能使tomcat01能ping通tomcat-net-01
# 测试打通tomcat01 - mynet
# docker network connect mynet tomcat01
这样就把tomcat01和mynet网络连通了
结论:假设要跨网络操作别人,就需要使用docker network connect 连通!
redis集群部署实战
# 创建网卡
docker network create redis --subnet 172.38.0.0/16
# 通过脚本创建六个redis配置
for port in $(seq 1 6); \
do \
mkdir -p /mydata/redis/node-${port}/conf
touch /mydata/redis/node-${port}/conf/redis.conf
cat << EOF >>/mydata/redis/node-${port}/conf/redis.conf
port 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.38.0.1${port}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
EOF
done
#通过脚本启动6个redis
for port in $(seq 1 6); \
do \
docker run -p 637${port}:6379 -p 1637${port}:16379 --name redis-${port} \
-v /mydata/redis/node-${port}/data:/data \
-v /mydata/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.1${port} redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf; \
done
#这个是单个启动的
docker run -p 6371:6379 -p 16371:16379 --name redis-1 \
-v /mydata/redis/node-1/data:/data \
-v /mydata/redis/node-1/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.11 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf;
# 看到了6个redis 然后进入一个
[root@iZ2ze261ew0wzy5zenjh2xZ conf]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b21b1b02dd7f redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 4 seconds ago Up 2 seconds 0.0.0.0:6376->6379/tcp, 0.0.0.0:16376->16379/tcp redis-6
1b856f095d96 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 4 seconds ago Up 3 seconds 0.0.0.0:6375->6379/tcp, 0.0.0.0:16375->16379/tcp redis-5
aac1ec4711fc redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 5 seconds ago Up 4 seconds 0.0.0.0:6374->6379/tcp, 0.0.0.0:16374->16379/tcp redis-4
c2c52b0f0c5d redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 6 seconds ago Up 5 seconds 0.0.0.0:6373->6379/tcp, 0.0.0.0:16373->16379/tcp redis-3
e6aa8c256205 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 7 seconds ago Up 6 seconds 0.0.0.0:6372->6379/tcp, 0.0.0.0:16372->16379/tcp redis-2
cf356f08e3ee redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 8 seconds ago Up 7 seconds 0.0.0.0:6371->6379/tcp, 0.0.0.0:16371->16379/tcp redis-1
[root@iZ2ze261ew0wzy5zenjh2xZ conf]# docker exec -it b21b1b02dd7f /bin/sh
/data # ls
appendonly.aof nodes.conf
#创建redis集群-6个(自动分配三主三从)
/data # redis-cli --cluster create 172.38.0.11:6379 172.38.0.12:6379 172.38.0.13:6379 172.38.0.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 172.38.0.15:6379 to 172.38.0.11:6379
Adding replica 172.38.0.16:6379 to 172.38.0.12:6379
Adding replica 172.38.0.14:6379 to 172.38.0.13:6379
M: 57d682bb1c6bfa4529b5c56ea8d0784a53612176 172.38.0.11:6379
slots:[0-5460] (5461 slots) master
M: ef57138e7cd079bf79135aeca5e8dd497f591a09 172.38.0.12:6379
slots:[5461-10922] (5462 slots) master
M: 8b891bf98ea8408409897dda4c1d8ae295d7ca1b 172.38.0.13:6379
slots:[10923-16383] (5461 slots) master
S: b4763404c28683e973f5844e86412b390874597f 172.38.0.14:6379
replicates 8b891bf98ea8408409897dda4c1d8ae295d7ca1b
S: b1d31a707e707bdd1a843829a7ff9b4cffe0cec4 172.38.0.15:6379
replicates 57d682bb1c6bfa4529b5c56ea8d0784a53612176
S: e43d8850ec1673aab2a1ceb1620b19b05c277876 172.38.0.16:6379
replicates ef57138e7cd079bf79135aeca5e8dd497f591a09
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
...
>>> Performing Cluster Check (using node 172.38.0.11:6379)
M: 57d682bb1c6bfa4529b5c56ea8d0784a53612176 172.38.0.11:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: b4763404c28683e973f5844e86412b390874597f 172.38.0.14:6379
slots: (0 slots) slave
replicates 8b891bf98ea8408409897dda4c1d8ae295d7ca1b
M: ef57138e7cd079bf79135aeca5e8dd497f591a09 172.38.0.12:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: e43d8850ec1673aab2a1ceb1620b19b05c277876 172.38.0.16:6379
slots: (0 slots) slave
replicates ef57138e7cd079bf79135aeca5e8dd497f591a09
M: 8b891bf98ea8408409897dda4c1d8ae295d7ca1b 172.38.0.13:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: b1d31a707e707bdd1a843829a7ff9b4cffe0cec4 172.38.0.15:6379
slots: (0 slots) slave
replicates 57d682bb1c6bfa4529b5c56ea8d0784a53612176
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
#启动redis -c 是启动集群选项
/data # redis-cli -c
127.0.0.1:6379> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3 --看到有三个
cluster_current_epoch:6
cluster_my_epoch:2
cluster_stats_messages_ping_sent:288
cluster_stats_messages_pong_sent:279
cluster_stats_messages_meet_sent:3
cluster_stats_messages_sent:570
cluster_stats_messages_ping_received:276
cluster_stats_messages_pong_received:291
cluster_stats_messages_meet_received:3
cluster_stats_messages_received:570
127.0.0.1:6379> cluster nodes --查看集群关系
b1d31a707e707bdd1a843829a7ff9b4cffe0cec4 172.38.0.15:6379@16379 slave 57d682bb1c6bfa4529b5c56ea8d0784a53612176 0 1617506962000 5 connected
8b891bf98ea8408409897dda4c1d8ae295d7ca1b 172.38.0.13:6379@16379 master - 0 1617506962388 3 connected 10923-16383
57d682bb1c6bfa4529b5c56ea8d0784a53612176 172.38.0.11:6379@16379 master - 0 1617506962589 1 connected 0-5460
e43d8850ec1673aab2a1ceb1620b19b05c277876 172.38.0.16:6379@16379 myself,slave ef57138e7cd079bf79135aeca5e8dd497f591a09 0 1617506961000 6 connected
ef57138e7cd079bf79135aeca5e8dd497f591a09 172.38.0.12:6379@16379 master - 0 1617506961000 2 connected 5461-10922
b4763404c28683e973f5844e86412b390874597f 172.38.0.14:6379@16379 slave 8b891bf98ea8408409897dda4c1d8ae295d7ca1b 0 1617506961387 4 connected
127.0.0.1:6379> set a b --看使用的哪个端口
-> Redirected to slot [15495] located at 172.38.0.13:6379
OK
172.38.0.13:6379> get a --这里已经把redis3容器停了
Error: Operation timed out
/data # redis-cli -c
127.0.0.1:6379> get a --重新登录验证,自动切换为14端口
-> Redirected to slot [15495] located at 172.38.0.14:6379
"b"
172.38.0.14:6379> cluster nodes --发现13fail了,14变成了master(主)
57d682bb1c6bfa4529b5c56ea8d0784a53612176 172.38.0.11:6379@16379 master - 0 1617507328000 1 connected 0-5460
ef57138e7cd079bf79135aeca5e8dd497f591a09 172.38.0.12:6379@16379 master - 0 1617507328629 2 connected 5461-10922
b1d31a707e707bdd1a843829a7ff9b4cffe0cec4 172.38.0.15:6379@16379 slave 57d682bb1c6bfa4529b5c56ea8d0784a53612176 0 1617507328629 5 connected
8b891bf98ea8408409897dda4c1d8ae295d7ca1b 172.38.0.13:6379@16379 master,fail - 1617507103428 1617507102523 3 connected
b4763404c28683e973f5844e86412b390874597f 172.38.0.14:6379@16379 myself,master - 0 1617507327000 7 connected 10923-16383
e43d8850ec1673aab2a1ceb1620b19b05c277876 172.38.0.16:6379@16379 slave ef57138e7cd079bf79135aeca5e8dd497f591a09 0 1617507328000 6 connected
172.38.0.14:6379>
springboot微服务打包docker镜像
1、构架springboot项目
2、打包应用
3、编写Dockerfile
4、构建镜像
5、发布运行