Dockerfile介绍使用
今天主要是学习Docker的第三步,了解Dockerfile使用并通过Dockerfile发布测试项目;接下来依旧会持续更新Docker的高级部分,感兴趣的可以关注;
1、Dockerfile简介
首先,我们需要带着以下几个问题去学习,通过以下三个问题,可以更直观的掌握Dockerfile:
1.是什么?
2.为什么要使用它?
3.如何使用?
1.1、Dockerfile 是什么?
Dockerfile 是一个文本文件,包含一系列用于构建镜像的指令(Instructions)。每条指令都会构建一层镜像,指令的内容描述了该层镜像应如何构建。
1、Dockerfile 用于构建Docker镜像
2、纯文本文件
1.2、为什么要使用Dockerfile?
先思考一个问题,Docker Hub 官方提供了许多满足我们服务需求的镜像,为什么还需要自定义镜像?
其实,Dockerfile对我们开发人员的作用有两点:
1. 将我们自己开发的应用程序打包成镜像,从而在容器中运行(核心作用)
2. 对已经存在的镜像镜像扩展,打包成适合生产环境的应用镜像。
1.3、如何使用Dockerfile?
在使用前,我们得详细了解Dockerfile中的命令
2、Dockerfile实战
2.1、创建一个springboot项目
2.2、编写简单的测试程序
package com.zw.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@GetMapping("/hello")
public String helloDockerFile(){
return "hello,Dockerfile!!!!!";
}
}
2.3、本地测试程序
2.4、Maven打包,通过jar包测试程序是否正常
E:\java_project\dockerfile_test\target>java -jar dockerfile_test-0.0.1-SNAPSHOT.jar
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.14)
2025-01-01 10:19:10.262 INFO 6768 --- [ main] com.zw.DockerfileTestApplication : Starting DockerfileTestApplication v0.0.1-SNAPSHOT using Java 1.8.0_231 on zhangwu with PID 6768 (E:\java_project\dockerfile_test\target\dockerfile_test-0.0.1-SNAPSHOT.jar started by zhangwu in E:\java_project\dockerfile_test\target)
2025-01-01 10:19:10.264 INFO 6768 --- [ main] com.zw.DockerfileTestApplication : No active profile set, falling back to 1 default profile: "default"
2025-01-01 10:19:10.866 INFO 6768 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8081 (http)
2025-01-01 10:19:10.874 INFO 6768 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2025-01-01 10:19:10.875 INFO 6768 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.63]
2025-01-01 10:19:10.940 INFO 6768 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2025-01-01 10:19:10.940 INFO 6768 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 647 ms
2025-01-01 10:19:11.167 INFO 6768 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8081 (http) with context path ''
2025-01-01 10:19:11.175 INFO 6768 --- [ main] com.zw.DockerfileTestApplication : Started DockerfileTestApplication in 1.189 seconds (JVM running for 1.462)
测试结果:
C:\Users\zhang>curl localhost:8081/hello
hello,Dockerfile!!!!!
C:\Users\zhang>
2.5、编写Dockerfile文件
FROM java:8
COPY *.jar /app.jar
EXPOSE 8081
CMD ["--server.port=8081"]
ENTRYPOINT ["java","-jar","/app.jar"]
2.6、上传Dockerfile文件、jar包
2.7、构建Docker镜像
docker build -f Dockerfile -t dockerfile_test_project:1.0.0 .
2.7、运行Docker镜像
docker run -d --name dockerfile_test_project -p 8081:8081 0a7ba966e337
2.8、测试结果
注意:
如果访问失败,请检查下阿里云安全组是否开放了该端口
本篇完结(下一篇,Docker网络)