1.上传springboot的jar
root@cenots-7.5:/home/code#rz -----app.jar
2.编辑Dockerfile
root@cenots-7.5:/home/code#vi Dockerfile
内容
FROM openjdk:8
# 作者
MAINTAINER nnd
# 声明要使用的端口
EXPOSE 8080
# VOLUME 指定了临时文件目录为/tmp。
# 将本地包添加到容器中并更名为容器内目录名
ADD /app.jar /app.jar
ADD /application.yml /application.yml
#挂载卷
# 同步容器与本机时间
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo 'Asia/Shanghai' >/etc/timezone
EXPOSE 9933
#ENTRYPOINT命令用于指定这个容器启动的时候要运行的命令(可以追加命令)
ENTRYPOINT ["java","-jar","app.jar"]
3.制作Docker镜像
root@cenots-7.5:/home/code#docker build -t hello:1.0 .
[+] Building 1.5s (9/9) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 578B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/openjdk:8 0.9s
=> CACHED [1/4] FROM docker.io/library/openjdk:8@sha256:86e863cc57215cfb181bd319736d0baf62 0.0s
=> [internal] load build context 0.1s
=> => transferring context: 19.18MB 0.1s
=> [2/4] ADD /app.jar /app.jar 0.1s
=> [3/4] ADD /application.yml /application.yml 0.0s
=> [4/4] RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' > 0.3s
=> exporting to image 0.1s
=> => exporting layers 0.1s
=> => writing image sha256:9f826b92d5b2142b92a6977f4728ea4cc93a6a2847a7314c07f92b5ae9fb932 0.0s
=> => naming to docker.io/library/hello:1.0
4.查看并启动容器
root@cenots-7.5:/home/code#docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello 1.0 9f826b92d5b2 7 seconds ago 545MB
postgres 12 0d56839ac0df 3 weeks ago 412MB
redis latest 7c4b517da47d 2 months ago 153MB
openzipkin/zipkin latest 7fb470c13693 4 months ago 165MB
postgres latest 1921dda0e2c5 5 months ago 412MB
redis 6.2.10-alpine a470c0525301 9 months ago 27.1MB
sonarqube 8.9.10-community 4009139ebd19 11 months ago 490MB
seataio/seata-server 1.5.2 f6a5368b6720 16 months ago 186MB
nacos/nacos-server 2.0.3 433eb51fef8d 2 years ago 1.05GB
seataio/seata-server 1.4.2 4909c4c024dc 2 years ago 156MB
5.启动容器
root@cenots-7.5:/home/code#docker run -d --name h1 -p 9666:9933 hello:1.0
33ef6638f3dddcbfbce243140ada085148bc127f783fb8d7f10c9a4ca1245265
root@cenots-7.5:/home/code#docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
33ef6638f3dd hello:1.0 "java -jar app.jar" 7 seconds ago Up 6 seconds 8080/tcp, 0.0.0.0:9666->9933/tcp, :::9666->9933/tcp h1
be0f46c5450a seataio/seata-server:1.4.2 "java -Djava.securit..." 4 months ago Up 2 weeks 0.0.0.0:8091->8091/tcp, :::8091->8091/tcp seata-server
e5e9b616802c nacos/nacos-server:2.0.3 "bin/docker-startup...." 5 months ago Up 2 weeks 0.0.0.0:8848->8848/tcp, :::8848->8848/tcp, 0.0.0.0:9848->9848/tcp, :::9848->9848/tcp nacos203
85c7e7acf404 redis:6.2.10-alpine "docker-entrypoint.s..." 9 months ago Up 2 weeks 0.0.0.0:56379->6379/tcp, :::56379->6379/tcp
6.调用服务接口http://ip:9666/login 查看日志
root@cenots-7.5:/home/code#docker logs -f h1
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.2.RELEASE)
2023-12-01 13:46:07.309 INFO 1 --- [ main] org.example.App : Starting App v1.0-SNAPSHOT on 33ef6638f3dd with PID 1 (/app.jar started by root in /)
2023-12-01 13:46:07.312 INFO 1 --- [ main] org.example.App : No active profile set, falling back to default profiles: default
2023-12-01 13:46:08.434 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 9933 (http)
2023-12-01 13:46:08.464 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2023-12-01 13:46:08.464 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.14]
2023-12-01 13:46:08.476 INFO 1 --- [ main] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib]
2023-12-01 13:46:08.549 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2023-12-01 13:46:08.549 INFO 1 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1178 ms
2023-12-01 13:46:08.763 INFO 1 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2023-12-01 13:46:09.051 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 9933 (http) with context path ''
2023-12-01 13:46:09.055 INFO 1 --- [ main] org.example.App : Started App in 2.177 seconds (JVM running for 2.56)
2023-12-01 13:47:43.656 INFO 1 --- [nio-9933-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2023-12-01 13:47:43.657 INFO 1 --- [nio-9933-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2023-12-01 13:47:43.663 INFO 1 --- [nio-9933-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 6 ms
User{user='213123123', pwd='213123123'}
已打印登录信息,启动成功完成。
IDEA插件方式
首先把docker服务的远程端口打开
[root@localhost ~]# vim /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd-current -H unix:///var/run/docker.sock -H tcp://0.0.0.0:2375 \
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker
其他扩展知识 docker-compose