docker部署springboot项目

一.打jar包

为了使jar包能够直接运行,需要在pom里面加上一些配置

The plugin rewrites your manifest, and in particular it manages the Main-Class and Start-Class entries. If the defaults don’t work you have to configure the values in the Spring Boot plugin, not in the jar plugin. The Main-Class in the manifest is controlled by the layout property of the Spring Boot plugin, as shown in the following example:

意思是说:一般加上下面的就行了。如果不起作用,需要自己配置

1
2
3
4
5
6
7
8
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

自己配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>${start.class}</mainClass>
<layout>JAR</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

只用自己配一下启动类:

比如:org.xxx.xxx.xxxApplication

然后用maven去package一下获得jar包

二.写Dockerfile

把这个jar包放到任意一个文件夹下

在这个文件夹下创建Dockerfile文件

1
2
3
4
5
FROM java:8
VOLUME /tmp
ADD CSS-core-1.0-SNAPSHOT.jar css.jar
EXPOSE 8088
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/css.jar"]

三.创建镜像

1
docker build - 镜像名 . //Dockerfile上下文路径

镜像名必须为小写

[root@iZbp18425116ezu5mwz33wZ competition-service-system]# docker build -t CSS .
invalid argument "CSS" for "-t, --tag" flag: invalid reference format: repository name must be lowercase
See 'docker build --help'.

四.创建容器

常用命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
列出镜像
docker images
删除镜像
docker image rm 镜像ID //常用
查看容器状态
docker ps //查看运行的容器
docker ps –a //查看所有的容器(包含运行和退出)
启动容器

-d 后台运⾏容器
--rm 容器在启动后,执⾏完成命令或程序后就销毁
--name 给容器起⼀个⾃定义名称

docker run --rm -d --name tomcat1 -p 8080:8080 tomcat
停止容器
docker stop 容器ID
删除容器
docker rm 容器ID
进入容器
docker exec -it 容器ID(Names) bash
查看日志
docker logs 容器名称/ID