1. 新建DockerFile,写入内容
vi DockerFile
# 指定基础镜像,用于后续的指令构建。
FROM openjdk:17-jdk-alpine
# 在容器内部设置环境变量。
ENV MYPATH /root/ta
# 设置后续指令的工作目录。
WORKDIR $MYPATH
#将文件、目录或远程URL复制到镜像中。
ADD ta-0.0.1-SNAPSHOT.jar ta-0.0.1-SNAPSHOT.jar
#暴露端口
EXPOSE 8080
# 运行jar包
CMD java -jar ta-0.0.1-SNAPSHOT.jar
#定义系统环境变量
ENV username=ta
ENV password=123456
2. DockerFile创建镜像(当前目录保证只有DockerFile 和 jar包)
docker build -f DockerFile -t ta:1.0.0 .
3. 运行容器
docker run -d --net host --name ta ta:1.0.0
查看容器日志
[root@ETL027207 tmp]# docker logs -f --tail 200 ta
Standard Commons Logging discovery in action with spring-jcl: please remove commons-logging.jar from classpath in order to avoid potential conflicts
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.2.0)
2023-12-27T07:58:45.248Z INFO 1 --- [ main] com.ta.TaApplication : Starting TaApplication v0.0.1-SNAPSHOT using Java 17-ea with PID 1 (/root/ta/ta-0.0.1-SNAPSHOT.jar started by root in /root/ta)
2023-12-27T07:58:45.265Z INFO 1 --- [ main] com.ta.TaApplication : No active profile set, falling back to 1 default profile: "default"
2023-12-27T07:58:48.641Z INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http)
2023-12-27T07:58:48.656Z INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2023-12-27T07:58:48.657Z INFO 1 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.16]
2023-12-27T07:58:48.922Z INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2023-12-27T07:58:48.936Z INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3398 ms
Standard Commons Logging discovery in action with spring-jcl: please remove commons-logging.jar from classpath in order to avoid potential conflicts
2023-12-27T07:58:51.107Z INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path ''
2023-12-27T07:58:51.125Z INFO 1 --- [ main] com.ta.TaApplication : Started TaApplication in 6.838 seconds (process running for 7.659)
2023-12-27T07:59:41.424Z INFO 1 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2023-12-27T07:59:41.424Z INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2023-12-27T07:59:41.426Z INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 2 ms
^C
ok 运行成功