文章目录
- 1、docker部署Mysql
- 2、dockerfile构建镜像
-
- 1.1、在idea中导入课件中的项目`资料\day01\ebuy-docker`
- 1.2、修改项目application.yml数据库连接参数
- 1.3、启动项目访问测试:http://localhost:8081/
- [1.4、执行mvn package命令进行项目打包](#1.4、执行mvn package命令进行项目打包)
- 1.5、虚拟机中新建目录/opt/dockerfileebuy
- 1.6、上传jar包到/opt/dockerfileebuy目录下
- 3、dockerfile文件
- [4、构建镜像 ebuy-docker:v1.0](#4、构建镜像 ebuy-docker:v1.0)
- [5、创建容器 ebuy-docker](#5、创建容器 ebuy-docker)
- [6、查看 容器 ebuy-docker 的 / 目录](#6、查看 容器 ebuy-docker 的 / 目录)
需求:将提供的Spring Boot项目使用容器化进行部署
分析:
1、Spring Boot项目中使用到了Mysql数据库,先使用docker部署mysql
2、Spring Boot项目使用docker容器进行部署:可以基于上一步java环境的dockerfile创建springboot项目的容器。
实现步骤:
1、使用docker部署mysql
2、使用dockerfile构建Spring Boot镜像
- 将Spring Boot打包成一个fat jar包
- 把jar包上传到Linux服务器上
- 在jar包目录下编写dockerfile文件
- 构建镜像
3、创建容器进行测试
1、docker部署Mysql
使用docker部署Mysql步骤如下所示:
shell
# 创建mysql容器。 -e: 设置环境变量 --privileged=true 开启root用户权限
docker run -di --name mysql01 -p 4306:3306 -v mysql_data01:/var/lib/mysql -v mysql_conf01:/etc/mysql --privileged=true -e MYSQL_ROOT_PASSWORD=1234 mysql:8
shell
docker run -di \
--name mysql01 \
-p 4306:3306 \
-v mysql_data01:/var/lib/mysql \
-v mysql_conf01:/etc/mysql \
--privileged=true \
-e MYSQL_ROOT_PASSWORD=1234 \
mysql:8
使用Navicat连接mysql:
创建数据库:docker
创建表:tb_school
sql
CREATE TABLE `tb_school` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`address` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
添加测试数据:
sql
INSERT INTO `tb_school` VALUES (1, '尚硅谷-北京校区', '北京市昌平区宏福科技园2号楼3层');
INSERT INTO `tb_school` VALUES (2, '尚硅谷-上海校区', '上海市松江区谷阳北路166号大江商厦3层');
INSERT INTO `tb_school` VALUES (3, '尚硅谷-深圳校区', '深圳市宝安区西部硅谷大厦B座C区一层');
INSERT INTO `tb_school` VALUES (4, '尚硅谷-西安校区', '西安市雁塔区和发智能大厦B座3层');
INSERT INTO `tb_school` VALUES (5, '尚硅谷-成都校区', '成都市成华区北辰星拱青创园综合楼3层');
INSERT INTO `tb_school` VALUES (6, '尚硅谷-武汉校区', '武汉市东湖高新区东湖网谷6号楼4层');
2、dockerfile构建镜像
具体步骤:
1.1、在idea中导入课件中的项目资料\day01\ebuy-docker
如果发现项目导进来报错,报的错和jdk或者maven相关的。
检查maven配置和jdk配置。
1.2、修改项目application.yml数据库连接参数
1.3、启动项目访问测试:http://localhost:8081/
1.4、执行mvn package命令进行项目打包
bash
E:\尚硅谷\13-尚品甄选\资料\day01\ebuy-docker>mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------< com.atguigu.ebuy:ebuy-docker >--------------------
[INFO] Building ebuy-docker 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.3.0:resources (default-resources) @ ebuy-docker ---
[INFO] Copying 1 resource
[INFO] Copying 6 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ ebuy-docker ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 6 source files to E:\尚硅谷\13-尚品甄选\资料\day01\ebuy-docker\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.3.0:testResources (default-testResources) @ ebuy-docker ---
[INFO] skip non existing resourceDirectory E:\尚硅谷\13-尚品甄选\资料\day01\ebuy-docker\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.10.1:testCompile (default-testCompile) @ ebuy-docker ---
[INFO] Changes detected - recompiling the module!
[INFO]
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ ebuy-docker ---
[INFO]
[INFO] --- maven-jar-plugin:3.3.0:jar (default-jar) @ ebuy-docker ---
[INFO] Building jar: E:\尚硅谷\13-尚品甄选\资料\day01\ebuy-docker\target\ebuy-docker-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:3.0.5:repackage (repackage) @ ebuy-docker ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.494 s
[INFO] Finished at: 2024-09-26T14:13:46+08:00
[INFO] ------------------------------------------------------------------------
1.5、虚拟机中新建目录/opt/dockerfileebuy
shell
[root@localhost ~]# mkdir /opt/dockerfileebuy
[root@localhost ~]# cd /opt
[root@localhost opt]# ls
apache-tomcat-10.1.10 jdk-17.0.7 mysql-community-client-8.0.30-1.el7.x86_64.rpm mysql-community-icu-data-files-8.0.30-1.el7.x86_64.rpm redis-7.0.10
apache-tomcat-10.1.10.tar.gz jdk-17_linux-x64_bin.tar.gz mysql-community-client-plugins-8.0.30-1.el7.x86_64.rpm mysql-community-libs-8.0.30-1.el7.x86_64.rpm redis-7.0.10.tar.gz
containerd kafka mysql-community-common-8.0.30-1.el7.x86_64.rpm mysql-community-libs-compat-8.0.30-1.el7.x86_64.rpm rh
dockerfileebuy kibana mysql-community-debuginfo-8.0.30-1.el7.x86_64.rpm mysql-community-server-8.0.30-1.el7.x86_64.rpm tool
dockerfilejdk17 mongo mysql-community-devel-8.0.30-1.el7.x86_64.rpm mysql-community-server-debug-8.0.30-1.el7.x86_64.rpm z3.tar
elasticsearch mysql-8.0.30-1.el7.x86_64.rpm-bundle.tar mysql-community-embedded-compat-8.0.30-1.el7.x86_64.rpm mysql-community-test-8.0.30-1.el7.x86_64.rpm
1.6、上传jar包到/opt/dockerfileebuy目录下
3、dockerfile文件
/opt/dockerfileebuy目录下新建dockerfile文件
shell
[root@localhost dockerfileebuy]# vim dockerfile
shell
FROM centos7-jdk17
MAINTAINER atguigu
# 声明容器内主进程所对应的端口
EXPOSE 8081
ADD ebuy-docker-1.0-SNAPSHOT.jar /ebuy-docker-1.0-SNAPSHOT.jar
# 相当于windows中的cd命令
WORKDIR /
ENTRYPOINT ["java" , "-jar" , "ebuy-docker-1.0-SNAPSHOT.jar"]
目录结构如下所示:
shell
[root@localhost dockerfileebuy]# ll
总用量 27320
-rw-r--r--. 1 root root 272 9月 26 14:28 dockerfile
-rw-r--r--. 1 root root 27968970 9月 26 14:24 ebuy-docker-1.0-SNAPSHOT.jar
4、构建镜像 ebuy-docker:v1.0
shell
[root@localhost dockerfileebuy]# docker build -t ebuy-docker:v1.0 .
[+] Building 7.2s (7/7) FINISHED docker:default
=> [internal] load .dockerignore 0.1s
=> => transferring context: 2B 0.0s
=> [internal] load build definition from dockerfile 0.1s
=> => transferring dockerfile: 364B 0.0s
=> [internal] load metadata for docker.io/library/centos7-jdk17:latest 0.0s
=> [1/3] FROM docker.io/library/centos7-jdk17 0.1s
=> [internal] load build context 5.3s
=> => transferring context: 27.97MB 5.2s
=> [2/3] ADD ebuy-docker-1.0-SNAPSHOT.jar /ebuy-docker-1.0-SNAPSHOT.jar 1.2s
=> exporting to image 0.3s
=> => exporting layers 0.2s
=> => writing image sha256:f21623c70ab5356567f5930660602cb3634bf719fc6cfbbbcf4ab09bd1c5c401 0.0s
=> => naming to docker.io/library/ebuy-docker:v1.0
5、创建容器 ebuy-docker
shell
docker run -d \
--name ebuy-docker \
-p 8081:8081 \
ebuy-docker:v1.0
shell
[root@localhost dockerfileebuy]# docker run -d \
> --name ebuy-docker \
> -p 8081:8081 \
> ebuy-docker:v1.0
57068d7ebc26f7209079cd8c3b1fa6627712a6f5f5e8eb8bda793ade46196f6f
6、查看 容器 ebuy-docker 的 / 目录
shell
[root@localhost dockerfileebuy]# docker exec -it ebuy-docker /bin/bash
[root@57068d7ebc26 /]# ls
anaconda-post.log bin dev ebuy-docker-1.0-SNAPSHOT.jar etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var