试用docker搭建java环境
本篇仅做探索,主要解决以下问题:
- 无需搭建Java开发环境
- 开发环境变化只需更新镜像即可(比如从Java8改为Java9)
- 无需安装IDE(比如eclipse)
- 提供一个极简Demo
编写Hello world!
按照码农协会行业定律,初学必写Hello World,以表达对编程世界的敬仰之情。如下面代码所示:
//引入命名空间
import java.util.*;
//类
public class Hello{
//程序入口
public static void main(String[] args){
//打印字符串(控制台)
System.out.println("Hello World!");
//打印当前时间
System.out.println(new Date());
}
}
编写Dockerfile
如下所示,示例Dockerfile文件如下:
# 基于Java 9
FROM java:9
# 设置工作目录
WORKDIR /app
# 复制文件到工作目录
COPY . /app
# 设置Java环境变量
ENV PATH=$PATH:$JAVA_HOME/bin
ENV JRE_HOME=${JAVA_HOME}/jre
ENV CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
# 编译
RUN ["/usr/lib/jvm/java-9-openjdk-amd64/bin/javac","HYTCGW-0.0.1-SNAPSHOT.java"]
# 运行
ENTRYPOINT ["/usr/lib/jvm/java-9-openjdk-amd64/bin/java", "HYTCGW-0.0.1-SNAPSHOT"]
构建镜像并执行
接下来,我们可以执行以下命令以构建Docker镜像:
docker build --rm -f "Dockerfile" -t java-hello:latest .
[root@VM-0-2-centos byp]# docker build --rm -f "Dockerfile" -t java-hello:latest .
Sending build context to Docker daemon 73.04MB
Step 1/4 : FROM java:8
8: Pulling from library/java
5040bd298390: Pull complete
fce5728aad85: Pull complete
76610ec20bf5: Pull complete
60170fec2151: Pull complete
e98f73de8f0d: Pull complete
11f7af24ed9c: Pull complete
49e2d6393f32: Pull complete
bb9cdec9c7f3: Pull complete
Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d
Status: Downloaded newer image for java:8
---> d23bdf5b1b1b
Step 2/4 : VOLUME /tmp
---> Running in a5c7360ff9a4
Removing intermediate container a5c7360ff9a4
---> c89956ff9940
Step 3/4 : ADD HYTCGW-0.0.1-SNAPSHOT.jar /hello.jar
---> 7bab1ffa8784
Step 4/4 : ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/hello.jar"]
---> Running in c8e797c8f54f
Removing intermediate container c8e797c8f54f
---> 7faf675bd16c
Successfully built 7faf675bd16c
Successfully tagged java-hello:latest
构建成功后,我们跑起来看效果:
docker run java-hello:latest
[root@VM-0-2-centos byp]# docker run java-hello:latest
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.4.RELEASE)
2020-08-12 02:31:10.312 INFO 1 --- [ main] com.hytc.HytcgwApplication : Starting HytcgwApplication v0.0.1-SNAPSHOT on c3e03747f1c5 with PID 1 (/hello.jar started by root in /)
2020-08-12 02:31:10.328 DEBUG 1 --- [ main] com.hytc.HytcgwApplication : Running with Spring Boot v2.1.4.RELEASE, Spring v5.1.6.RELEASE
2020-08-12 02:31:10.339 INFO 1 --- [ main] com.hytc.HytcgwApplication : The following profiles are active: test
2020-08-12 02:31:13.168 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2020-08-12 02:31:13.183 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2020-08-12 02:31:13.233 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 30ms. Found 0 repository interfaces.
2020-08-12 02:31:14.141 INFO 1 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$86cebf02] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-08-12 02:31:15.152 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 80 (http)
2020-08-12 02:31:15.203 INFO 1 --- [ main] o.a.coyote.http11.Http11NioProtocol : Initializing ProtocolHandler ["http-nio-80"]
2020-08-12 02:31:15.262 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-08-12 02:31:15.262 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.17]
2020-08-12 02:31:15.459 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-08-12 02:31:15.459 INFO 1 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 4960 ms
2020-08-12 02:31:16.011 INFO 1 --- [ main] c.a.d.s.b.a.DruidDataSourceAutoConfigure : Init DruidDataSource
2020-08-12 02:31:26.939 INFO 1 --- [ main] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} inited
2020-08-12 02:31:29.256 INFO 1 --- [ main] pertySourcedRequestMappingHandlerMapping : Mapped URL path [/v2/api-docs] onto method [public org.springframework.http.ResponseEntity springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest)]
2020-08-12 02:31:29.643 INFO 1 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-08-12 02:31:30.639 INFO 1 --- [ main] org.quartz.impl.StdSchedulerFactory : Using default implementation for ThreadExecutor
2020-08-12 02:31:30.762 INFO 1 --- [ main] org.quartz.core.SchedulerSignalerImpl : Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
2020-08-12 02:31:30.762 INFO 1 --- [ main] org.quartz.core.QuartzScheduler : Quartz Scheduler v.2.3.0 created.
2020-08-12 02:31:30.763 INFO 1 --- [ main] org.quartz.simpl.RAMJobStore : RAMJobStore initialized.
2020-08-12 02:31:30.765 INFO 1 --- [ main] org.quartz.core.QuartzScheduler : Scheduler meta-data: Quartz Scheduler (v2.3.0) 'quartzScheduler' with instanceId 'NON_CLUSTERED'
Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
NOT STARTED.
Currently in standby mode.
Number of jobs executed: 0
Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.
2020-08-12 02:31:30.773 INFO 1 --- [ main] org.quartz.impl.StdSchedulerFactory : Quartz scheduler 'quartzScheduler' initialized from an externally provided properties instance.
2020-08-12 02:31:30.773 INFO 1 --- [ main] org.quartz.impl.StdSchedulerFactory : Quartz scheduler version: 2.3.0
2020-08-12 02:31:30.774 INFO 1 --- [ main] org.quartz.core.QuartzScheduler : JobFactory set to: org.springframework.scheduling.quartz.SpringBeanJobFactory@1c481ff2
2020-08-12 02:31:30.918 INFO 1 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Context refreshed
2020-08-12 02:31:30.961 INFO 1 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Found 1 custom documentation plugin(s)
2020-08-12 02:31:31.031 INFO 1 --- [ main] s.d.s.w.s.ApiListingReferenceScanner : Scanning for api listing references
2020-08-12 02:31:31.698 INFO 1 --- [ main] o.s.s.quartz.SchedulerFactoryBean : Starting Quartz Scheduler now
2020-08-12 02:31:31.698 INFO 1 --- [ main] org.quartz.core.QuartzScheduler : Scheduler quartzScheduler_$_NON_CLUSTERED started.
2020-08-12 02:31:31.724 INFO 1 --- [ main] o.a.coyote.http11.Http11NioProtocol : Starting ProtocolHandler ["http-nio-80"]
2020-08-12 02:31:31.884 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 80 (http) with context path ''
2020-08-12 02:31:31.893 INFO 1 --- [ main] com.hytc.HytcgwApplication : Started HytcgwApplication in 23.154 seconds (JVM running for 24.065)
履正律师PC端接口
docker运行java项目
[root@VM-0-2-centos byp]#
[root@VM-0-2-centos byp]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
25c17147a26f java-hello "java -Djava.securit…" 50 seconds ago Up 50 seconds happy_lederberg
13e442ccdff6 mysql "docker-entrypoint.s…" 18 hours ago Up 18 hours 33060/tcp, 0.0.0.0:33060->3306/tcp my-mysql
[root@VM-0-2-centos byp]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
25c17147a26f java-hello "java -Djava.securit…" 54 seconds ago Up 53 seconds happy_lederberg
a41005cb1226 java-hello "java -Djava.securit…" 17 minutes ago Exited (143) 2 minutes ago naughty_khayyam
c3e03747f1c5 java-hello:latest "java -Djava.securit…" 28 minutes ago Exited (143) 19 minutes ago clever_maxwell
13e442ccdff6 mysql "docker-entrypoint.s…" 18 hours ago Up 18 hours 33060/tcp, 0.0.0.0:33060->3306/tcp my-mysql
[root@VM-0-2-centos byp]# docker stop 25c17147a26f
25c17147a26f
[root@VM-0-2-centos byp]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
13e442ccdff6 mysql "docker-entrypoint.s…" 18 hours ago Up 18 hours 33060/tcp, 0.0.0.0:33060->3306/tcp my-mysql
[root@VM-0-2-centos byp]# docker run -d -p 80:80 java-hello
934fbb9c86fac774e7aa02f2cc9f302525f0dc6e6aaf65e2d8eefe737fb8f359
[root@VM-0-2-centos byp]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
934fbb9c86fa java-hello "java -Djava.securit…" 4 seconds ago Up 3 seconds 0.0.0.0:80->80/tcp wonderful_zhukovsky
13e442ccdff6 mysql "docker-entrypoint.s…" 18 hours ago Up 18 hours 33060/tcp, 0.0.0.0:33060->3306/tcp my-mysql
[root@VM-0-2-centos byp]# docker logs 934fbb9c86fac774e7aa02f2cc9f302525f0dc6e6aaf65e2d8eefe737fb8f359
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.4.RELEASE)
2020-08-12 03:02:44.664 INFO 1 --- [ main] com.hytc.HytcgwApplication : Starting HytcgwApplication v0.0.1-SNAPSHOT on 934fbb9c86fa with PID 1 (/hello.jar started by root in /)
2020-08-12 03:02:44.667 DEBUG 1 --- [ main] com.hytc.HytcgwApplication : Running with Spring Boot v2.1.4.RELEASE, Spring v5.1.6.RELEASE
2020-08-12 03:02:44.667 INFO 1 --- [ main] com.hytc.HytcgwApplication : The following profiles are active: test
2020-08-12 03:02:47.298 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2020-08-12 03:02:47.307 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2020-08-12 03:02:47.378 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 49ms. Found 0 repository interfaces.
2020-08-12 03:02:48.480 INFO 1 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$26d67542] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-08-12 03:02:49.349 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 80 (http)
2020-08-12 03:02:49.405 INFO 1 --- [ main] o.a.coyote.http11.Http11NioProtocol : Initializing ProtocolHandler ["http-nio-80"]
2020-08-12 03:02:49.423 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-08-12 03:02:49.423 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.17]
2020-08-12 03:02:49.617 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-08-12 03:02:49.617 INFO 1 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 4789 ms
2020-08-12 03:02:50.123 INFO 1 --- [ main] c.a.d.s.b.a.DruidDataSourceAutoConfigure : Init DruidDataSource
2020-08-12 03:02:50.872 INFO 1 --- [ main] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} inited
2020-08-12 03:02:53.079 INFO 1 --- [ main] pertySourcedRequestMappingHandlerMapping : Mapped URL path [/v2/api-docs] onto method [public org.springframework.http.ResponseEntity springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest)]
2020-08-12 03:02:53.520 INFO 1 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-08-12 03:02:54.479 INFO 1 --- [ main] org.quartz.impl.StdSchedulerFactory : Using default implementation for ThreadExecutor
2020-08-12 03:02:54.553 INFO 1 --- [ main] org.quartz.core.SchedulerSignalerImpl : Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
2020-08-12 03:02:54.553 INFO 1 --- [ main] org.quartz.core.QuartzScheduler : Quartz Scheduler v.2.3.0 created.
2020-08-12 03:02:54.555 INFO 1 --- [ main] org.quartz.simpl.RAMJobStore : RAMJobStore initialized.
2020-08-12 03:02:54.556 INFO 1 --- [ main] org.quartz.core.QuartzScheduler : Scheduler meta-data: Quartz Scheduler (v2.3.0) 'quartzScheduler' with instanceId 'NON_CLUSTERED'
Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
NOT STARTED.
Currently in standby mode.
Number of jobs executed: 0
Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.
2020-08-12 03:02:54.556 INFO 1 --- [ main] org.quartz.impl.StdSchedulerFactory : Quartz scheduler 'quartzScheduler' initialized from an externally provided properties instance.
2020-08-12 03:02:54.557 INFO 1 --- [ main] org.quartz.impl.StdSchedulerFactory : Quartz scheduler version: 2.3.0
2020-08-12 03:02:54.557 INFO 1 --- [ main] org.quartz.core.QuartzScheduler : JobFactory set to: org.springframework.scheduling.quartz.SpringBeanJobFactory@29ca3d04
2020-08-12 03:02:54.721 INFO 1 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Context refreshed
2020-08-12 03:02:54.757 INFO 1 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Found 1 custom documentation plugin(s)
2020-08-12 03:02:54.841 INFO 1 --- [ main] s.d.s.w.s.ApiListingReferenceScanner : Scanning for api listing references
2020-08-12 03:02:55.434 INFO 1 --- [ main] o.s.s.quartz.SchedulerFactoryBean : Starting Quartz Scheduler now
2020-08-12 03:02:55.434 INFO 1 --- [ main] org.quartz.core.QuartzScheduler : Scheduler quartzScheduler_$_NON_CLUSTERED started.
2020-08-12 03:02:55.450 INFO 1 --- [ main] o.a.coyote.http11.Http11NioProtocol : Starting ProtocolHandler ["http-nio-80"]
2020-08-12 03:02:55.492 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 80 (http) with context path ''
2020-08-12 03:02:55.495 INFO 1 --- [ main] com.hytc.HytcgwApplication : Started HytcgwApplication in 12.444 seconds (JVM running for 13.393)
履正律师PC端接口
[root@VM-0-2-centos byp]#
访问项目的swagger http://132.232.25.195/doc.html
方式二 https://blog.csdn.net/qq_37141773/article/details/100080240
一、docker的安装
docker是近年比较火的一个开源的应用容器引擎,咱看半天也没太清楚到底有啥用,反正就是感觉像是一个linux的微缩版,这里暂不讨论,有些东西用着用着就知道他的好处及用途了,只是看的话很难真正理解。
PS补充:经过使用后,我目前是这样理解的:docker就是一个虚拟机,其中有三个概念:仓库,镜像,容器。仓库就类似于你ide的工作空间,镜像就相当于工作空间中未启动的项目,而容器则是项目启动的一个实例。镜像一启动,就会创建一个容器,启动几次就会创建几个容器。
我的服务器购买时选择的操作系统是CentOS,所以这里的安装环境也是CentOS
docker安装的话有两种方式,这里我们使用比较方便的yum安装
安装一些必要的系统工具:PS:sudo表示以管理员权限执行,如是root用户(最高权限)可不输入
$ sudo yum install -y yum-utils device-mapper-persistent-data lvm2
添加软件源信息:
$ sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
更新 yum 缓存:
$ sudo yum makecache fast
安装 Docker-ce:
$ sudo yum -y install docker-ce
启动 Docker 后台服务
$ sudo systemctl start docker
测试运行 hello-world
$ docker run hello-world
出现下面界面代表docker安装成功了!
二、部署Springboot项目
我这里是新建了一个最简单的springboot项目
1.我们使用idea的maven插件将项目打包,也可以用其他方式打包,在此不赘述。
2.打包后编写Dockerfile文件,创建文件名为Dockerfile的文本文件,内容如下:
# 基于Java 8
FROM java:8
# 指向了一个/app,由于Spring Boot使用内置的Tomcat容器,Tomcat默认使用/tmp作为工作目录。效果就是在主机的/var/lib/docker目录下创建了一个临时文件,并连接到容器的/root。
VOLUME /app
#ADD将项目的jar文件作为app.jar添加到容器
ADD HYTCGW-0.0.1-SNAPSHOT.jar lzlsadmin.jar
# 设置Java环境变量
ENV PATH=$PATH:$JAVA_HOME/bin
ENV JRE_HOME=${JAVA_HOME}/jre
ENV CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
# 编译 RUN表示在新创建的镜像中执行一些命令,然后把执行的结果提交到当前镜像。这里使用touch命令来改变文件的修改时间,Docker创建的所有容器文件默认状态都是“未修改”。这对于简单应用来说不需要,不过对于一些静态内容(比如:index.html)的文件就需要一个“修改时间”。
RUN bash -c 'touch /lzlsadmin.jar'
#EXPOSE 容器暴露端口
EXPOSE 8080
# 运行 ENTRYPOINT 应用启动命令 参数设定
#ENTRYPOINT ["java", "-jar", "lzlsadmin.jar"]
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/lzlsadmin.jar"]
java:8是指Docker Hub上官方提供的java镜像,版本号是8也就是jdk1.8,有了这个基础镜像后,Dockerfile可以通过FROM指令直接获取它的状态——也就是在容器中java是已经安装的,接下来通过自定义的命令来运行Spring Boot应用:
VOLUME指向了一个/root的目录,由于Spring Boot使用内置的Tomcat容器,Tomcat默认使用/tmp作为工作目录。效果就是在主机的/var/lib/docker目录下创建了一个临时文件,并连接到容器的/root。
ADD将项目的jar文件作为app.jar添加到容器
RUN表示在新创建的镜像中执行一些命令,然后把执行的结果提交到当前镜像。这里使用touch命令来改变文件的修改时间,Docker创建的所有容器文件默认状态都是“未修改”。这对于简单应用来说不需要,不过对于一些静态内容(比如:index.html)的文件就需要一个“修改时间”。
EXPOSE 容器暴露端口
ENTRYPOINT 容器启动时运行的命令,相当于我们在命令行中输入java -jar xxxx.jar,为了缩短 Tomcat 的启动时间,添加java.security.egd
的系统属性指向/dev/urandom
作为 ENTRYPOINT
3.将此Dockerfile文件与项目jar包使用Xftp软件上传到服务器上!
4.执行docker build命令,docker就会根据Dockerfile里你定义好的命令进行构建新的镜像。
[root@VM-0-2-centos LZLS_ADMIN]# docker build -t lzadmin-docker .
Sending build context to Docker daemon 73.04MB
Step 1/9 : FROM java:8
---> d23bdf5b1b1b
Step 2/9 : VOLUME /app
---> Running in 92015a2afac1
Removing intermediate container 92015a2afac1
---> 6d51a7c6fb02
Step 3/9 : ADD HYTCGW-0.0.1-SNAPSHOT.jar lzlsadmin.jar
---> cab5e82975b7
Step 4/9 : ENV PATH=$PATH:$JAVA_HOME/bin
---> Running in a00c1b223000
Removing intermediate container a00c1b223000
---> 2fb39dc258f7
Step 5/9 : ENV JRE_HOME=${JAVA_HOME}/jre
---> Running in 2aedf724f3b1
Removing intermediate container 2aedf724f3b1
---> 55fa06e4ae56
Step 6/9 : ENV CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
---> Running in 11a23aa23762
Removing intermediate container 11a23aa23762
---> 1eec4e75ca72
Step 7/9 : RUN bash -c 'touch /lzlsadmin.jar'
---> Running in 36b1edb20d2e
Removing intermediate container 36b1edb20d2e
---> 76d0d13b9b29
Step 8/9 : EXPOSE 8080
---> Running in 29ad0fe6d183
Removing intermediate container 29ad0fe6d183
---> 1ef58ad5e8e6
Step 9/9 : ENTRYPOINT ["java", "-jar", "lzlsadmin.jar"]
---> Running in d826450c35ab
Removing intermediate container d826450c35ab
---> de340c188f25
Successfully built de340c188f25
Successfully tagged lzadmin-docker:latest
[root@VM-0-2-centos LZLS_ADMIN]#
执行docker images,可以看到新建的镜像
[root@VM-0-2-centos LZLS_ADMIN]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
lzadmin-docker latest de340c188f25 7 minutes ago 789MB
hytclzls_admin latest 7faf675bd16c 25 hours ago 716MB
java-hello latest 7faf675bd16c 25 hours ago 716MB
redis latest 1319b1eaa0b7 7 days ago 104MB
mysql latest 0d64f46acfd1 8 days ago 544MB
java 8 d23bdf5b1b1b 3 years ago 643MB
[root@VM-0-2-centos LZLS_ADMIN]#
5.使用 指令:docker run -d -p 8080:8080 lzadmin-docker运行该springBoot项目
[root@VM-0-2-centos LZLS_ADMIN]# docker run -d -p 8080:8080 lzadmin-docker
a28f26f7fffd7c80e81614d84a7352e994ceada659bc4603d0c7617185aa377b
[root@VM-0-2-centos LZLS_ADMIN]#
最后测试一下:可以访问了哦
部署多个项目
docker ps可以查看当前创建的容器:
docker rm -f 容器id 可以删除容器
我们按照上述步骤再建立一个名为nginx-docker的镜像(取名为nginx-docker只是后期想接触nginx,这里没有任何关系)
现在有这两个准备部署的项目镜像了
我们分别向1001和1002为这两个镜像创建容器(选择1001和1002是为了大家可以访问进行测试,这两个端口我不会用):
其中-d代表后台运行,也可以换为-it,可以看到项目运行日志
现在访问看看
是没有问题的,大家也可以访问一下:http://106.53.90.29:1002/hello
查看日志
docker logs -f -t --tail 200 964bec0331e7 #--tail 行数 容器ID
在docker上部署springboot项目
关于docker的安装,网上有许多教程,这里就不在赘述,windows家庭版请使用dockertool,在linux上安装docker比windows要简单的多,本次部署的springboot项目会使用mysql,环境为centos7,本文记录了sql文件导入docker的mysql的方法
在docker上部署springboot项目
首先在springboot项目的pom.xml文件中添加Docker镜像名称:
springboot
在plugin中添加docker构建插件:
org.springframework.boot
spring-boot-maven-plugin
com.spotify
docker-maven-plugin
1.0.0
${docker.image.prefix}/${project.artifactId}
src/main/docker
/
${project.build.directory}
${project.build.finalName}.jar
接着使用maven install(可以使用maven命令行,也可以使用ide自带的maven)打包,创建Dockerfile文件,Dockerfile相当于说明书,告诉docker容器应该如何运行项目,规定项目运行的一些规则:
FROM java:8
VOLUME /tmp
ADD movie-0.0.1-SNAPSHOT.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
FROM:表示基础镜像,即运行环境
VOLUME:一个特别指定的目录,用于存储数据,该命令的作用是在/var/lib/docker创建一个名为tmp的目录,在开启redis服务时,需要特别指定redis的数据存储在哪个文件夹,此时这个命令就十分有用
ADD:拷贝文件并且重命名
EXPOSE:并不是真正的发布端口,这个只是容器部署人员与建立image的人员之间的交流,即建立image的人员告诉容器布署人员容器应该映射哪个端口给外界
ENTRYPOINT:容器启动时运行的命令,相当于我们在命令行中输入java -jar xxxx.jar,为了缩短 Tomcat 的启动时间,添加java.security.egd
的系统属性指向/dev/urandom
作为 ENTRYPOINT
创建docker文件夹,将jar包与Dockerfile移动到该文件夹,进入该文件夹,运行命令:
docker build -t docker .
此时会将该项目构建成一个image(镜像),运行命令:
docker images
可以查看对应的镜像信息:
运行镜像:
docker run -p 8080:8080 -d docker
-p表示端口映射,冒号左边为docker容器外的端口号,右边为容器内的端口号,-d表示后台运行 docker表示REPOSITORY的值
运行命令:
docker ps
可以查看运行的镜像:
至此,springboot项目部署完毕,由于我的项目需要使用mysql,即使我们在本机上安装了mysql,springboot项目是无法访问的,因为docker中运行的项目与本机是两套完全不同的环境,因此如果想要访问,可以在docker中部署mysql,也可以将项目的jdbc的url中的localhost改为本机的IP地址,本文采取第一种方式
在docker上安装mysql
首先在docker上安装mysql:
docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql
在后台启动mysql,并将密码设置为123456,如果我们本机上运行了mysql,可以将上述的-p参数改为3307:3306,或是关掉本地的mysql
查看容器的ID:
docker ps
记下容器的ID号
接下来将sql文件导入到docker的数据库,将movie.sql拷贝到容器的root目录中:
docker cp movie.sql 21db7dd6618e:\root
进入到运行容器的bash
docker exec -it 21db7dd6618e bash
在docker的mysql中创建movie数据库,
我的movie.sql放在root目录下,在root目录下运行命令:
mysql -uroot -p123456 -D movie
在centos7下数据库可能无法显式中文,使用下列命令:
mysql>set names utf8;
获得mysql的IP地址:
docker inspect 21db7dd6618e
将springboot中的JDBC更改为上述IP地址即可
链接zhi:
https://blog.csdn.net/dhaiuda/article/details/82228186