先说下大概流程,我这边使用SpringBoot构建项目,直接打成Jar包,运行在Linux上的8082端口,将Jar包名字修改为docker-package.jar,把Jar包上传后在同级目录下创建Dockerfile文件(没有后缀),然后用 docker build [OPTIONS] PATH 指令将Jar包打成Docker镜像。
让我们开始吧。
项目代码很简单,只需要提供一个Get接口来查看调用情况就行了。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/docker")
@SpringBootApplication
public class DockerApplication {
public static void main(String[] args) {
SpringApplication.run(DockerApplication.class, args);
}
@GetMapping("/get")
public String getData() {
return "调用成功:" + System.currentTimeMillis();
}
}
我在项目内加入了一个Get接口:/docker/get
避免端口冲突,再将项目端口修改为8082
application.properties内容如下:
server.port=8082
然后我将Jar包名称指定为:docker-package,只需要在pom.xml中配置finalName就行了。
org.springframework.boot
spring-boot-maven-plugin
docker-package
项目 package,得到docker-package.jar
我们将docker-package.jar上传至Linux,同级目录下创建Dockerfile(没有后缀)
Dockerfile内容大致如下:
[root@instance-g1almmmy docker]# cat Dockerfile
FROM openjdk:8-jdk-alpine
VOLUME /tmp
COPY docker-package.jar docker-package.jar
ENTRYPOINT ["java","-jar","/docker-package.jar"]
FROM openjdk:8-jdk-alpine:使用 openjdk:8-jdk-alpine 环境创建
VOLUME /tmp:定义匿名数据卷。在启动容器时忘记挂载数据卷,会自动挂载到匿名卷。
COPY docker-package.jar docker-package.jar:复制指令,从上下文目录中复制文件或者目录到容器里指定路径。(前面的docker-package.jar是宿主机上的路径,后面的docker-package.jar是复制到容器内的路径)
ENTRYPOINT [“java”,"-jar","/docker-package.jar"]:执行 docker run 的时候可以指定 ENTRYPOINT 运行所需的参数。
更多相关参数,可以查看:Docker Dockerfile
运行指令:docker build -t docker-package:v0617 .
注意后面还有一个"."
-t: 镜像的名字及标签,通常 name:tag
有如下输出,说明镜像制作成功了
Sending build context to Docker daemon 55.1MB
Step 1/4 : FROM openjdk:8-jdk-alpine
---> a3562aa0b991
Step 2/4 : VOLUME /tmp
---> Using cache
---> f2dea56e2ab9
Step 3/4 : COPY docker-package.jar docker-package.jar
---> 44e15cb5c397
Removing intermediate container bb38d5b87128
Step 4/4 : ENTRYPOINT java -jar /docker-package.jar
---> Running in bc8524326d20
---> e023becb7df6
Removing intermediate container bc8524326d20
Successfully built e023becb7df6
Successfully tagged docker-package:v0617
查看docker镜像
[root@instance-g1almmmy docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker-package v0617 e023becb7df6 14 minutes ago 132MB
运行docker镜像
[root@instance-g1almmmy docker]# docker run -d -p 8082:8082 e023becb7df6
39afe42888d2942de572dff19b6e86786a3d0541707521318d6bb894a9009950
-d:后台运行
-p:指定运行端口 宿主机:容器内部端口
39afe42888d2942de572dff19b6e86786a3d0541707521318d6bb894a9009950:容器ID,运行成功后生成,一会查看日志和端口需要用到。
查看docker容器日志
[root@instance-g1almmmy docker]# docker logs 39afe42888d2942de572dff19b6e86786a3d0541707521318d6bb894a9009950
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.9.RELEASE)
2020-06-17 15:15:20.209 INFO 1 --- [ main] com.example.demo.DockerApplication : Starting DockerApplication v0.0.1-SNAPSHOT on 39afe42888d2 with PID 1 (/docker-package.jar started by root in /)
2020-06-17 15:15:20.217 INFO 1 --- [ main] com.example.demo.DockerApplication : No active profile set, falling back to default profiles: default
2020-06-17 15:15:22.869 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8082 (http)
2020-06-17 15:15:22.981 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-06-17 15:15:22.982 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.26]
2020-06-17 15:15:23.154 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-06-17 15:15:23.154 INFO 1 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2798 ms
2020-06-17 15:15:23.672 INFO 1 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-06-17 15:15:24.348 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8082 (http) with context path ''
2020-06-17 15:15:24.357 INFO 1 --- [ main] com.example.demo.DockerApplication : Started DockerApplication in 5.408 seconds (JVM running for 6.408)
运行成功
查看端口
docker port 39afe42888d2942de572dff19b6e86786a3d0541707521318d6bb894a9009950
8082/tcp -> 0.0.0.0:8082
说明docker容器的8082已经映射出来了
启动完成