Docker快速入门进阶

曾经终败给现在 2023-09-29 13:52 116阅读 0赞

Docker入门进阶

学习资料:

【狂神说Java】Docker最新超详细版教程通俗易懂_哔哩哔哩_bilibili

文章目录

  • Docker入门进阶
    • 1、容器数据卷
      • 1.1、什么是容器数据卷
      • 1.2、使用数据卷
      • 1.3、安装Mysql
      • 1.4、具名挂载和匿名挂载
        • 1.4.1、匿名挂载
        • 1.4.2、具名挂载
    • 2、初始Dockerfile
      • 2.1、创建docker的测试目录
      • 2.2、创建dockerfile1脚本文件,通过这个脚本可以生成镜像,
      • 2.3、执行Dockerfile脚本文件,构建镜像
      • 2.4、启动镜像,查看容器
    • 3、数据卷容器
      • 3.1、启动父容器docker01
      • 3.2、启动容器docker02并挂载到父容器docker01的共享卷,命令`—volumes-from 容器列表`
      • 3.3、小结
      • 3.4、案例:Mysql容器实现数据同步
    • 4、Dockerfile(重点)
      • 4.1、Dockerfile介绍
      • 4.2、DockerFile构建过程
      • 4.3、Docker的指令
      • 4.4、Dockerfile实战
        • 4.4.1、创建一个自己的centos镜像
          • 4.4.1.1、创建工作目录
          • 4.4.1.2、编写Dockerfile文件 (文件名:mydockerfile-centos)
          • 4.4.1.3、通过这个文件构建镜像
          • 4.4.1.4、运行测试
          • 4.4.1.5、启动镜像
      • 4.4、CMD 和 ENTRYPOINT区别
        • 4.4.1、测试cmd
          • 4.4.1.1、编写dockerfile文件
          • 4.4.1.2、 构建镜像
          • 4.4.1.3、运行镜像
        • 4.4.2、测试ENTRYPOINT
          • 4.4.2.1、编写dockerfile文件
          • 4.4.2.2、构建镜像
          • 4.4.2.3、执行
          • 4.4.2.4、追加一个命令 -l
      • 4.5、Tomcat镜像
        • 4.5.1、准备镜像文件(tomcat压缩包 和 jdk压缩包 )
        • 4.5.2、编写dokerfile文件,官方命名`Dockerfile`,build 会自动寻找,就不用-f 指定了
        • 4.5.3、构造镜像
        • 4.5.4、运行镜像
        • 4.5.5、访问测试
        • 4.5.6、发布项目(由于做了卷挂载,我们直接在本地编写项目就可以发布了!)
      • 4.6、发布My镜像到Docker Hub
        • 4.6.1、[地址](https://hub.docker.com/) 注册自己的账号!
        • 4.6.2、确定这个账号可以登录
        • 4.6.3、登录完毕就可以在提交镜像了,就是一步docker pull
      • 4.7、发布到阿里云镜像服务上
        • 4.7.1、登录阿里云
        • 4.7.2、找到容器镜像服务
        • 4.7.3、创建命名空间
        • 4.7.4、创建容器镜像
      • 总结
    • 5、Docker网络
      • 5.1、理解Docker0
      • 5.2、 —link
      • 5.3、自定义网络
      • 5.4、网络连通
      • 5.5、部署Redis集群
    • 6、SpringBoot微服务打包Docker镜像

1、容器数据卷

1.1、什么是容器数据卷

docker的理念回顾

将应用和环境打包成一个镜像

数据?如果数据都在容器中,那么我们容器删除,数据就会丢失!需求:数据可以持久化

MySQL,容器删除了,删库跑路!需求:MySQL数据可以存储在本地!

容器之间可以有一个数据共享的技术!Docker容器中产生的数据,同步到本地!

这就是卷技术!目录的挂载,将我们容器内的目录,挂载到Linux上面!

img

使用卷技术的原因:容器的持久化和同步操作!容器间也是可以数据共享的!

1.2、使用数据卷

挂载后 主机目录和容器目录共享一个目录资源,容器目录也可以理解为是主机目录的快捷方式

方式一 :直接使用命令挂载 -v

  1. # -v, --volume list 绑定挂载卷
  2. docker run -it -v 主机目录:容器目录
  3. # 通过 查看挂载
  4. docker inspect 容器id

1、挂载并进入命令行/bin/bash

  1. docker run -it -v /home/ceshi:/home centos /bin/bash
  2. [root@Agustletmen ~]# docker run -it -v /home/ceshi:/home centos /bin/bash
  3. [root@Agustletmen home]# ls
  4. ceshi kk.java test test.java www

2、查看挂载

  1. docker inspect 【容器id

img

测试文件的同步(在主机上改动,观察容器变化)

容器

img

主机

img

也可以从主机同步到容器

是一个双向绑定的过程

好处:我们以后修改容器配置只需要在本地修改即可,容器内会自动同步。

1.3、安装Mysql

1、获取mysql镜像

  1. [root@Agustletmen /]# docker pull mysql:5.7

2、运行容器,挂载倆个数据目录,配置密码-e MYSQL_ROOT_PASSWORD=password

  1. # 启动
  2. # -d 后台运行
  3. # -p 端口映射
  4. # -v 数据卷挂载
  5. # -e 环境配置
  6. # --name 容器名字
  7. [root@Agustletmen /]# docker run -d -p 9200:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7
  8. # 启动成功之后,我们在本地使用sql可视化工具来测试是否运行正常
  9. # 连接到服务器的9200--和容器内的3306映射
  10. ------------------------------------------------------------------------------
  11. # 注意,这里的-v 端口映射可以映射原来的3306
  12. [root@Agustletmen /]# 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

3、测试连接,在windows上用数据库连接工具测试连接mysql容器·

img

4、文件目录映射成功

img

5、linux中,一个数据库对应一个目录,一个数据表对应一个文件

img

成功看到test目录

img

1.4、具名挂载和匿名挂载

1.4.1、匿名挂载

1、匿名挂载就是不指定主机目录进行挂载(没有给卷(主机目录)起名就是匿名)

  1. # -v 容器内路径!
  2. docker run -d -P --name nginx01 -v /etc/nginx nginx
  3. ----------------------------------------------------------------------------------
  4. [root@Agustletmen /]# docker run -d -P --name nginx01 -v /etc/nginx nginx
  5. cea26a64e8df744e1ffc43a3809361e509d1a11394ebef52f07636685521b284

2、查看所有的volume(卷)的情况

  1. [root@Agustletmen /]# docker volume --help
  2. Usage: docker volume COMMAND
  3. Manage volumes
  4. Commands:
  5. create Create a volume
  6. inspect Display detailed information on one or more volumes
  7. ls List volumes
  8. prune Remove all unused local volumes
  9. rm Remove one or more volumes
  10. Run 'docker volume COMMAND --help' for more information on a command.
  11. ----------------------------------------------------------------------------------
  12. [root@Agustletmen /]# docker volume ls
  13. DRIVER VOLUME NAME
  14. local 52bf1fef20a9cd57fdb2733ff948e2c659f29d45c9d90768f771f9ca55d9d6ac
  15. local 0692acb6e53ee9da29eae6b034c47feebcf9009a2d2745b90b837771a9d61e31
  16. local f81fb68c61eac7a93f92e256a6f0ed2e6a05d660e0e0b77805b6da163e1e0213
  17. #这些字符串就代表匿名卷(目录),是匿名挂载

3、指定卷来查看挂载

  1. [root@Agustletmen /]# docker volume ls
  2. DRIVER VOLUME NAME
  3. local 52bf1fef20a9cd57fdb2733ff948e2c659f29d45c9d90768f771f9ca55d9d6ac
  4. local 0692acb6e53ee9da29eae6b034c47feebcf9009a2d2745b90b837771a9d61e31
  5. local f81fb68c61eac7a93f92e256a6f0ed2e6a05d660e0e0b77805b6da163e1e0213
  6. local juming-nginx
  7. [root@Agustletmen /]# docker volume inspect 52bf1fef20a9cd57fdb2733ff948e2c659f29d45c9d90768f771f9ca55d9d6ac
  8. [
  9. {
  10. "CreatedAt": "2022-05-07T20:19:55+08:00",
  11. "Driver": "local",
  12. "Labels": null,
  13. "Mountpoint": "/var/lib/docker/volumes/52bf1fef20a9cd57fdb2733ff948e2c659f29d45c9d90768f771f9ca55d9d6ac/_data",
  14. "Name": "52bf1fef20a9cd57fdb2733ff948e2c659f29d45c9d90768f771f9ca55d9d6ac",
  15. "Options": null,
  16. "Scope": "local"
  17. }
  18. ]
1.4.2、具名挂载

1、具名挂载就是指定了主机目录进行挂载(有给卷(主机目录)起名就是具名(有具体的名))

  1. [root@Agustletmen /]# docker run -d -P --name nginx02 -v juming-nginx:/ect/nginx nginx
  2. 96cb922c14fe62d5a3298515d8efae6f5de51bac4239f45eb4b1e49e1aa4dd95

2、查看所有的volume(卷)的情况

  1. [root@Agustletmen /]# docker volume ls
  2. DRIVER VOLUME NAME
  3. local 52bf1fef20a9cd57fdb2733ff948e2c659f29d45c9d90768f771f9ca55d9d6ac
  4. local 0692acb6e53ee9da29eae6b034c47feebcf9009a2d2745b90b837771a9d61e31
  5. local f81fb68c61eac7a93f92e256a6f0ed2e6a05d660e0e0b77805b6da163e1e0213
  6. local juming-nginx

3、指定卷来查看挂载

  1. [root@Agustletmen /]# docker volume inspect juming-nginx
  2. [
  3. {
  4. "CreatedAt": "2022-05-08T11:04:42+08:00",
  5. "Driver": "local",
  6. "Labels": null,
  7. "Mountpoint": "/var/lib/docker/volumes/juming-nginx/_data",
  8. "Name": "juming-nginx",
  9. "Options": null,
  10. "Scope": "local"
  11. }
  12. ]

所有的docker容器内的卷,没有指定绝对路径的目录的情况下都是在/var/lib/docker/volumes/xxxx/_data下.

拓展

  1. 通过 -v 容器内路径: ro rw 改变读写权限
  2. ro #readonly 只读
  3. rw #readwrite 可读可写
  4. docker run -d -P --name nginx05 -v juming:/etc/nginx:ro nginx
  5. docker run -d -P --name nginx05 -v juming:/etc/nginx:rw nginx

ro 只要看到ro就说明这个路径只能通过宿主机来操作,容器内部是无法操作!

2、初始Dockerfile

什么是Dockerfile

Dockerfile 是用来构建docker镜像的构建文件,是一段命令脚本。

创建Dockerfile

2.1、创建docker的测试目录

  1. [root@Agustletmen home]# mkdir docker-test-volume

2.2、创建dockerfile1脚本文件,通过这个脚本可以生成镜像,

dockerfile1脚本文件内容分析:

  1. # 文件中的内容 指令(大写) 参数
  2. # 镜像是一层一层的,这里的每个命令,就是镜像的一层!
  3. FROM centos # 指定镜像
  4. VOLUME ["volume01","volume02"] # 挂载目录
  5. CMD echo "----end----" # CMD 执行命令行命令
  6. CMD /bin/bash
  7. # 以上:wq保存
  8. [root@Agustletmen docker-test-volume]# cat dockerfile1
  9. FROM centos
  10. VOLUME ["volume01","volume02"]
  11. CMD echo "---end---"
  12. CMD /bin/bash

2.3、执行Dockerfile脚本文件,构建镜像

  1. [root@Agustletmen docker-test-volume]# docker build -f dockerfile1 -t qx/centos .
  2. Sending build context to Docker daemon 2.048kB
  3. Step 1/4 : FROM centos
  4. ---> 5d0da3dc9764
  5. Step 2/4 : VOLUME ["volume01","volume02"]
  6. ---> Running in d498c0c44e03
  7. Removing intermediate container d498c0c44e03
  8. ---> 63abf7145d67
  9. Step 3/4 : CMD echo "---end---"
  10. ---> Running in 503556d0efd5
  11. Removing intermediate container 503556d0efd5
  12. ---> 2ed5db878944
  13. Step 4/4 : CMD /bin/bash
  14. ---> Running in f1540f87146c
  15. Removing intermediate container f1540f87146c
  16. ---> 1287388b5493
  17. Successfully built 1287388b5493
  18. Successfully tagged qx/centos:latest
  19. ------------------------------------------------------------------------------------
  20. [root@Agustletmen docker-test-volume]# docker images
  21. REPOSITORY TAG IMAGE ID CREATED SIZE
  22. qx/centos latest 1287388b5493 47 seconds ago 231MB
  23. nginx latest 605c77e624dd 4 months ago 141MB
  24. tomcat latest fb5657adc892 4 months ago 680MB
  25. redis latest 7614ae9453d1 4 months ago 113MB
  26. mysql 5.7 c20987f18b13 4 months ago 448MB
  27. centos latest 5d0da3dc9764 7 months ago 231MB

2.4、启动镜像,查看容器

  1. [root@Agustletmen docker-test-volume]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. qx/centos latest 1287388b5493 47 seconds ago 231MB
  4. nginx latest 605c77e624dd 4 months ago 141MB
  5. tomcat latest fb5657adc892 4 months ago 680MB
  6. redis latest 7614ae9453d1 4 months ago 113MB
  7. mysql 5.7 c20987f18b13 4 months ago 448MB
  8. centos latest 5d0da3dc9764 7 months ago 231MB
  9. ------------------------------------------------------------------------------------
  10. [root@Agustletmen docker-test-volume]# docker images
  11. REPOSITORY TAG IMAGE ID CREATED SIZE
  12. qx/centos latest 1287388b5493 5 minutes ago 231MB
  13. nginx latest 605c77e624dd 4 months ago 141MB
  14. tomcat latest fb5657adc892 4 months ago 680MB
  15. redis latest 7614ae9453d1 4 months ago 113MB
  16. mysql 5.7 c20987f18b13 4 months ago 448MB
  17. centos latest 5d0da3dc9764 7 months ago 231MB
  18. [root@Agustletmen docker-test-volume]# docker run -it 1287388b5493 /bin/bash
  19. ------------------------------------------------------------------------------------
  20. [root@17f19913d5d0 /]# ls -l
  21. total 56
  22. lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin
  23. drwxr-xr-x 5 root root 360 May 8 05:29 dev
  24. drwxr-xr-x 1 root root 4096 May 8 05:29 etc
  25. drwxr-xr-x 2 root root 4096 Nov 3 2020 home
  26. lrwxrwxrwx 1 root root 7 Nov 3 2020 lib -> usr/lib
  27. lrwxrwxrwx 1 root root 9 Nov 3 2020 lib64 -> usr/lib64
  28. drwx------ 2 root root 4096 Sep 15 2021 lost+found
  29. drwxr-xr-x 2 root root 4096 Nov 3 2020 media
  30. drwxr-xr-x 2 root root 4096 Nov 3 2020 mnt
  31. drwxr-xr-x 2 root root 4096 Nov 3 2020 opt
  32. dr-xr-xr-x 111 root root 0 May 8 05:29 proc
  33. dr-xr-x--- 2 root root 4096 Sep 15 2021 root
  34. drwxr-xr-x 11 root root 4096 Sep 15 2021 run
  35. lrwxrwxrwx 1 root root 8 Nov 3 2020 sbin -> usr/sbin
  36. drwxr-xr-x 2 root root 4096 Nov 3 2020 srv
  37. dr-xr-xr-x 13 root root 0 May 8 05:29 sys
  38. drwxrwxrwt 7 root root 4096 Sep 15 2021 tmp
  39. drwxr-xr-x 12 root root 4096 Sep 15 2021 usr
  40. drwxr-xr-x 20 root root 4096 Sep 15 2021 var
  41. # 自己挂载的目录volume01、volume02, 即是数据卷目录
  42. drwxr-xr-x 2 root root 4096 May 8 05:29 volume01
  43. drwxr-xr-x 2 root root 4096 May 8 05:29 volume02

这个卷和外部一定有一个同步的目录,(匿名挂载)

查看这个匿名挂载

  1. [root@Agustletmen home]# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 9e1756bd49df 1287388b5493 "/bin/bash" About a minute ago Up About a minute pedantic_goodall
  4. 2c4b1bc54e01 mysql:5.7 "docker-entrypoint.s…" 3 hours ago Up 3 hours 33060/tcp, 0.0.0.0:9200->3306/tcp, :::9200->3306/tcp mysql01
  5. [root@Agustletmen home]# docker inspect 9e1756bd49df

img

已同步

  1. [root@Agustletmen home]# cd /var/lib/docker/volumes/0d5dded3bf1053999639b223418bb688d323516a0c4e619e7178ba37394e09da
  2. [root@Agustletmen 0d5dded3bf1053999639b223418bb688d323516a0c4e619e7178ba37394e09da]# ls
  3. _data
  4. [root@Agustletmen 0d5dded3bf1053999639b223418bb688d323516a0c4e619e7178ba37394e09da]# cd _data/
  5. [root@Agustletmen _data]# ls
  6. container.txt
  7. [root@Agustletmen _data]#

3、数据卷容器

容器挂载数据卷,实现容器间的数据同步和资源共享!

img

3.1、启动父容器docker01

  1. [root@Agustletmen ~]# docker run -it --name docker01 qx/centos

3.2、启动容器docker02并挂载到父容器docker01的共享卷,命令--volumes-from 容器列表

  1. [root@Agustletmen ~]# docker run -it --name docker02 --volumes-from docker01 qx/centos
  2. [root@2c11c416854a /]# ls -l
  3. total 56
  4. lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin
  5. drwxr-xr-x 5 root root 360 May 8 05:46 dev
  6. drwxr-xr-x 1 root root 4096 May 8 05:46 etc
  7. drwxr-xr-x 2 root root 4096 Nov 3 2020 home
  8. lrwxrwxrwx 1 root root 7 Nov 3 2020 lib -> usr/lib
  9. lrwxrwxrwx 1 root root 9 Nov 3 2020 lib64 -> usr/lib64
  10. drwx------ 2 root root 4096 Sep 15 2021 lost+found
  11. drwxr-xr-x 2 root root 4096 Nov 3 2020 media
  12. drwxr-xr-x 2 root root 4096 Nov 3 2020 mnt
  13. drwxr-xr-x 2 root root 4096 Nov 3 2020 opt
  14. dr-xr-xr-x 127 root root 0 May 8 05:46 proc
  15. dr-xr-x--- 2 root root 4096 Sep 15 2021 root
  16. drwxr-xr-x 11 root root 4096 Sep 15 2021 run
  17. lrwxrwxrwx 1 root root 8 Nov 3 2020 sbin -> usr/sbin
  18. drwxr-xr-x 2 root root 4096 Nov 3 2020 srv
  19. dr-xr-xr-x 13 root root 0 May 8 05:29 sys
  20. drwxrwxrwt 7 root root 4096 Sep 15 2021 tmp
  21. drwxr-xr-x 12 root root 4096 Sep 15 2021 usr
  22. drwxr-xr-x 20 root root 4096 Sep 15 2021 var
  23. drwxr-xr-x 2 root root 4096 May 8 05:44 volume01
  24. drwxr-xr-x 2 root root 4096 May 8 05:44 volume02
  25. --------------------------------------------------------------------------------------------
  26. # docker01的volume01中创建一个文件
  27. [root@ef1fa959c564 /]# cd volume01
  28. [root@ef1fa959c564 volume01]# ls
  29. [root@ef1fa959c564 volume01]# touch docker01.txt
  30. [root@ef1fa959c564 volume01]# ls
  31. docker01.txt
  32. [root@ef1fa959c564 volume01]#
  33. --------------------------------------------------------------------------------------------
  34. # 查看docker02的volume01,发现docker01创建的文件同步过来了
  35. [root@2c11c416854a /]# cd volume01
  36. [root@2c11c416854a volume01]# ls
  37. docker01.txt
  38. [root@2c11c416854a volume01]#
  39. --------------------------------------------------------------------------------------------
  40. # 同理,在docker02创建的文件也会同步到docker01中

docker01容器中

img

docker02容器中

img

这里的docker01就是叫数据卷容器

再加个docker03试试,也有docker01和docker02已经创建的文件

  1. [root@Agustletmen ~]# docker run -it --name docker03 --volumes-from docker01 qx/centos
  2. [root@50edf6994d7b /]# ls
  3. bin etc lib lost+found mnt proc run srv tmp var volume02
  4. dev home lib64 media opt root sbin sys usr volume01
  5. [root@50edf6994d7b /]# cd volume01
  6. [root@50edf6994d7b volume01]# ls
  7. docker01.txt docker02.txt

在docker03创建的文件也会同步到docker01和docker02中

  1. # docker03
  2. [root@50edf6994d7b volume01]# touch docker03.txt
  3. [root@50edf6994d7b volume01]# ls
  4. docker01.txt docker02.txt docker03.txt
  5. [root@50edf6994d7b volume01]#
  6. --------------------------------------------------------------------------------------------
  7. # docker02
  8. [root@2c11c416854a volume01]# ls
  9. docker01.txt docker02.txt docker03.txt
  10. [root@2c11c416854a volume01]#
  11. --------------------------------------------------------------------------------------------
  12. [root@ef1fa959c564 volume01]# ls
  13. docker01.txt docker02.txt docker03.txt
  14. [root@ef1fa959c564 volume01]#

通过 --volumes-from 实现容器之间的数据共享

3.3、小结

  • docker03创建的文件docker01、docker02也同步共享;
  • 删除docker01,docker02和docker03依旧保持数据同步和资源共享,其中docker01之前所创建的文件也还存在与docker02与docker03中;
  • 容器挂载后双向资源绑定,双向拷贝。

3.4、案例:Mysql容器实现数据同步

1、启动mysql01,绑定端口3306,配置密码-e MYSQL_ROOT_PASSWORD=password,挂载卷-v

  1. docker run -it --name mysql01 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -v /etc/mysql/conf.d -v /var/lib/mysql mysql:5.7

2、启动mysql02(绑定端口9200)、mysql03(绑定端口9300)

  1. # mysql02
  2. docker run -it --name mysql02 --volumes-from mysql01 -p 9200:3306 -e MYSQL_ROOT_PASSWORD=123456 -v /etc/mysql/conf.d -v /var/lib/mysql mysql:5.7
  3. # mysql03
  4. docker run -it --name mysql03 --volumes-from mysql01 -p 9300:3306 -e MYSQL_ROOT_PASSWORD=123456 -v /etc/mysql/conf.d -v /var/lib/mysql mysql:5.7

三个数据会实现数据的共享与同步

结论

  • 容器之间的配置信息的传递,数据卷容器的生命周期一直持续到没有容器使用为止。
  • 但是一旦你持久化到了本地,这个时候,本地的数据是不会删除的!

4、Dockerfile(重点)

4.1、Dockerfile介绍

dockerfile是用来构建docker镜像的文件!命令参数脚本!

构建步骤:

  1. 编写一个dockerfile文件
  2. docker build 构建称为一个镜像
  3. docker run运行镜像
  4. docker push发布镜像(DockerHub 、阿里云仓库)

官方的镜像

官方仓库搜索:https://hub.docker.com/\_/centos

img

点击版本跳转到Github,看到dockerfile文件:

docker 99%的镜像都是从FROM scratch(基础镜像)开始

img

很多官方镜像都是基础包,很多功能没有,我们通常会自己搭建自己的镜像!

官方既然可以制作镜像,那我们也可以!

4.2、DockerFile构建过程

DockerFile脚本文件语法

1、每个保留关键字(指令)都是必须是大写字母

2、执行从上到下顺序

3、# 表示注释

4、每一个指令都会创建提交一个新的镜像曾,并提交!

Dockerfile

img

  • Dockerfile是面向开发的,我们以后要发布项目,做镜像,就需要编写dockerfile文件,这个文件十分简单!
  • Docker镜像逐渐成企业交付的标准,必须要掌握!
  • DockerFile:构建文件,定义了一切的步骤,源代码
  • DockerImages:通过DockerFile构建生成的镜像,最终发布和运行产品。
  • Docker容器:容器就是镜像运行起来提供服务。

4.3、Docker的指令







































































指令 解释 小写
FROM 基础镜像,一切从这里开始构建 from
MAINTAINER 镜像是谁写的, 姓名+邮箱(翻译:维护人员) maintainer
RUN 镜像构建的时候需要运行的命令 run
ADD 步骤,tomcat镜像,这个tomcat压缩包!添加内容 添加同目录 add
WORKDIR 镜像的工作目录 workdir
VOLUME 挂载的目录 volume
EXPOSE 保留端口配置(开放的端口) expose
CMD 指定这个容器启动的时候要运行的命令,只有最后一个会生效,可被替代。 cmd
ENTRYPOINT 指定这个容器启动的时候要运行的命令,可以追加命令(入口点) entrypoint
ONBUILD 当构建一个被继承 DockerFile 这个时候就会运行ONBUILD的指令,触发指令。 onbuild
COPY 类似ADD,将我们文件拷贝到镜像中 copy
ENV 构建的时候设置环境变量! env

4.4、Dockerfile实战

Docker Hub 中99%的镜像都是从这个基础镜像过来的 (FROM scratch),然后配置需要的软件和配置来进行构建

img

4.4.1、创建一个自己的centos镜像

官方的centos许多命令都没有,所以我们创建一个自己的centos镜像

4.4.1.1、创建工作目录
  1. [root@Agustletmen home]# mkdir dockerfile
  2. [root@Agustletmen home]# ls
  3. ceshi dockerfile docker-test-volume kk.java mysql test test.java www
  4. [root@Agustletmen home]# cd dockerfile
  5. [root@Agustletmen dockerfile]#
4.4.1.2、编写Dockerfile文件 (文件名:mydockerfile-centos)
  1. FROM centos:7.9.2009
  2. MAINTAINER qx<3066686488@qq.com>
  3. ENV MYPATH /usr/local
  4. WORKDIR $MYPATH
  5. RUN yum -y install vim
  6. RUN yum -y install net-tools
  7. EXPOSE 80
  8. CMD echo $MYPATH
  9. CMD echo "-----end----"
  10. CMD /bin/bash
  11. [root@Agustletmen dockerfile]# cat mydockerfile-centos
  12. FROM centos:7.9.2009
  13. MAINTAINER qx<3066686488@qq.com>
  14. ENV MYPATH /usr/local
  15. WORKDIR $MYPATH
  16. RUN yum -y install vim
  17. RUN yum -y install net-tools
  18. EXPOSE 80
  19. CMD echo $MYPATH
  20. CMD echo "-----end----"
  21. CMD /bin/bash
4.4.1.3、通过这个文件构建镜像
  1. # 通过这个文件构建镜像
  2. # 命令docker build -f dockerfile文件路径 -t 镜像名:[tag] .
  3. docker build -f /home/dockerfile/mydockerfile-centos -t mycentos:0.1 .
  4. --------------------------------------------------------------------------------------------
  5. [root@Agustletmen dockerfile]# docker build -f /home/dockerfile/mydockerfile-centos -t mycentos:0.1 .


  1. Step 5/10 : RUN yum -y install vim
  2. ---> Running in 71b82bf5f0ac
  3. CentOS Linux 8 - AppStream 30 B/s | 38 B 00:01
  4. Error: Failed to download metadata for repo 'appstream': Cannot prepare internal mirrorlist: No URLs in mirrorlist
  5. The command '/bin/sh -c yum -y install vim' returned a non-zero code: 1
  6. # 因为没有vim这个rpm包。vim命令在vim-ehanced这个包内。
  7. yum失败的可以在yum之前加一个更新软件源
  8. systemctl stop firewalld.service # 停止防火墙

百度了一通说因为centOS8于2021年12月31日停止了源的服务,而现在时间是2022年3月了,所以之前的方法都不行的原因所在(本质是源的问题)。
未具体指明 centos7 的镜像时,它会默认去找最新的镜像,然后就一直失败
将脚本中FROM centos改成指定版本FROM centos:FROM centos7
然后就成功了…

查看操作系统的版本

命令:cat /etc/redhat-release

具体操作查看:https://mp.csdn.net/mp\_blog/creation/success/124649895

yum失败的可以在yum之前加一个更新软件源:https://mp.csdn.net/mp\_blog/creation/success/124650686

注意:最后需要重启docker

  1. systemctl restart docker


4.4.1.4、运行测试
  1. # 直接进入工作目录
  2. # ENV MYPATH /usr/local
  3. # WORKDIR $MYPATH
  4. [root@bbb761d1cf2e local]# pwd
  5. /usr/local
  6. --------------------------------------------------------------------------------------------
  7. [root@bbb761d1cf2e local]# iconfig
  8. bash: iconfig: command not found
  9. [root@bbb761d1cf2e local]# ifconfig
  10. eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  11. inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255
  12. ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
  13. RX packets 8 bytes 656 (656.0 B)
  14. RX errors 0 dropped 0 overruns 0 frame 0
  15. TX packets 0 bytes 0 (0.0 B)
  16. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  17. lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
  18. inet 127.0.0.1 netmask 255.0.0.0
  19. loop txqueuelen 1000 (Local Loopback)
  20. RX packets 0 bytes 0 (0.0 B)
  21. RX errors 0 dropped 0 overruns 0 frame 0
  22. TX packets 0 bytes 0 (0.0 B)
  23. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  24. --------------------------------------------------------------------------------------------
  25. [root@bbb761d1cf2e local]# vim test #可用

对比原乡的centos,增加了vim以及ifconfig指令,以及pwd直接进入工作目录/usr/local

4.4.1.5、启动镜像

列出镜像的变更历史

  1. docker history 镜像
  2. [root@Agustletmen ~]# docker images
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. mycentos 0.1 4f21cbf56ece 13 minutes ago 591MB
  5. qx/centos latest 1287388b5493 4 hours ago 231MB
  6. nginx latest 605c77e624dd 4 months ago 141MB
  7. tomcat latest fb5657adc892 4 months ago 680MB
  8. redis latest 7614ae9453d1 4 months ago 113MB
  9. mysql 5.7 c20987f18b13 4 months ago 448MB
  10. centos 7.9.2009 eeb6ee3f44bd 7 months ago 204MB
  11. --------------------------------------------------------------------------------------------
  12. [root@Agustletmen ~]# docker history 4f21cbf56ece
  13. IMAGE CREATED CREATED BY SIZE COMMENT
  14. 4f21cbf56ece 13 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "/bin… 0B
  15. 3c0e004491b5 13 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
  16. 9f4f3866b085 13 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
  17. 447c90c89df6 13 minutes ago /bin/sh -c #(nop) EXPOSE 80 0B
  18. 9381b088a91d 13 minutes ago /bin/sh -c yum -y install net-tools 166MB
  19. e189bef41d7d 14 minutes ago /bin/sh -c yum -y install vim 221MB
  20. a66aa8ec7172 24 minutes ago /bin/sh -c #(nop) WORKDIR /usr/local 0B
  21. a23ce59321e3 24 minutes ago /bin/sh -c #(nop) ENV MYPATH=/usr/local 0B
  22. ece7ed692267 24 minutes ago /bin/sh -c #(nop) MAINTAINER qx<3066686488@… 0B
  23. eeb6ee3f44bd 7 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
  24. <missing> 7 months ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
  25. <missing> 7 months ago /bin/sh -c #(nop) ADD file:b3ebbe8bd304723d4… 204MB

4.4、CMD 和 ENTRYPOINT区别

  • CMD : 指定这个容器启动的时候要运行的命令,只有最后一个会生效,可被替代。(替代的方式)
  • ENTRYPOINT : 指定这个容器启动的时候要运行的命令,可以追加命令。(追加的方式)
4.4.1、测试cmd
4.4.1.1、编写dockerfile文件
  1. [root@Agustletmen ~]# cat dockerfile-cmd-test
  2. FROM centos:7.9.2009
  3. CMD ["ls","-a"]
4.4.1.2、 构建镜像
  1. docker build -f dockerfile-cmd-test -t cmd-test:0.1 .
  2. [root@Agustletmen dockerfile]# docker build -f dockerfile-cmd-test -t cmdtest .
  3. Sending build context to Docker daemon 3.072kB
  4. Step 1/2 : FROM centos:7.9.2009
  5. ---> eeb6ee3f44bd
  6. Step 2/2 : CMD ["ls","-a"]
  7. ---> Running in 8407f693b413
  8. Removing intermediate container 8407f693b413
  9. ---> e96c865a0557
  10. Successfully built e96c865a0557
  11. Successfully tagged cmdtest:latest
4.4.1.3、运行镜像
  1. # 发现我们的ls -a 命令生效
  2. [root@Agustletmen dockerfile]# docker run e96c865a0557
  3. .
  4. ..
  5. .dockerenv
  6. anaconda-post.log
  7. bin
  8. dev
  9. etc
  10. home
  11. lib
  12. lib64
  13. media
  14. mnt
  15. opt
  16. proc
  17. root
  18. run
  19. sbin
  20. srv
  21. sys
  22. tmp
  23. usr
  24. var
  25. ----------------------------------------------------------------------------------------
  26. # 想追加一个命令 -l ls -al
  27. [root@Agustletmen dockerfile]# docker run e96c865a0557 -l
  28. docker: Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "-l": executable file not found in $PATH: unknown.
  29. # cmd的情况下 -l替换了CMD["ls","-a"]命令,而-l不是命令,所以报错
  30. ----------------------------------------------------------------------------------------
  31. # 若要正确的执行,则...
  32. [root@Agustletmen dockerfile]# docker run e96c865a0557 ls -al
  33. total 64
  34. drwxr-xr-x 1 root root 4096 May 8 09:31 .
  35. drwxr-xr-x 1 root root 4096 May 8 09:31 ..
  36. -rwxr-xr-x 1 root root 0 May 8 09:31 .dockerenv
  37. -rw-r--r-- 1 root root 12114 Nov 13 2020 anaconda-post.log
  38. lrwxrwxrwx 1 root root 7 Nov 13 2020 bin -> usr/bin
  39. drwxr-xr-x 5 root root 340 May 8 09:31 dev
  40. drwxr-xr-x 1 root root 4096 May 8 09:31 etc
  41. drwxr-xr-x 2 root root 4096 Apr 11 2018 home
  42. lrwxrwxrwx 1 root root 7 Nov 13 2020 lib -> usr/lib
  43. lrwxrwxrwx 1 root root 9 Nov 13 2020 lib64 -> usr/lib64
  44. drwxr-xr-x 2 root root 4096 Apr 11 2018 media
  45. drwxr-xr-x 2 root root 4096 Apr 11 2018 mnt
  46. drwxr-xr-x 2 root root 4096 Apr 11 2018 opt
  47. dr-xr-xr-x 124 root root 0 May 8 09:31 proc
  48. dr-xr-x--- 2 root root 4096 Nov 13 2020 root
  49. drwxr-xr-x 11 root root 4096 Nov 13 2020 run
  50. lrwxrwxrwx 1 root root 8 Nov 13 2020 sbin -> usr/sbin
  51. drwxr-xr-x 2 root root 4096 Apr 11 2018 srv
  52. dr-xr-xr-x 13 root root 0 May 8 05:29 sys
  53. drwxrwxrwt 7 root root 4096 Nov 13 2020 tmp
  54. drwxr-xr-x 13 root root 4096 Nov 13 2020 usr
  55. drwxr-xr-x 18 root root 4096 Nov 13 2020 var
4.4.2、测试ENTRYPOINT
4.4.2.1、编写dockerfile文件
  1. [root@Agustletmen dockerfile]# vim dockerdile-cmd-entrypoint
  2. FROM centos
  3. ENTRYPOINT ["ls","-a"]
4.4.2.2、构建镜像
  1. [root@Agustletmen dockerfile]# docker build -f dockerfile-cmd-entrypoint -t entrypoint-test .
4.4.2.3、执行
  1. [root@Agustletmen dockerfile]# docker build -f dockerdile-cmd-entrypoint -t entrypoint-test .
  2. Sending build context to Docker daemon 4.096kB
  3. Step 1/2 : FROM centos:7.9.2009
  4. ---> eeb6ee3f44bd
  5. Step 2/2 : ENTRYPOINT ["ls","-a"]
  6. ---> Running in 82dcee6ee6ca
  7. Removing intermediate container 82dcee6ee6ca
  8. ---> f5300821d8f0
  9. Successfully built f5300821d8f0
  10. Successfully tagged entrypoint-test:latest
  11. [root@Agustletmen dockerfile]# docker run f5300821d8f0
  12. .
  13. ..
  14. .dockerenv
  15. anaconda-post.log
  16. bin
  17. dev
  18. etc
  19. home
  20. lib
  21. lib64
  22. media
  23. mnt
  24. opt
  25. proc
  26. root
  27. run
  28. sbin
  29. srv
  30. sys
  31. tmp
  32. usr
  33. var
  34. [root@Agustletmen dockerfile]#
4.4.2.4、追加一个命令 -l
  1. # 我们追加的命令是直接拼接在我们的 ENTRYPOINT 命令的后面
  2. ls -al
  3. [root@Agustletmen dockerfile]# docker run f5300821d8f0 -l #ls -al
  4. total 64
  5. drwxr-xr-x 1 root root 4096 May 8 11:43 .
  6. drwxr-xr-x 1 root root 4096 May 8 11:43 ..
  7. -rwxr-xr-x 1 root root 0 May 8 11:43 .dockerenv
  8. -rw-r--r-- 1 root root 12114 Nov 13 2020 anaconda-post.log
  9. lrwxrwxrwx 1 root root 7 Nov 13 2020 bin -> usr/bin
  10. drwxr-xr-x 5 root root 340 May 8 11:43 dev
  11. drwxr-xr-x 1 root root 4096 May 8 11:43 etc
  12. drwxr-xr-x 2 root root 4096 Apr 11 2018 home
  13. lrwxrwxrwx 1 root root 7 Nov 13 2020 lib -> usr/lib
  14. lrwxrwxrwx 1 root root 9 Nov 13 2020 lib64 -> usr/lib64
  15. drwxr-xr-x 2 root root 4096 Apr 11 2018 media
  16. drwxr-xr-x 2 root root 4096 Apr 11 2018 mnt
  17. drwxr-xr-x 2 root root 4096 Apr 11 2018 opt
  18. dr-xr-xr-x 118 root root 0 May 8 11:43 proc
  19. dr-xr-x--- 2 root root 4096 Nov 13 2020 root
  20. drwxr-xr-x 11 root root 4096 Nov 13 2020 run
  21. lrwxrwxrwx 1 root root 8 Nov 13 2020 sbin -> usr/sbin
  22. drwxr-xr-x 2 root root 4096 Apr 11 2018 srv
  23. dr-xr-xr-x 13 root root 0 May 8 05:29 sys
  24. drwxrwxrwt 7 root root 4096 Nov 13 2020 tmp
  25. drwxr-xr-x 13 root root 4096 Nov 13 2020 usr
  26. drwxr-xr-x 18 root root 4096 Nov 13 2020 var

4.5、Tomcat镜像

4.5.1、准备镜像文件(tomcat压缩包 和 jdk压缩包 )
4.5.2、编写dokerfile文件,官方命名Dockerfile,build 会自动寻找,就不用-f 指定了
  1. FROM centos:7.9.2009
  2. MAINTAINER qx<3066686488@qq.com>
  3. COPY readme.txt /usr/local/readme.txt
  4. ADD apache-tomcat-9.0.35.tar.gz /usr/local/
  5. ADD jdk-8u161-linux-x64.tar.gz /usr/local/
  6. RUN yum -y install vim
  7. ENV MYPATH /usr/local
  8. WORKDIR $MYPATH
  9. ENV JAVA_HOME /usr/local/jdk1.8.0_161
  10. ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
  11. ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.35
  12. ENV CATALINA_BASH /usr/local/apache-tomcat-9.0.35
  13. ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
  14. EXPOSE 8080
  15. CMD /usr/local/apache-tomcat-9.0.35/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.35/bin/logs/catalina.out
4.5.3、构造镜像
  1. [root@Agustletmen kk]# docker build -t diytomcat .
  2. [root@Agustletmen kk]# docker images
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. diytomcat latest 95b3cd025f33 39 seconds ago 825MB
4.5.4、运行镜像
  1. [root@Agustletmen kk]# docker run -d -p 8080:8080 --name qxtomcat -v /home/kk/tomcat/test:/usr/local/apache-tomcat-9.0.35/webapps/test -v /home/kk/tomcat/tomcatlogs/:/usr/local/apache-tomcat-9.0.35/logs diytomcat
  2. 90e57e059efe3920a49648aa8bf792b917cb1c77b186d1332354ca15658f61dc
4.5.5、访问测试
  1. [root@Agustletmen kk]# docker exec -it 90e57e059efe3920 /bin/bash
  2. [root@90e57e059efe local]#
  3. --------------------------------------------------------------------------------------------
  4. [root@90e57e059efe local]# ls
  5. aegis apache-tomcat-9.0.35 etc include lib libexec sbin src
  6. apache-tomcat-9.0.35 bin games jdk1.8.0_161 lib64 readme.txt share
  7. [root@90e57e059efe local]# ls -l
  8. total 60
  9. drwxr-xr-x 3 root root 4096 May 8 13:38 aegis
  10. drwxr-xr-x 1 root root 4096 May 8 13:37 apache-tomcat-9.0.35
  11. drwxr-xr-x 4 root root 4096 May 8 13:38 apache-tomcat-9.0.58
  12. drwxr-xr-x 2 root root 4096 Apr 11 2018 bin
  13. drwxr-xr-x 2 root root 4096 Apr 11 2018 etc
  14. drwxr-xr-x 2 root root 4096 Apr 11 2018 games
  15. drwxr-xr-x 2 root root 4096 Apr 11 2018 include
  16. drwxr-xr-x 8 10 143 4096 Dec 20 2017 jdk1.8.0_161
  17. drwxr-xr-x 2 root root 4096 Apr 11 2018 lib
  18. drwxr-xr-x 2 root root 4096 Apr 11 2018 lib64
  19. drwxr-xr-x 2 root root 4096 Apr 11 2018 libexec
  20. -rw-r--r-- 1 root root 0 May 8 12:13 readme.txt
  21. drwxr-xr-x 2 root root 4096 Apr 11 2018 sbin
  22. drwxr-xr-x 5 root root 4096 Nov 13 2020 share
  23. drwxr-xr-x 2 root root 4096 Apr 11 2018 src
  24. --------------------------------------------------------------------------------------------

本地访问

img

远程访问

img

在本地编写web.xml和index.jsp进行测试

img

img

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <meta charset="utf-8">
  7. <title>hello. qx</title>
  8. </head>
  9. <body>
  10. Hello World!<br/>
  11. <%
  12. System.out.println("-----my test web logs------");
  13. %>
  14. </body>
  15. </html>

发现:项目部署成功, 可以直接访问ok!

4.5.6、发布项目(由于做了卷挂载,我们直接在本地编写项目就可以发布了!)

发现:项目部署成功,可以直接访问!

我们以后开发的步骤:需要掌握Dockerfile的编写!我们之后的一切都是使用docker镜像来发布运行!

img

4.6、发布My镜像到Docker Hub

Docker Hub

4.6.1、地址 注册自己的账号!

img

4.6.2、确定这个账号可以登录
  1. [root@Agustletmen ~]# docker login --help
  2. Usage: docker login [OPTIONS] [SERVER]
  3. Log in to a Docker registry.
  4. If no server is specified, the default is defined by the daemon.
  5. Options:
  6. -p, --password string Password
  7. --password-stdin Take the password from stdin
  8. -u, --username string Username
  9. ------------------------------------------------------------------------------------------
  10. # 登录成功
  11. [root@Agustletmen ~]# docker login -u [用户名]
  12. Password:
  13. WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
  14. Configure a credential helper to remove this warning. See
  15. https://docs.docker.com/engine/reference/commandline/login/#credentials-store
  16. Login Succeeded
4.6.3、登录完毕就可以在提交镜像了,就是一步docker pull
  1. # push到我们的服务器上
  2. [root@Agustletmen ~]# docker push diytomcat
  3. Using default tag: latest
  4. The push refers to repository [docker.io/library/diytomcat]
  5. 7a9cf744cf66: Preparing
  6. c9df6f93b331: Preparing
  7. e9ccbde0f3be: Preparing
  8. aae5953e109a: Preparing
  9. 174f56854903: Preparing
  10. denied: requested access to the resource is denied # 拒绝
  11. # push镜像的问题?
  12. [root@Agustletmen ~]# docker push diytomcat
  13. Using default tag: latest
  14. The push refers to repository [docker.io/library/diytomcat]
  15. denied: requested access to the resource is denied
  16. # 解决,增加一个tag
  17. docker tag [镜像id] chenmuke/diytomcat:1.0
  18. # 发布成功
  19. [root@Agustletmen kk]# docker tag 162589cfe96d chenmuke/diytomcat:1.0
  20. [root@Agustletmen kk]# docker push chenmuke/diytomcat:1.0
  21. aa5e35c983ea: Pushing 60.1MB/221.2MB
  22. a5c23e4ced73: Pushing 44.77MB/384.5MB
  23. a5c23e4ced73: Pushing 43.1MB/384.5MB
  24. aa5e35c983ea: Pushing 62.33MB/221.2MB
  25. a5c23e4ced73: Pushing 49.23MB/384.5MB
  26. aa5e35c983ea: Pushed
  27. a5c23e4ced73: Pushing 231.4MB/384.5MB
  28. a5c23e4ced73: Pushing 349.4MB/384.5MB
  29. a5c23e4ced73: Pushed
  30. 174f56854903: Pushed
  31. 1.0: digest: sha256:0c34175435f7c2f9a0197d70cd4390ebc3fbe2675842f9a38e2a0b47e7ac1f34 size: 1373

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eE8La1Ok-1652100279845)(C:\Users\30666\AppData\Roaming\Typora\typora-user-images\image-20220509133801722.png)]

4.7、发布到阿里云镜像服务上

4.7.1、登录阿里云
4.7.2、找到容器镜像服务
4.7.3、创建命名空间

img

4.7.4、创建容器镜像

img

img

img

点击仓库名称,参考官方文档即可

img

在这里插入图片描述

总结

img

5、Docker网络

清空所有环境

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lCcYJdfG-1652100279847)(C:\Users\30666\AppData\Roaming\Typora\typora-user-images\image-20220509135330752.png)]

5.1、理解Docker0

img

tomcat无法使用ip addr以及ping的可以看一下文章

Docker在官网下载Tomcat镜像,执行ip addr报错

Docker在官网下载Tomcat镜像,执行ping报错

三个网络

  1. # 问题: docker是如何处理容器网络访问的?
  2. [root@Agustletmen ~]# docker run -d -P --name tomcat01 tomcat
  3. 22b2277cb4b45cfab5ef8e6a91754d4bd4b55904fdd74878a584c80297e468dc
  4. --------------------------------------------------------------------------------------------
  5. # 查看容器内部的网络地址 ip addr
  6. # 发现容器启动的时候得到一个 eth0@if155 ip地址,docker分配的!
  7. [root@Agustletmen ~]# docker exec -it tomcat01 ip addr
  8. 1:lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  9. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  10. inet 127.0.0.1/8 scope host lo
  11. valid_lft forever preferred_lft forever
  12. 154: eth0@if155: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
  13. link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
  14. inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
  15. valid_lft forever preferred_lft forever
  16. --------------------------------------------------------------------------------------------
  17. # 思考: linux 能不能ping通容器?
  18. root@22b2277cb4b4:/usr/local/tomcat# ping 172.17.0.3
  19. PING 172.17.0.3 (172.17.0.3) 56(84) bytes of data.
  20. 64 bytes from 172.17.0.3: icmp_seq=1 ttl=64 time=0.043 ms
  21. 64 bytes from 172.17.0.3: icmp_seq=2 ttl=64 time=0.053 ms
  22. 64 bytes from 172.17.0.3: icmp_seq=3 ttl=64 time=0.054 ms
  23. 64 bytes from 172.17.0.3: icmp_seq=4 ttl=64 time=0.058 ms
  24. 64 bytes from 172.17.0.3: icmp_seq=5 ttl=64 time=0.054 ms
  25. # linux 可以 ping 通docker容器内部!

原理

我们每启动一个docker容器, docker就会给docker容器分配一个ip, 我们只要安装了docker,就会有一个网卡 docker0桥接模式,使用的技术是veth-pair技术!

再次测试 ip addr,并且再启动一个容器测试, 发现又多了一对网卡

img

  1. # 我们发现这个容器带来网卡,都是一对对的
  2. # veth-pair 就是一对的虚拟设备接口,他们都是成对出现的,一端连着协议,一端彼此相连
  3. # 正因为有这个特性,veth-pair充当一个桥梁, 连接各种虚拟网络设备
  4. # OpenStac, Docker容器之间的链接,OVS的链接, 都是使用veth-pair技术

测试tomcat01和tomcat02之间是否可以ping通

  1. [root@Agustletmen kk]# docker exec -it tomcat01 ip addr
  2. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  3. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  4. inet 127.0.0.1/8 scope host lo
  5. valid_lft forever preferred_lft forever
  6. 154: eth0@if155: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
  7. link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
  8. inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
  9. valid_lft forever preferred_lft forever
  10. [root@Agustletmen kk]# docker exec -it tomcat02 ping 172.17.0.3
  11. PING 172.17.0.3 (172.17.0.3) 56(84) bytes of data.
  12. 64 bytes from 172.17.0.3: icmp_seq=1 ttl=64 time=0.094 ms
  13. 64 bytes from 172.17.0.3: icmp_seq=2 ttl=64 time=0.091 ms
  14. 64 bytes from 172.17.0.3: icmp_seq=3 ttl=64 time=0.092 ms
  15. 64 bytes from 172.17.0.3: icmp_seq=4 ttl=64 time=0.089 ms

结论:容器与容器之间是可以相互ping通的!

绘制一个网络模型图

img

结论:tomcat01和tomcat02是共用的一个路由器docker0

所有容器不指定网络的情况下,都是docker0路由的,doucker会给我们的容器分配一个默认的可用IP

小结

Docker使用的是Linux的桥接,宿主机中是一个Docker容器的网桥docker0

img

Docker中的所有的网络接口都是虚拟的,虚拟的转发效率高!(内网传递文件!)

只要容器删除,对应的网桥一对就没有了!

img

img

思考一个场景,我们编写了一个微服务,database url =ip; 项目不重启,数据ip换掉了,我们希望可以处理这个问题,可以按名字来进行访问容器

  1. [root@Agustletmen ~]# docker exec -it tomcat02 ping tomcat01
  2. ping: tomcat01: Name or service not known
  3. # 如何可以解决呢?
  4. # 通过--link既可以解决网络连通问题
  5. [root@Agustletmen ~]# docker exec -it tomcat03 ping tomcat02
  6. PING tomcat02 (172.17.0.4) 56(84) bytes of data.
  7. 64 bytes from tomcat02 (172.17.0.4): icmp_seq=1 ttl=64 time=0.133 ms
  8. 64 bytes from tomcat02 (172.17.0.4): icmp_seq=2 ttl=64 time=0.081 ms
  9. 64 bytes from tomcat02 (172.17.0.4): icmp_seq=3 ttl=64 time=0.092 ms
  10. # 反向可以ping通吗?
  11. [root@Agustletmen ~]# docker exec -it tomcat02 ping tomcat03
  12. ping: tomcat03: Name or service not known

探究:inspect!

img

这个tomcat03就是在本地配置了tomcat02的配置

  1. # 查看 hosts 配置 在这里发现原理
  2. [root@Agustletmen ~]# docker exec -it tomcat03 cat /etc/hosts
  3. 127.0.0.1 localhost
  4. ::1 localhost ip6-localhost ip6-loopback
  5. fe00::0 ip6-localnet
  6. ff00::0 ip6-mcastprefix
  7. ff02::1 ip6-allnodes
  8. ff02::2 ip6-allrouters
  9. 172.17.0.4 tomcat02 7118d20ebdb0
  10. 172.17.0.5 00cbb3f37f52

--link 就是在我们的host配置中增加了一个172.17.0.4 tomcat02 7118d20ebdb0

我们现在玩Docker已经不建议使用–link了!

自定义网络!不使用Docker0!

Docker0的问题:它不支持容器名链接访问!

5.3、自定义网络

查看所有的docker网络

img

网络模式

  • bridge: 桥接模式,桥接 docker 默认,自己创建的也是用brdge模式
  • none: 不配置网络
  • host: 和宿主机共享网络
  • container:容器网络连通!(用的少, 局限很大)

测试

  1. # 我们直接启动的命令 默认有 --net bridge 而这个就是我们的docker0
  2. [root@Agustletmen ~]# docker run -d -P --name tomcat01 tomcat
  3. [root@Agustletmen ~]# docker run -d -P --name tomcat01 --net bridge tomcat
  4. # docker0 特定 默认,域名不能访问,--link可以打通连接,当时比较麻烦
  5. # 我们可以自定义一个网络!
  6. # --driver bridge 桥接
  7. # --subnet 192.168.0.0/1 子网地址 192.168.0.2 ~ 192.168.255.255
  8. # --gateway 192.168.0.1 网关,路由器地址
  9. [root@Agustletmen ~]# docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet
  10. fcf905ddf4da0432e6082d982709f21122e40d93ad517214ebae88691edb3e50
  11. [root@Agustletmen ~]# docker network ls
  12. NETWORK ID NAME DRIVER SCOPE
  13. 3196eda46a46 bridge bridge local
  14. 43df728acf9f host host local
  15. fcf905ddf4da mynet bridge local
  16. c5e663a596ff none null local

我们自己创建的网络就ok了!

img

在自己创建的网络里面启动两个容器

  1. [root@Agustletmen ~]# docker run -d -P --name tomcat-net-01 --net mynet tomcat
  2. 8d33398d8a78347c449e8ee66fa87a60381fd2298a7ae45bca07156b4663b093
  3. [root@Agustletmen ~]# docker run -d -P --name tomcat-net-02 --net mynet tomcat
  4. fd5c63570d9ace63006b49e07f0231183d1b43bca24084006b0e7503d1e89470
  5. [root@Agustletmen ~]# docker inspect mynet
  6. [
  7. {
  8. "Name": "mynet",
  9. "Id": "fcf905ddf4da0432e6082d982709f21122e40d93ad517214ebae88691edb3e50",
  10. "Created": "2022-05-09T15:32:32.199790374+08:00",
  11. "Scope": "local",
  12. "Driver": "bridge",
  13. "EnableIPv6": false,
  14. "IPAM": {
  15. "Driver": "default",
  16. "Options": {
  17. },
  18. "Config": [
  19. {
  20. "Subnet": "192.168.0.0/16",
  21. "Gateway": "192.168.0.1"
  22. }
  23. ]
  24. },
  25. "Internal": false,
  26. "Attachable": false,
  27. "Ingress": false,
  28. "ConfigFrom": {
  29. "Network": ""
  30. },
  31. "ConfigOnly": false,
  32. "Containers": {
  33. "8d33398d8a78347c449e8ee66fa87a60381fd2298a7ae45bca07156b4663b093": {
  34. "Name": "tomcat-net-01",
  35. "EndpointID": "db3c3e7106361166d85dae736b484dafb4f0003b1b84e0474e99b21c4cb8e302",
  36. "MacAddress": "02:42:c0:a8:00:02",
  37. "IPv4Address": "192.168.0.2/16",
  38. "IPv6Address": ""
  39. },
  40. "fd5c63570d9ace63006b49e07f0231183d1b43bca24084006b0e7503d1e89470": {
  41. "Name": "tomcat-net-02",
  42. "EndpointID": "e37a122b7b8e2d6e5289191d1e3158d845bca27d6f80cc43d2da65fb0883bc8b",
  43. "MacAddress": "02:42:c0:a8:00:03",
  44. "IPv4Address": "192.168.0.3/16",
  45. "IPv6Address": ""
  46. }
  47. },
  48. "Options": {
  49. },
  50. "Labels": {
  51. }
  52. }
  53. ]
  54. # 再次测试ping连接
  55. # 现在不适用 --link也可以使用 ping 名字
  56. # 而且双向都可以ping
  57. [root@Agustletmen ~]# docker exec -it tomcat-net-01 ping 192.168.0.3
  58. PING 192.168.0.3 (192.168.0.3) 56(84) bytes of data.
  59. 64 bytes from 192.168.0.3: icmp_seq=1 ttl=64 time=0.085 ms
  60. 64 bytes from 192.168.0.3: icmp_seq=2 ttl=64 time=0.065 ms
  61. 64 bytes from 192.168.0.3: icmp_seq=3 ttl=64 time=0.063 ms
  62. ------------------------------------------------------------------------------------------
  63. [root@Agustletmen ~]# docker exec -it tomcat-net-01 ping tomcat-net-02
  64. PING tomcat-net-02 (192.168.0.3) 56(84) bytes of data.
  65. 64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=1 ttl=64 time=0.057 ms
  66. 64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=2 ttl=64 time=0.059 ms
  67. 64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=3 ttl=64 time=0.074 ms
  68. 64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=4 ttl=64 time=0.083 ms

我们自定义的网络docker都已经帮我们维护好了对应的关系,推荐我们平时这样使用网络

好处:

  • redis - 不同的集群使用不同的网络,保证集群时安全和健康的
  • mysql - 不同的集群使用不同的网络,保证集群时安全和健康的

5.4、网络连通

  1. [root@Agustletmen ~]# docker run -d -P --name tomcat01 tomcat
  2. 7118cab1a6efce565b5555dd72de56fa9f30017aada88700ef095e45d43bd085
  3. [root@Agustletmen ~]# docker run -d -P --name tomcat02 tomcat
  4. eb6575c7545c5961912c261440b4396c2f54636df630a93dcf1ce630025c798e

测试打通tomcat01和mynet

img

img

img

  1. [root@Agustletmen ~]# docker network connect mynet tomcat01
  2. # 测试打通tomcat01 到 mynet
  3. # 连通之后就是讲tomcat01 放到了mynet网路下
  4. # 一个容器两个ip地址:
  5. # 阿里云服务器,公网ip,私网ip

img

  1. # tomcat01连通,有connect
  2. [root@Agustletmen ~]# docker exec -it tomcat01 ping tomcat-net-01
  3. PING tomcat-net-01 (192.168.0.2) 56(84) bytes of data.
  4. 64 bytes from tomcat-net-01.mynet (192.168.0.2): icmp_seq=1 ttl=64 time=0.098 ms
  5. 64 bytes from tomcat-net-01.mynet (192.168.0.2): icmp_seq=2 ttl=64 time=0.080 ms
  6. 64 bytes from tomcat-net-01.mynet (192.168.0.2): icmp_seq=3 ttl=64 time=0.072 ms
  7. --------------------------------------------------------------------------------------------
  8. # tomcat02依旧无法连通,没有connect
  9. [root@Agustletmen ~]# docker exec -it tomcat02 ping tomcat-net-01
  10. ping: tomcat-net-01: Name or service not known
  11. --------------------------------------------------------------------------------------------
  12. [root@Agustletmen ~]# docker network connect mynet tomcat02
  13. [root@Agustletmen ~]# docker exec -it tomcat02 ping tomcat-net-01
  14. PING tomcat-net-01 (192.168.0.2) 56(84) bytes of data.
  15. 64 bytes from tomcat-net-01.mynet (192.168.0.2): icmp_seq=1 ttl=64 time=0.106 ms
  16. 64 bytes from tomcat-net-01.mynet (192.168.0.2): icmp_seq=2 ttl=64 time=0.069 ms
  17. 64 bytes from tomcat-net-01.mynet (192.168.0.2): icmp_seq=3 ttl=64 time=0.076 ms
  18. 64 bytes from tomcat-net-01.mynet (192.168.0.2): icmp_seq=4 ttl=64 time=0.106 ms

结论:假设要跨网络 操作别人,就要使用docker network connect连通.....!

5.5、部署Redis集群

img

  1. # 创建网卡
  2. docker network create redis --subnet 172.38.0.0/16
  3. # 通过脚本创建六个redis配置
  4. for port in $(seq 1 6); \
  5. do \
  6. mkdir -p /mydata/redis/node-${port}/conf
  7. touch /mydata/redis/node-${port}/conf/redis.conf
  8. cat << EOF >/mydata/redis/node-${port}/conf/redis.conf
  9. port 6379
  10. bind 0.0.0.0
  11. cluster-enabled yes
  12. cluster-config-file nodes.conf
  13. cluster-node-timeout 5000
  14. cluster-announce-ip 172.38.0.1${port}
  15. cluster-announce-port 6379
  16. cluster-announce-bus-port 16379
  17. appendonly yes
  18. EOF
  19. done
  20. # 创建结点1
  21. docker run -p 6371:6379 -p 16371:16379 --name redis-1 \
  22. -v /mydata/redis/node-1/data:/data \
  23. -v /mydata/redis/node-1/conf/redis.conf:/etc/redis/redis.conf \
  24. -d --net redis --ip 172.38.0.11 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
  25. #创建结点2
  26. docker run -p 6372:6379 -p 16372:16379 --name redis-2 \
  27. -v /mydata/redis/node-2/data:/data \
  28. -v /mydata/redis/node-2/conf/redis.conf:/etc/redis/redis.conf \
  29. -d --net redis --ip 172.38.0.12 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
  30. #创建结点3
  31. docker run -p 6373:6379 -p 16373:16379 --name redis-3 \
  32. -v /mydata/redis/node-3/data:/data \
  33. -v /mydata/redis/node-3/conf/redis.conf:/etc/redis/redis.conf \
  34. -d --net redis --ip 172.38.0.13 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
  35. #创建结点4
  36. docker run -p 6374:6379 -p 16374:16379 --name redis-4 \
  37. -v /mydata/redis/node-4/data:/data \
  38. -v /mydata/redis/node-4/conf/redis.conf:/etc/redis/redis.conf \
  39. -d --net redis --ip 172.38.0.14 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
  40. #创建结点5
  41. docker run -p 6375:6379 -p 16375:16379 --name redis-5 \
  42. -v /mydata/redis/node-5/data:/data \
  43. -v /mydata/redis/node-5/conf/redis.conf:/etc/redis/redis.conf \
  44. -d --net redis --ip 172.38.0.15 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
  45. #创建结点6
  46. docker run -p 6376:6379 -p 16376:16379 --name redis-6 \
  47. -v /mydata/redis/node-6/data:/data \
  48. -v /mydata/redis/node-6/conf/redis.conf:/etc/redis/redis.conf \
  49. -d --net redis --ip 172.38.0.16 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
  50. # 创建集群
  51. /data # redis-cli --cluster create 172.38.0.11:6379 172.38.0.12:6379 172.3
  52. 8.0.13:6379 172.38.0.14:6379 172.38.0.15:6379 172.38.0.16:6379 --cluster-r
  53. eplicas 1
  54. >>> Performing hash slots allocation on 6 nodes...
  55. Master[0] -> Slots 0 - 5460
  56. Master[1] -> Slots 5461 - 10922
  57. Master[2] -> Slots 10923 - 16383
  58. Adding replica 172.38.0.15:6379 to 172.38.0.11:6379
  59. Adding replica 172.38.0.16:6379 to 172.38.0.12:6379
  60. Adding replica 172.38.0.14:6379 to 172.38.0.13:6379
  61. M: 9e06e807213a188a1c6c02a7fcc4150c766d7c2e 172.38.0.11:6379
  62. slots:[0-5460] (5461 slots) master
  63. M: fe4a0fed86e8c0ac912a830b028da406127483fc 172.38.0.12:6379
  64. slots:[5461-10922] (5462 slots) master
  65. M: c2666dbc353b2f7ebb679250e73053d942fc4170 172.38.0.13:6379
  66. slots:[10923-16383] (5461 slots) master
  67. S: 9b9d54ed658da0f0cf78c2f535826f200641e74c 172.38.0.14:6379
  68. replicates c2666dbc353b2f7ebb679250e73053d942fc4170
  69. S: 9439ea45481b5e44406ca41b57649549b0a6174c 172.38.0.15:6379
  70. replicates 9e06e807213a188a1c6c02a7fcc4150c766d7c2e
  71. S: e6a1d68fbd470b639f4f30348ca6ff7b251a0a40 172.38.0.16:6379
  72. replicates fe4a0fed86e8c0ac912a830b028da406127483fc
  73. Can I set the above configuration? (type 'yes' to accept): yes
  74. >>> Nodes configuration updated
  75. >>> Assign a different config epoch to each node
  76. >>> Sending CLUSTER MEET messages to join the cluster
  77. Waiting for the cluster to join
  78. .....
  79. >>> Performing Cluster Check (using node 172.38.0.11:6379)
  80. M: 9e06e807213a188a1c6c02a7fcc4150c766d7c2e 172.38.0.11:6379
  81. slots:[0-5460] (5461 slots) master
  82. 1 additional replica(s)
  83. M: c2666dbc353b2f7ebb679250e73053d942fc4170 172.38.0.13:6379
  84. slots:[10923-16383] (5461 slots) master
  85. 1 additional replica(s)
  86. M: fe4a0fed86e8c0ac912a830b028da406127483fc 172.38.0.12:6379
  87. slots:[5461-10922] (5462 slots) master
  88. 1 additional replica(s)
  89. S: 9439ea45481b5e44406ca41b57649549b0a6174c 172.38.0.15:6379
  90. slots: (0 slots) slave
  91. replicates 9e06e807213a188a1c6c02a7fcc4150c766d7c2e
  92. S: 9b9d54ed658da0f0cf78c2f535826f200641e74c 172.38.0.14:6379
  93. slots: (0 slots) slave
  94. replicates c2666dbc353b2f7ebb679250e73053d942fc4170
  95. S: e6a1d68fbd470b639f4f30348ca6ff7b251a0a40 172.38.0.16:6379
  96. slots: (0 slots) slave
  97. replicates fe4a0fed86e8c0ac912a830b028da406127483fc
  98. [OK] All nodes agree about slots configuration.
  99. >>> Check for open slots...
  100. >>> Check slots coverage...
  101. [OK] All 16384 slots covered.

启动redis

  1. /data # redis-cli -c
  2. 127.0.0.1:6379> cluster info
  3. cluster_state:ok
  4. cluster_slots_assigned:16384
  5. cluster_slots_ok:16384
  6. cluster_slots_pfail:0
  7. cluster_slots_fail:0
  8. cluster_known_nodes:6
  9. cluster_size:3
  10. cluster_current_epoch:6
  11. cluster_my_epoch:1
  12. cluster_stats_messages_ping_sent:126
  13. cluster_stats_messages_pong_sent:131
  14. cluster_stats_messages_sent:257
  15. cluster_stats_messages_ping_received:126
  16. cluster_stats_messages_pong_received:126
  17. cluster_stats_messages_meet_received:5
  18. cluster_stats_messages_received:257
  19. 127.0.0.1:6379> cluster nodes
  20. c2666dbc353b2f7ebb679250e73053d942fc4170 172.38.0.13:6379@16379 master - 0 1652096997614 3 connected 10923-16383
  21. fe4a0fed86e8c0ac912a830b028da406127483fc 172.38.0.12:6379@16379 master - 0 1652096996098 2 connected 5461-10922
  22. 9439ea45481b5e44406ca41b57649549b0a6174c 172.38.0.15:6379@16379 slave 9e06e807213a188a1c6c02a7fcc4150c766d7c2e 0 1652096996000 5 connected
  23. 9b9d54ed658da0f0cf78c2f535826f200641e74c 172.38.0.14:6379@16379 slave c2666dbc353b2f7ebb679250e73053d942fc4170 0 1652096997102 4 connected
  24. e6a1d68fbd470b639f4f30348ca6ff7b251a0a40 172.38.0.16:6379@16379 slave fe4a0fed86e8c0ac912a830b028da406127483fc 0 1652096996599 6 connected
  25. 9e06e807213a188a1c6c02a7fcc4150c766d7c2e 172.38.0.11:6379@16379 myself,master - 0 1652096997000 1 connected 0-5460
  26. 127.0.0.1:6379> set name qx
  27. -> Redirected to slot [5798] located at 172.38.0.12:6379
  28. OK

197.38.0.16是172.38.0.12的从机

  1. 127.0.0.1:6379> set name qx
  2. -> Redirected to slot [5798] located at 172.38.0.12:6379
  3. OK
  4. --------------------------------------------------------------------------------------------
  5. [root@Agustletmen ~]# docker stop redis-2
  6. --------------------------------------------------------------------------------------------
  7. 127.0.0.1:6379> get name
  8. -> Redirected to slot [5798] located at 172.38.0.16:6379
  9. "qx"

docker搭建redis集群完成

img

6、SpringBoot微服务打包Docker镜像

1.构建springboot项目

2.打包应用

3.编写Dockerfile

  1. FROM java:8
  2. COPY *.jar /app.jar
  3. CMD ["--server.port=8080"]
  4. EXPOSE 8080
  5. ENTRYPOINT ["java","-jar","/app.jar"]

img

将Dockerfile以及jar包上传之 /home/idea目录下

img

4.构建镜像

  1. [root@Agustletmen idea]# ls
  2. Dockerfile springboot-docker-0.0.1-SNAPSHOT.jar
  3. [root@Agustletmen idea]# docker build -t qx666 .
  4. Sending build context to Docker daemon 17.55MB
  5. Step 1/5 : FROM java:8
  6. 8: Pulling from library/java
  7. 5040bd298390: Pull complete
  8. fce5728aad85: Pull complete
  9. 76610ec20bf5: Pull complete
  10. 60170fec2151: Pull complete
  11. e98f73de8f0d: Pull complete
  12. 11f7af24ed9c: Pull complete
  13. 49e2d6393f32: Pull complete
  14. bb9cdec9c7f3: Pull complete
  15. Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d
  16. Status: Downloaded newer image for java:8
  17. ---> d23bdf5b1b1b
  18. Step 2/5 : COPY *.jar /app.jar
  19. ---> 99d0bdb81632
  20. Step 3/5 : CMD ["--server.port=8080"]
  21. ---> Running in ce31c13897cc
  22. Removing intermediate container ce31c13897cc
  23. ---> 423238a7e19c
  24. Step 4/5 : EXPOSE 8080
  25. ---> Running in cf3b11c9e684
  26. Removing intermediate container cf3b11c9e684
  27. ---> 55174b580640
  28. Step 5/5 : ENTRYPOINT ["java","-jar","/app.jar"]
  29. ---> Running in 3a997cb8ad5a
  30. Removing intermediate container 3a997cb8ad5a
  31. ---> d08bd824d0e4
  32. Successfully built d08bd824d0e4
  33. Successfully tagged qx666:latest

5.发布运行!

  1. [root@Agustletmen idea]# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. b67858910214 qx666 "java -jar /app.jar …" About a minute ago Up About a minute 0.0.0.0:49163->8080/tcp, :::49163->8080/tcp qx-springboot-web
  4. [root@Agustletmen idea]# curl localhost:49163
  5. {
  6. "timestamp":"2022-05-09T12:36:45.403+00:00","status":404,"error":"Not Found","path":"/"}
  7. [root@Agustletmen idea]# curl localhost:49163/hello
  8. hello,qx

远程访问(需要开启阿里云上的安全组配置)

img

5-1652100279854)]

将Dockerfile以及jar包上传之 /home/idea目录下

[外链图片转存中…(img-vrZyroq3-1652100279854)]

4.构建镜像

  1. [root@Agustletmen idea]# ls
  2. Dockerfile springboot-docker-0.0.1-SNAPSHOT.jar
  3. [root@Agustletmen idea]# docker build -t qx666 .
  4. Sending build context to Docker daemon 17.55MB
  5. Step 1/5 : FROM java:8
  6. 8: Pulling from library/java
  7. 5040bd298390: Pull complete
  8. fce5728aad85: Pull complete
  9. 76610ec20bf5: Pull complete
  10. 60170fec2151: Pull complete
  11. e98f73de8f0d: Pull complete
  12. 11f7af24ed9c: Pull complete
  13. 49e2d6393f32: Pull complete
  14. bb9cdec9c7f3: Pull complete
  15. Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d
  16. Status: Downloaded newer image for java:8
  17. ---> d23bdf5b1b1b
  18. Step 2/5 : COPY *.jar /app.jar
  19. ---> 99d0bdb81632
  20. Step 3/5 : CMD ["--server.port=8080"]
  21. ---> Running in ce31c13897cc
  22. Removing intermediate container ce31c13897cc
  23. ---> 423238a7e19c
  24. Step 4/5 : EXPOSE 8080
  25. ---> Running in cf3b11c9e684
  26. Removing intermediate container cf3b11c9e684
  27. ---> 55174b580640
  28. Step 5/5 : ENTRYPOINT ["java","-jar","/app.jar"]
  29. ---> Running in 3a997cb8ad5a
  30. Removing intermediate container 3a997cb8ad5a
  31. ---> d08bd824d0e4
  32. Successfully built d08bd824d0e4
  33. Successfully tagged qx666:latest

5.发布运行!

  1. [root@Agustletmen idea]# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. b67858910214 qx666 "java -jar /app.jar …" About a minute ago Up About a minute 0.0.0.0:49163->8080/tcp, :::49163->8080/tcp qx-springboot-web
  4. [root@Agustletmen idea]# curl localhost:49163
  5. {
  6. "timestamp":"2022-05-09T12:36:45.403+00:00","status":404,"error":"Not Found","path":"/"}
  7. [root@Agustletmen idea]# curl localhost:49163/hello
  8. hello,qx

远程访问(需要开启阿里云上的安全组配置)

[外链图片转存中…(img-oDrQDR4S-1652100279855)]

img

发表评论

表情:
评论列表 (有 0 条评论,116人围观)

还没有评论,来说两句吧...

相关阅读

    相关 三、Docker

    1 容器数据卷 1.1 卷技术介绍 关键字:持久化、同步、数据共享。 通过前面Docker入门的学习,我们可以知道docker的理念就是把应用和环境打包成镜像,这

    相关 Docker

    容器数据卷 什么是容器数据卷 将应用和环境打包成一个镜像! 数据?如果数据都在容器中,那么我们容器删除,数据就会丢失!需求:数据可以持久化 MySQL,容器删除了,