[转]Docker 部署SpringBoot项目不香吗?
Docker 部署SpringBoot项目不香吗?
点击上方“方志朋”,选择“设为星标”
回复”666“获取新整理的面试文章
作者:流星007
链接:toutiao.com/i6843391272229536267
目录
- docker介绍
- 安装docker
- Ubuntu安装docker
- CentOS安装docker
- 通过脚本安装
- 拉取java环境
- 创建springboot项目
- 打包springboot到docker
- docker查看容器的日志
- 查看log4j2输出问文件日志
Ubuntu安装docker
由于本人的系统是CentOS,所以这里就只展示一下CentOS的安装方式,Ubuntu以及其他系统请自行百度。CentOS安装docker
docker要求CentOS在7.0以后的版本,如果你的系统版本还在7.0以前,请先升级一下版本在进行安装,同时不支持32位的系统,内核版本至少3.10。请先确保没有安装过docker,否则有可能会导致安装失败,如果之前安装过,可以尝试直接yum isntall -y docker1.更新软件源第一个命令yum update
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum update
yum install -y docker-ce
systemctl start docker
5.重启
systemctl restart docker
6.停止
systemctl stop docker
7.开机自启动
systemctl enable docker
8.查看docker的状态
systemctl status docker
通过脚本安装
除了上面的yum安装,还可以采用shell脚本安装,安装流程如下:curl -fsSL https://get.docker.com/ | sh
或者
wget -qO- https://get.docker.com/ | sh
拉取jdk很简单,直接执行一行代码即可,我这里拉取的时java 8的版本。
docker pull java:8
拉取完成之后执行:
docker images
如果出现下面内容,表示拉取成功
<properties>
<docker.image.prefix>registry.aliyuncs.com/linhuatest</docker.image.prefix>
</properties>
<!-- docker插件 -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<!--docker文件所在的目录-->
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
3.新建docker文件在main目录下新建docker目录,然后在docker目录下创建dockerfile文件,不需要后缀名。FROM java:8
VOLUME /tmp/tomcat
ADD spring-boot-docker-0.0.1-SNAPSHOT.jar springboot-docker.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/springboot-docker.jar"]
FROM:指定存在的镜像,java:8是我刚刚拉取的镜像,运行的基础。VOLUME:指向的一个临时文件,用于存储tomcat工作。ADD:复制文件并且重命名文件。ENTRYPOINT:初始化配置或者自定义配置。5.创建测试接口
package com.ymy.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Slf4j
public class TestController {
@RequestMapping(value = "/test",method = RequestMethod.GET)
public String test(){
System.out.println("这是控制台日志!");
log.info("这是输出到文件的日志");
return "HELLO-BUG!!!!!!!!!!";
}
}
在resources目录下创建log4j2.xml文件主要看这两个参数:server:
port: 9999
到这里springboot项目就创建完成了,我们现在启动一下项目。
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.5.RELEASE)
15:29:19.386 [main] INFO com.ymy.SpringBootDockerApplication - Starting SpringBootDockerApplication on LAPTOP-3GLHJRE9 with PID 20652 (D:\springboot\spring-boot-docker\target\classes started by admin in D:\springboot)
15:29:19.395 [main] INFO com.ymy.SpringBootDockerApplication - No active profile set, falling back to default profiles: default
15:29:20.183 [main] INFO org.springframework.boot.web.embedded.tomcat.TomcatWebServer - Tomcat initialized with port(s): 9999 (http)
15:29:20.200 [main] INFO org.apache.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler ["http-nio-9999"]
15:29:20.201 [main] INFO org.apache.catalina.core.StandardService - Starting service [Tomcat]
15:29:20.201 [main] INFO org.apache.catalina.core.StandardEngine - Starting Servlet engine: [Apache Tomcat/9.0.31]
15:29:20.309 [main] INFO org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/] - Initializing Spring embedded WebApplicationContext
15:29:20.309 [main] INFO org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 881 ms
15:29:20.452 [main] INFO org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor - Initializing ExecutorService 'applicationTaskExecutor'
15:29:20.568 [main] INFO org.apache.coyote.http11.Http11NioProtocol - Starting ProtocolHandler ["http-nio-9999"]
15:29:20.596 [main] INFO org.springframework.boot.web.embedded.tomcat.TomcatWebServer - Tomcat started on port(s): 9999 (http) with context path ''
15:29:20.599 [main] INFO com.ymy.SpringBootDockerApplication - Started SpringBootDockerApplication in 1.664 seconds (JVM running for 4.04)
如果看到这个页面表示springboot项目没有问题,现在我们将项目拷贝到服务器中。
mvn clean package docker:build
执行完之后将会看到
docker images
docker run --name springbooot-docker -p 9999:9999 -d 4a2
run:运行的意思–name:指定镜像启动的之后的名称-p:容器和外部的端口映射 第一个端口:外部 第二个端口:内部-d:后台运行 -t:实时运行,窗口关闭,程序结束。4a2:表示镜像的id(IMAGE ID)前3位,这里的id并不需要输入全称,只需要输入前几个就行,有一个前提:当有很多镜像的时候,前面几个字符就有可能会相同,这个时候就需要多输入几位,直到不相同位置。启动了镜像之后我怎么知道有没有成功呢?这个时候我们就需要一个命令来查看已经启动的任务
docker ps
docker ps
找到容器的id
docker logs -f
tail -100f info.log
docker exec -it ca2cd59fff9b /bin/bash
ca2cd59fff9b:容器id然后再切换到我们日志的输出路径
cd work/spring-boot-docker
tail -100f info.log
exit
热门内容:
- 为什么 Java 中“1000==1000”为false,而”100==100“为true?
- 面试官问我:如何设计 QQ、微信等第三方账号登陆 ?还要我说出数据库表设计!
- 为什么MySQL不推荐使用uuid或者雪花id作为主键?
- 炫酷,Spring Boot + ECharts 实现用户访问地图可视化(附源码)
- 字节跳动一面:i++ 是线程安全的吗?
- 只需4步,自己搞个 Spring Boot Starter !
最近面试BAT,整理一份面试资料《Java面试BAT通关手册》,覆盖了Java核心技术、JVM、Java并发、SSM、微服务、数据库、数据结构等等。
获取方式:点“在看”,关注公众号并回复 666 领取,更多内容陆续奉上。
明天见(。・ω・。)微信扫一扫
关注该公众号