赞
踩
本套笔记是记录学习《狂神说java——docker》课程笔记,仅供学习使用。
docker学习笔记——入门https://blog.csdn.net/m0_37258694/article/details/107195591
docker的理念回顾
将应用和环境打包成一个镜像!
数据?如果数据都在容器中,那么我们删除容器,数据就会丢失!需求:数据可以持久化
Mysql,容器删了,衫裤跑了!:需求:Mysql数据可以存储到本地!
容器之间可以有一个数据共享的技术!Docker容器中产生的数据,同步到本地!
这就是卷技术!目录的挂载,将我们容器内的目录,挂载到Linux上面!
总结一句话:容器的持久化和同步操作!荣期间也是可以数据共享的!
- #命令
- docker run -it -v 主机目录:容器目录
-
- #测试
- [root@CentOS122 ubuntu]# docker run -it -v /home/ceshi:/home centos /bin/bash
-
- #启动起来的时候我们可以通过docker inspect 容器id 查看容器卷挂载情况
- [root@CentOS122 ceshi]# docker inspect 71727d6777f2
测试文件同步
再来测试!
1、停止容器
2、宿主机上修改文件
3、启动容器
4、容器内的数据依旧是同步的!
ps:删除容器,外部挂载目录依旧存在,不会被删除!
好处:我们以后修改只需要在本地修改即可,容器内会自动同步的!
思考:MySQL的数据卷持久化问题
- # 获取镜像
-
-
-
- #运行容器,需要做数据卷挂载!
- #安装启动mysql,需要配置密码的,这一点需要注意!
-
- #官方测试:
- docker run --name some-mysql -v /my/custom:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
-
- #启动我们的mysql
- -d 后台运行
- -p 端口映射
- -v 卷挂载
- -e 环境配置
- --name 容器名字
- [root@CentOS122 ceshi]# docker run -d -p 3306:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql
-
- #启动成功之后,我们在本地使用sqlyog来测试
- #sqlyog-连接到服务器
-
- #测试在本地创建一个数据库,查看我们映射路径是否ok!
- #匿名挂载
- -v 容器内路径
- [root@CentOS122 data]# docker run -d --name nginx01 -v /etc/nginx nginx
-
- #查看所有的volume的情况
- [root@CentOS122 data]# docker volume ls
- DRIVER VOLUME NAME
- local 0a72cab3a7ecd61ae86d666c4c3fc364d820613ab7705fd38f84636608a7d90c
- local 004e9827898f77d10b68198ebc3c3e153e63b81ef426b8bdf1afa6c494ff5844
- #这里发现,就是匿名挂载,我们在-v只写了容器内的路径,没有写容器外的路径
-
- #具名挂载
- [root@CentOS122 data]# docker run -d --name nginx01 -v juming-nginx:/etc/nginx nginx
-
- [root@CentOS122 data]# docker ps
- CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- 8b503354ae5b nginx "/docker-entrypoint.…" 3 seconds ago Up 2 seconds 80/tcp nginx01
-
- [root@CentOS122 data]# docker volume ls
- DRIVER VOLUME NAME
- local juming-nginx
-
- #通过-v 卷名:容器内路径
- #查看这个卷
- [root@CentOS122 data]# docker volume inspect 8b503354ae5b
-
所有的docker容器内的卷,没有指定目录的情况下都是在/var/lib/docker/volumes/xxx/_data
我们可以通过具名挂载可以方便的找到我们的一个卷,大多数情况下使用具名挂载!
- #如何确定具名挂载还是匿名挂载,试试指定路径挂载!
- -v 容器内路径 #匿名挂载
- -v 卷名:容器内路径 #具名挂载
- -v 、宿主机路径:容器内路径 #指定路径挂载
拓展:
- # 通过-v 容器内路径:ro rw 改变读写权限
- ro readonly #只读
- rw readwrite #可读可写
-
- #一旦这里设置了容器权限,容器对我们挂载出来的内容就有限制了!
- [root@CentOS122 data]# docker run -d --name nginx02 -v juming-nginx:/etc/nginx:ro nginx
- [root@CentOS122 data]# docker run -d --name nginx02 -v juming-nginx:/etc/nginx:rw nginx
-
- #ro 只要看到ro就说嘛这个路径只能通过宿主机来操作,容器内是无法操作的!
DockerFile就是用来构建docker镜像的构建稳健!命令脚本!先体验一下!
通过这个脚本可以生成镜像,镜像是一层一层的,脚本是一个一个的命令,每一个命令都是一层。
- #创建一个dockerfile文件,名字可以随机,建议dockerfile
- #文件内容
- FROM centos
- VOLUME ["volume01","volume02"]
-
- CMD echo "------end------"
- CMD /bin/bash
-
- #这里的命令,一句就是镜像的一层
- #启动自己写的容器
- [root@CentOS122 home]# docker run -it 1fc3c9d5d5d5 /bin/bash
这个卷和外部一定有一个同步的目录!
查看一下卷挂载路径:
[root@CentOS122 ubuntu]# docker inspect 7e208a8caaa0
测试一下刚才的文件是否同步出去了!
这种方式我们未来使用的很多,因为我们通常会构建自己的镜像!
假设构建镜像的时候没有过挂载卷,要手动景香瓜子 -v 卷名:容器内路径!
多个mysql同步数据!
#启动三个容器,通过我们刚才自己写的镜像启动
测试:可以删除docker01,查看一下docker02和docker03是否还可以访问这个文件?
测试的结果:依旧可以访问!
结论:
容器之间配置信息的传递,数据卷容器的生命周期一直延续到没有容器使用为止。
但是一旦你持久化到本地,这个时候,本地数据不会删除的!
dockerfile是用来构建docker镜像的文件!命令参数脚本!
构建步骤:
1、编写一个dockerfile文件
2、docker build构建成为一个镜像
3、docker run运行镜像
4、docker push 发布镜像(DockerHub、阿里云镜像仓库!)
很多官方镜像都是基础包,很多功能没有,我们通常会自己搭建自己的镜像!
官方既然可以制作镜像,那我们也可以!
基础知识:
1、每个保留关键字(指令)都必须是大写字母;
2、执行从上到下顺序执行
3、#表示注释
4、每一个指令都会创建提交一个新的镜像层,并提交!
dockerfile是没想开发的,我们以后要发布项目,做镜像,就需要编写dockerfile,这个文件很简单!
Docker镜像逐渐成为企业交付的标准,必须要掌握!
步骤:开发,部署,运维。。。缺一不可!
DockerFile:构建稳健,定义了一切的步骤,源代码;
DockerImages:通过DockerFile构件生成的镜像,最终发布和运行的产品!
Docker容器:容器就是镜像运行起来提供服务的服务器。
- FROM #基础畸形秀,一切从这里开始构建 centos
- MAINTAINTER #镜像是谁写的,姓名+邮箱
- RUN #镜像构建的时候需要运行的命令
- ADD #步骤:tomcat镜像,这个tomcat压缩包!添加内容
- WORKDIR #镜像的工作目录
- VOLUME #挂载的目录
- EXPOSE #暴露端口配置
- CMD #指定这个容器启动的时候要运行的命令,只有最后一次会生效,可被替换
- ENTERPOINT #指定这个容器启动的时候要运行的命令,可追加命令
- ONBUILD #当构建一个被继承DockerFile这个时候就会运行ONVUILD的指令。触发指令
- COPY #类似ADD,当我们文件拷贝到镜像中
- ENV #构建的时候设置环境变量
Docker Hub中%99的镜像都是从这个基础镜像过来的FROM scratch,然后配置需要的软件和配置进行的构建。
创建一个自己的centos
- # 1 、编写dockerfile的文件
- FROM centos
- MAINTAINER sj@string
-
- ENV MYPATH /usr/local
- WORKDIR $MYPATH
-
- RUN yum -y install vim
- RUN yum -ynstall net-tools
-
- EXPOSE 80
-
- CMD echo $MYPATH
- CMD echo "------end--------"
- CMD /bin/bash
-
- # 2、通过文件构建镜像
- docker build -f centos-dockerfile -t mycentos:1.0 .
-
-
- #测试运行
- docker run -it mycentos:1.0
对比:之前原生的centos
我们的镜像:
我么可以列出本地进行的变更历史:
我们平时拿到一个镜像,可以研究一下它是怎么做的?
CMD和ENTRYPOINT区别
- CMD #指定这个容器启动的时候要运行的命令,只有最后一次会生效,可被替换
- ENTERPOINT #指定这个容器启动的时候要运行的命令,可追加命令
测试cmd
- #编写dockerfile文件
- [root@CentOS122 home]# cat centos-cmd-dockerfile
- FROM centos
-
- CMD ["ls","-a"]
-
- #构建镜像
- [root@CentOS122 home]# docker build -f centos-cmd-dockerfile -t centos_cmd:1.0 .
- Sending build context to Docker daemon 391.4MB
- Step 1/2 : FROM centos
- ---> 831691599b88
- Step 2/2 : CMD ["ls","-a"]
- ---> [Warning] IPv4 forwarding is disabled. Networking will not work.
- ---> Running in a542487e9c78
- Removing intermediate container a542487e9c78
- ---> 09a93c904638
- Successfully built 09a93c904638
- Successfully tagged centos_cmd:1.0
-
- #运行,发现我们的ls -a命令生效了
- [root@CentOS122 home]# docker run centos_cmd
- Unable to find image 'centos_cmd:latest' locally
- docker: Error response from daemon: pull access denied for centos_cmd, repository does not exist or may require 'docker login': denied: requested access to the resource is denied.
- See 'docker run --help'.
- [root@CentOS122 home]# docker run centos_cmd:1.0
- WARNING: IPv4 forwarding is disabled. Networking will not work.
- .
- ..
- .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@CentOS122 home]# docker run centos_cmd:1.0 -l
- WARNING: IPv4 forwarding is disabled. Networking will not work.
- docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"-l\": executable file not found in $PATH": unknown.
- ERRO[0000] error waiting for container: context canceled
-
- #错误的原因
- #CMD情形下, -l替换CMD ["ls","-a"]命令,而-l 不是命令,所以报错
测试ENTERPOINT
- [root@CentOS122 home]# vim centos-entrypoint-dockerfile
- [root@CentOS122 home]# docker build -f centos-entrypoint-dockerfile -t centos_entrypoint:1.0
- "docker build" requires exactly 1 argument.
- See 'docker build --help'.
-
- Usage: docker build [OPTIONS] PATH | URL | -
-
- Build an image from a Dockerfile
- [root@CentOS122 home]# docker build -f centos-entrypoint-dockerfile -t centos_entrypoint:1.0 .
- Sending build context to Docker daemon 391.4MB
- Step 1/2 : FROM centos
- ---> 831691599b88
- Step 2/2 : ENTRYPOINT ["ls","-a"]
- ---> [Warning] IPv4 forwarding is disabled. Networking will not work.
- ---> Running in 445c47bb706a
- Removing intermediate container 445c47bb706a
- ---> 2cbb97c27ac8
- Successfully built 2cbb97c27ac8
- Successfully tagged centos_entrypoint:1.0
- [root@CentOS122 home]# docker run centos_entrypoint:1.0
- WARNING: IPv4 forwarding is disabled. Networking will not work.
- .
- ..
- .dockerenv
- bin
- dev
- etc
- home
- lib
- lib64
- lost+found
- media
- mnt
- opt
- proc
- root
- run
- sbin
- srv
- sys
- tmp
- usr
- var
- [root@CentOS122 home]# docker run centos_entrypoint:1.0 -l
- WARNING: IPv4 forwarding is disabled. Networking will not work.
- total 0
- drwxr-xr-x. 1 root root 6 Jul 12 05:08 .
- drwxr-xr-x. 1 root root 6 Jul 12 05:08 ..
- -rwxr-xr-x. 1 root root 0 Jul 12 05:08 .dockerenv
- lrwxrwxrwx. 1 root root 7 May 11 2019 bin -> usr/bin
- drwxr-xr-x. 5 root root 340 Jul 12 05:08 dev
- drwxr-xr-x. 1 root root 66 Jul 12 05:08 etc
- drwxr-xr-x. 2 root root 6 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 6 Jun 11 02:35 lost+found
- drwxr-xr-x. 2 root root 6 May 11 2019 media
- drwxr-xr-x. 2 root root 6 May 11 2019 mnt
- drwxr-xr-x. 2 root root 6 May 11 2019 opt
- dr-xr-xr-x. 234 root root 0 Jul 12 05:08 proc
- dr-xr-x---. 2 root root 162 Jun 11 02:35 root
- drwxr-xr-x. 11 root root 163 Jun 11 02:35 run
- lrwxrwxrwx. 1 root root 8 May 11 2019 sbin -> usr/sbin
- drwxr-xr-x. 2 root root 6 May 11 2019 srv
- dr-xr-xr-x. 13 root root 0 Jun 30 21:38 sys
- drwxrwxrwt. 7 root root 145 Jun 11 02:35 tmp
- drwxr-xr-x. 12 root root 144 Jun 11 02:35 usr
- drwxr-xr-x. 20 root root 262 Jun 11 02:35 var
Dockerfile中很多命令都是十分的相似,我们需要了解他们的区别,我们最好的学习就是对比他们然后测试效果!
1、准备镜像文件toncat压缩包,jdk压缩包!
2、编写dockerfile文件,官方明明dockerfile,build会自动
- FROM centos #
- MAINTAINER cheng<1204598429@qq.com>
- COPY README /usr/local/README #复制文件
- ADD jdk-8u231-linux-x64.tar.gz /usr/local/ #复制解压
- ADD apache-tomcat-9.0.35.tar.gz /usr/local/ #复制解压
- RUN yum -y install vim
- ENV MYPATH /usr/local #设置环境变量
- WORKDIR $MYPATH #设置工作目录
- ENV JAVA_HOME /usr/local/jdk1.8.0_231 #设置环境变量
- ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.35 #设置环境变量
- ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib #设置环境变量 分隔符是:
- EXPOSE 8080 #设置暴露的端口
- CMD /usr/local/apache-tomcat-9.0.35/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.35/logs/catalina.out # 设置默认命令
3、构建镜像
- # 因为dockerfile命名使用默认命名 因此不用使用-f 指定文件
- $ docker build -t mytomcat:0.1 .
4、run镜像
$ docker run -d -p 8080:8080 --name tomcat01 -v /home/kuangshen/build/tomcat/test:/usr/local/apache-tomcat-9.0.35/webapps/test -v /home/kuangshen/build/tomcat/tomcatlogs/:/usr/local/apache-tomcat-9.0.35/logs mytomcat:0.1
5、范文测试
6、发布项目
由于做了卷挂载,我们直接在本地编写项目就可以发布了!
发现:项目部署成功,可以直接访问。
我们以后的开发步骤:需要掌握Dockerfile的编写!我们之后的一切都是使用docker镜像来发布运行的!
Dockerhub
2、确定账户可以登录
3、登录
- $ docker login --help
- Usage: docker login [OPTIONS] [SERVER]
-
- Log in to a Docker registry.
- If no server is specified, the default is defined by the daemon.
-
- Options:
- -p, --password string Password
- --password-stdin Take the password from stdin
- -u, --username string Username
4、提交镜像
- [root@CentOS122 tomcattest]# docker login -u dockerlearnsj
- 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@CentOS122 tomcattest]# docker push tomcat02
- The push refers to repository [docker.io/library/tomcat02]
- b475618bf53f: Preparing
- 361fca1be245: Preparing
- 8caf7fc74a11: Preparing
- a1c4399f9b22: Preparing
- 4f866e977815: Preparing
- f73b2345c404: Waiting
- f5181c7ef902: Waiting
- 2e5b4ca91984: Waiting
- 527ade4639e0: Waiting
- c2c789d2d3c5: Waiting
- 8803ef42039d: Waiting
- denied: requested access to the resource is denied
- # 会发现push不上去,因为如果没有前缀的话默认是push到 官方的library
- # 解决方法
- # 第一种 build的时候添加你的dockerhub用户名,然后在push就可以放到自己的仓库了
- $ docker build -t chengcoder/mytomcat:0.1 .
- # 第二种 使用docker tag #然后再次push
- $ docker tag 容器id chengcoder/mytomcat:1.0 #然后再次push
上传到阿里云镜像服务器上
看官网 很详细https://cr.console.aliyun.com/repository/
- $ sudo docker login --username=zchengx registry.cn-shenzhen.aliyuncs.com
- $ sudo docker tag [ImageId] registry.cn-shenzhen.aliyuncs.com/dsadxzc/cheng:[镜像版本号]
- # 修改id 和 版本
- sudo docker tag a5ef1f32aaae registry.cn-shenzhen.aliyuncs.com/dsadxzc/cheng:1.0
- # 修改版本
- $ sudo docker push registry.cn-shenzhen.aliyuncs.com/dsadxzc/cheng:[镜像版本号]
ps:
- docker save 命令 :将镜像打包成一个tar压缩包,可以送给别人
-
- docker load 命令:解压tar包成镜像
情况所有网络
- #情况所有容器
- [root@CentOS122 tomcattest]# docker rm -f $(docker ps -aq)
-
- #删除所有镜像
- [root@CentOS122 tomcattest]# docker rmi -f $(docker images -aq)
测试
三个网路
问题:docker日如何处理容器内网路访问?
- #测试运行一个tomcat
- [root@CentOS122 ubuntu]# docker run -d --name tomcat01 tomcat
- WARNING: IPv4 forwarding is disabled. Networking will not work.
- ad894daf4d6be03a30e370022ee936a8f8c9e76a84e9e899c6f2ee729418acda
- [root@CentOS122 ubuntu]# docker ps
- CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- ad894daf4d6b tomcat "catalina.sh run" 3 seconds ago Up 2 seconds 8080/tcp tomcat01
-
- #查看宿主机网络
- [root@CentOS122 ubuntu]# ip addr
- 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
- link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
- inet 127.0.0.1/8 scope host lo
- valid_lft forever preferred_lft forever
- inet6 ::1/128 scope host
- valid_lft forever preferred_lft forever
- 2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
- link/ether 00:50:56:29:0e:06 brd ff:ff:ff:ff:ff:ff
- inet 192.168.31.121/24 brd 192.168.31.255 scope global noprefixroute ens32
- valid_lft forever preferred_lft forever
- inet6 fe80::a91:c749:1e4:c19a/64 scope link noprefixroute
- valid_lft forever preferred_lft forever
- 3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
- link/ether 52:54:00:8c:75:5c brd ff:ff:ff:ff:ff:ff
- inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
- valid_lft forever preferred_lft forever
- 4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
- link/ether 52:54:00:8c:75:5c brd ff:ff:ff:ff:ff:ff
- 5: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
- link/ether 02:42:15:3f:39:04 brd ff:ff:ff:ff:ff:ff
- inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
- valid_lft forever preferred_lft forever
- inet6 fe80::42:15ff:fe3f:3904/64 scope link
- valid_lft forever preferred_lft forever
- 9: veth85cd340@if8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
- link/ether ee:1c:4e:71:63:d7 brd ff:ff:ff:ff:ff:ff link-netnsid 0
- inet6 fe80::ec1c:4eff:fe71:63d7/64 scope link
- valid_lft forever preferred_lft forever
-
- #查看容器网络
- [root@CentOS122 ubuntu]# docker exec -it ad894daf4d6b /bin/bash
- root@ad894daf4d6b:/usr/local/tomcat# ip addr
- 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
- link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
- inet 127.0.0.1/8 scope host lo
- valid_lft forever preferred_lft forever
- #查看到容器内部网络地址 发现容器起送的时候会得到一个eth0@if9 ip地址,docker服务器分配得!
- 8: eth0@if9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
- link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
- inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
- valid_lft forever preferred_lft forever
-
- # 思考: linux能不能ping通容器内网路?可以的!
- # 容器内可以ping通宿主机吗?可以的!
- #容器内ping宿主机网络
- root@ad894daf4d6b:/usr/local/tomcat# ping 192.168.31.121
- PING 192.168.31.121 (192.168.31.121) 56(84) bytes of data.
- 64 bytes from 192.168.31.121: icmp_seq=1 ttl=64 time=0.100 ms
- 64 bytes from 192.168.31.121: icmp_seq=2 ttl=64 time=0.064 ms
- 64 bytes from 192.168.31.121: icmp_seq=3 ttl=64 time=0.063 ms
- 64 bytes from 192.168.31.121: icmp_seq=4 ttl=64 time=0.074 ms
- 64 bytes from 192.168.31.121: icmp_seq=5 ttl=64 time=0.097 ms
- ^C
- --- 192.168.31.121 ping statistics ---
- 5 packets transmitted, 5 received, 0% packet loss, time 4ms
- rtt min/avg/max/mdev = 0.063/0.079/0.100/0.018 ms
- root@ad894daf4d6b:/usr/local/tomcat# exit
- exit
- #宿主机ping容器网络
- [root@CentOS122 ubuntu]# ping 172.17.02
- PING 172.17.02 (172.17.0.2) 56(84) bytes of data.
- 64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.105 ms
- 64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.056 ms
- ^C
- --- 172.17.02 ping statistics ---
- 2 packets transmitted, 2 received, 0% packet loss, time 1000ms
- rtt min/avg/max/mdev = 0.056/0.080/0.105/0.026 ms
-
原理:
1、我们没启动一个docker容器,docker就会给docker容器分配一个ip,我们只要安装了docker,就会有一个docker0桥接模式,使用的技术就是veth-pair技术!
再次测试ip addr
2、再启动一个容器测试,发现又多一对网络
[root@CentOS122 ubuntu]# docker run -d --name tomcat02 tomcat
- # 我们发现这个容器带来的网卡都是一对一对的;
- # veth-pair就是一对的虚拟设备接口,它们都是成对出现的,一端连着协议,一端彼此相连;
- # 正因为又这个特性,veth-pair充当一个桥梁,链接各种虚拟网络设备的;
- # OpenStack,docker容器之间的链接,OVS的链接都是使用veth-pair技术
3、我们来测试一下tomcat01和tomcat02是否可以ping同
- [root@CentOS122 ubuntu]# docker exec -it tomcat01 ip addr
- 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
- link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
- inet 127.0.0.1/8 scope host lo
- valid_lft forever preferred_lft forever
- 8: eth0@if9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
- link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
- inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
- valid_lft forever preferred_lft forever
- [root@CentOS122 ubuntu]# docker exec -it tomcat02 ping 172.17.0.2
- PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
- 64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.088 ms
- 64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.058 ms
- 64 bytes from 172.17.0.2: icmp_seq=3 ttl=64 time=0.060 ms
- ^C
- --- 172.17.0.2 ping statistics ---
- 3 packets transmitted, 3 received, 0% packet loss, time 1000ms
- rtt min/avg/max/mdev = 0.058/0.068/0.088/0.016 ms
结论:
tomcat01和tomcat02共用一个路由器,docker0。
所有容器在不指定网络的情况下,都是docker0路由的,docker会给我们容器分配一个默认的可用ip。
小结:
docker使用的事linux的桥接,宿主机是一个docker容器的网桥docker0.
docker中所有的网络接口都是虚拟的,虚拟的转发效率高(类比内网传递文件)
只要删除容器,对应的网桥一对就没了!
思考一个场景:我们编写了一个微服务,database url=ip: 项目不重启,数据ip换了,我们希望可以处理这个问题,可以通过名字来进行访问容器?
思考一个场景:
我们编写一个微服务,docker-url=ip;项目不重启,数据库ip换掉了,我们希望可以处理这个问题,可以使用名称来进行访问容器?
- #直接使用名称
- [root@CentOS122 ubuntu]# docker exec -it tomcat02 ping tomcat01
- ping: tomcat01: Temporary failure in name resolution
- #运行一个tomcat03 --link tomcat02
- [root@CentOS122 ubuntu]# docker run -d -P --name tomcat03 --link tomcat02 tomcat
- WARNING: IPv4 forwarding is disabled. Networking will not work.
- 7cfa664b4b2ca259e5e000bd5663427c55beb1e41b13fbb8a0a2d1fd0980ff14
- #用tomcat03 ping tomcat02 可以ping通
- [root@CentOS122 ubuntu]# docker exec -it tomcat03 ping tomcat02
- PING tomcat02 (172.17.0.3) 56(84) bytes of data.
- 64 bytes from tomcat02 (172.17.0.3): icmp_seq=1 ttl=64 time=0.088 ms
- 64 bytes from tomcat02 (172.17.0.3): icmp_seq=2 ttl=64 time=0.059 ms
- ^C
- --- tomcat02 ping statistics ---
- 2 packets transmitted, 2 received, 0% packet loss, time 999ms
- rtt min/avg/max/mdev = 0.059/0.073/0.088/0.016 ms
- #反过来用tomcat02 ping tomcat03 ping不通
- [root@CentOS122 ubuntu]# docker exec -it tomcat02 ping tomcat03
- ping: tomcat03: Temporary failure in name resolution
探究:
docker network inspect 网络id 网段相同
- #查看docker网络
- [root@CentOS122 ubuntu]# docker network ls
- NETWORK ID NAME DRIVER SCOPE
- 16a626150777 bridge bridge local
- 68f5a8898e3d host host local
- 9f153659223a none null local
-
- #查看该网段
- [root@CentOS122 ubuntu]# docker network inspect 16a626150777
- [
- {
- "Name": "bridge",
- "Id": "16a6261507776953aedfcfd61a7ba057222d9fd16723fa8130971c2c285c4895",
- "Created": "2020-06-28T21:20:39.606370089+08:00",
- "Scope": "local",
- "Driver": "bridge",
- "EnableIPv6": false,
- "IPAM": {
- "Driver": "default",
- "Options": null,
- "Config": [
- {
- "Subnet": "172.17.0.0/16"
- }
- ]
- },
- "Internal": false,
- "Attachable": false,
- "Ingress": false,
- "ConfigFrom": {
- "Network": ""
- },
- "ConfigOnly": false,
- "Containers": {
- "26559243668d2c32855876a44f2180949decebd00eb8923106bc815325baecff": {
- "Name": "tomcat02",
- "EndpointID": "38159301d629bad1f45ed6f8564fdc625d69d7b67b454259b96d820ab206ef1a",
- "MacAddress": "02:42:ac:11:00:03",
- "IPv4Address": "172.17.0.3/16",
- "IPv6Address": ""
- },
- "7cfa664b4b2ca259e5e000bd5663427c55beb1e41b13fbb8a0a2d1fd0980ff14": {
- "Name": "tomcat03",
- "EndpointID": "9bf0c29be3a7754b8cf85c620f74fbdcc15011124b1d5a7766148ca91936be12",
- "MacAddress": "02:42:ac:11:00:04",
- "IPv4Address": "172.17.0.4/16",
- "IPv6Address": ""
- },
- "ad894daf4d6be03a30e370022ee936a8f8c9e76a84e9e899c6f2ee729418acda": {
- "Name": "tomcat01",
- "EndpointID": "8292f1534d07f54cdfa307ee1d2919e87dc51819e8ff6e5b15e36754e6d9acf2",
- "MacAddress": "02:42:ac:11:00:02",
- "IPv4Address": "172.17.0.2/16",
- "IPv6Address": ""
- }
- },
- "Options": {
- "com.docker.network.bridge.default_bridge": "true",
- "com.docker.network.bridge.enable_icc": "true",
- "com.docker.network.bridge.enable_ip_masquerade": "true",
- "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
- "com.docker.network.bridge.name": "docker0",
- "com.docker.network.driver.mtu": "1500"
- },
- "Labels": {}
- }
- ]
[root@CentOS122 ubuntu]# docker inspect 7cfa664b4b2c
查看tomcat03里面的/etc/hosts发现又tomcat02的配置:
- [root@CentOS122 ubuntu]# docker exec -it tomcat03 cat /etc/hosts
- 127.0.0.1 localhost
- ::1 localhost ip6-localhost ip6-loopback
- fe00::0 ip6-localnet
- ff00::0 ip6-mcastprefix
- ff02::1 ip6-allnodes
- ff02::2 ip6-allrouters
- 172.17.0.3 tomcat02 26559243668d
- 172.17.0.4 7cfa664b4b2c
--link本质就是在hosts配置中添加映射!
现在使用docker已经不建议使用--link了。
自定义网络,不适用docker0!
docker0问题:不支持容器名链接访问!
查看所有的docker网络
- #查看所有的docker网络
- [root@CentOS122 ubuntu]# docker network ls
- NETWORK ID NAME DRIVER SCOPE
- 16a626150777 bridge bridge local
- 68f5a8898e3d host host local
- 9f153659223a none null local
网络模式:
briage:桥接模式docker(默认,自己创建也是用bridge模式)
none:不配置网络,一般不用
host:和所有主机共享网络
container:容器网络连通(用得少!局限性大)
测试
- #我们直接使用启动命令 --net bridge,而这个就是我们的docker0
- #bridge就是docker0
- [root@CentOS122 ubuntu]# docker run -d -P --name tomcat01 tomcat
- #===》等价于docker run -d -P --name tomcat01 --net bridge tomcat
-
问题:docker0的特点默认域名不能访问。--link可以打通连接,但是很麻烦!
我们可以自定义网络:
- [root@CentOS122 ubuntu]# docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet
- 543a2a281ee2eb5b12c4180a8b6a09b25f15602361ec3943202d28891d883335
-
- [root@CentOS122 ubuntu]# docker network ls
- NETWORK ID NAME DRIVER SCOPE
- 16a626150777 bridge bridge local
- 68f5a8898e3d host host local
- 543a2a281ee2 mynet bridge local
- 9f153659223a none null local
docker network inspect mynet
启动两个tomcat,再次查看网络情况:
- [root@CentOS122 ubuntu]# docker run -d -P --name tomcat-net-01 --net mynet tomcat
- WARNING: IPv4 forwarding is disabled. Networking will not work.
- 6e1dd326db4c97cf3750d31a8addc9b40f65e7f0d34879b9ee616608323ae7c2
- [root@CentOS122 ubuntu]# docker run -d -P --name tomcat-net-02 --net mynet tomcat
- WARNING: IPv4 forwarding is disabled. Networking will not work.
- 9b6e4ec6ddf6da5ad2e0ae1ab45707d41d877641b364595a15b376f439867857
在自定义网络下,服务可以互相ping通,不用使用--link
- [root@CentOS122 ubuntu]# docker exec -it tomcat-net-01 ping tomcat-net-02
- PING tomcat-net-02 (192.168.0.3) 56(84) bytes of data.
- 64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=1 ttl=64 time=0.087 ms
- 64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=2 ttl=64 time=0.061 ms
- ^C
- --- tomcat-net-02 ping statistics ---
- 2 packets transmitted, 2 received, 0% packet loss, time 1000ms
- rtt min/avg/max/mdev = 0.061/0.074/0.087/0.013 ms
- [root@CentOS122 ubuntu]# docker exec -it tomcat-net-02 ping tomcat-net-01
- PING tomcat-net-01 (192.168.0.2) 56(84) bytes of data.
- 64 bytes from tomcat-net-01.mynet (192.168.0.2): icmp_seq=1 ttl=64 time=0.068 ms
- 64 bytes from tomcat-net-01.mynet (192.168.0.2): icmp_seq=2 ttl=64 time=0.105 ms
- ^C
- --- tomcat-net-01 ping statistics ---
- 2 packets transmitted, 2 received, 0% packet loss, time 2ms
- rtt min/avg/max/mdev = 0.068/0.086/0.105/0.020 ms
我们自定义网络docker当我们维护好了对应的关系,推荐我们平时这样使用网络!
好处:
redis -不同的集群 使用不同的网络,保证集群式安全的和健康的!
mysql-不同的集群使用不同的网络,保证集群式安全的和健康的!
- [root@CentOS122 ubuntu]# docker network --help
-
- Usage: docker network COMMAND
-
- Manage networks
-
- Commands:
- connect Connect a container to a network #链接一个容器到一个网络
- create Create a network
- disconnect Disconnect a container from a network
- inspect Display detailed information on one or more networks
- ls List networks
- prune Remove all unused networks
- rm Remove one or more networks
-
- Run 'docker network COMMAND --help' for more information on a command.
#查看命令
- [root@CentOS122 ubuntu]# docker network connect --help
-
- Usage: docker network connect [OPTIONS] NETWORK CONTAINER
-
- Connect a container to a network
-
- Options:
- --alias strings Add network-scoped alias for the container
- --driver-opt strings driver options for the network
- --ip string IPv4 address (e.g., 172.30.100.104)
- --ip6 string IPv6 address (e.g., 2001:db8::33)
- --link list Add link to another container
- --link-local-ip strings Add a link-local address for the
- #测试两个不同的网络连通,在启动两个tomcat 使用默认网络,即docker0
- [root@CentOS122 ubuntu]# docker run -d -P --name tomcat01 tomcat
- WARNING: IPv4 forwarding is disabled. Networking will not work.
- 7e38ecf0a2223deacb754b33725993666f780a4f70be02bcfda3786b5c95c126
- [root@CentOS122 ubuntu]# docker run -d -P --name tomcat02 tomcat
- WARNING: IPv4 forwarding is disabled. Networking will not work.
- 0a39827e064ed8b2a56bd4c42ad91e55ddc63ac5b466cc0b01ca28ec9c5e50c7
-
- #测试两个网络之间的链接,发现不通
- [root@CentOS122 ubuntu]# docker exec tomcat-net-01 ping tomcat01
- ping: tomcat01: Temporary failure in name resolution
这是我们需要将tomcat01容器的ip加入到另一个网络:是的容器tomcat01与该网络能够连通,
- # 将tomcat01 连通tomcat-net-01 ,连通就是通过将tomcat01加入到mynet网络
- # 也就是一个容器两个ip
- [root@CentOS122 ubuntu]# docker network connect mynet tomcat01
- [root@CentOS122 ubuntu]# docker exec -it tomcat01 ping tomcat-net-01
- PING tomcat-net-01 (192.168.0.2) 56(84) bytes of data.
- 64 bytes from tomcat-net-01.mynet (192.168.0.2): icmp_seq=1 ttl=64 time=0.107 ms
- 64 bytes from tomcat-net-01.mynet (192.168.0.2): icmp_seq=2 ttl=64 time=0.061 ms
- ^C
- --- tomcat-net-01 ping statistics ---
- 2 packets transmitted, 2 received, 0% packet loss, time 2ms
- rtt min/avg/max/mdev = 0.061/0.084/0.107/0.023 ms
- [root@CentOS122 ubuntu]# docker exec -it tomcat-net-01 ping tomcat01
- PING tomcat01 (192.168.0.4) 56(84) bytes of data.
- 64 bytes from tomcat01.mynet (192.168.0.4): icmp_seq=1 ttl=64 time=0.047 ms
- 64 bytes from tomcat01.mynet (192.168.0.4): icmp_seq=2 ttl=64 time=0.061 ms
- 64 bytes from tomcat01.mynet (192.168.0.4): icmp_seq=3 ttl=64 time=0.068 ms
- ^C
- --- tomcat01 ping statistics ---
- 3 packets transmitted, 3 received, 0% packet loss, time 3ms
- rtt min/avg/max/mdev = 0.047/0.058/0.068/0.012 ms
发现:
# 01连通 ,加入后此时,已经可以tomcat01 和 tomcat-01-net ping通了
# 02是依旧不通的
结论:假设要哭啊网络操作别人,就需要使用dockernetwork connect连通!
- # 创建网卡
- 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
-
- # 通过脚本运行六个redis
- for port in $(seq 1 6);\
- docker run -p 637${port}:6379 -p 1667${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
-
- #运行redis
- docker exec -it redis-1 /bin/sh #redis默认没有bash
- redis-cli --cluster create 172.38.0.11:6379 172.38.0.12:6379 172.38.0.13:6379 172.38.0.14:6379 172.38.0.15:6379 172.38.0.16:6379 --cluster-replicas 1
-
-
-
1、构建SpringBoot项目
2、打包运行
mvn package
3、编写dockerfile
- FROM java:8
- COPY *.jar /app.jar
- CMD ["--server.port=8080"]
- EXPOSE 8080
- ENTRYPOINT ["java","-jar","app.jar"]
4、构建镜像
- # 1.复制jar和DockerFIle到服务器
- # 2.构建镜像
- $ docker build -t xxxxx:xx .
5、发布运行
以后我们使用了Docker之后,给别人交付就是一个镜像即可!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。