docker 部署springboot、dubbo项目
博客专区 > trntaken 的博客 > 博客详情
docker 部署springboot、dubbo项目
trntaken 发表于2个月前
docker 部署springboot、dubbo项目
  • 发表于 2个月前
  • 阅读 36
  • 收藏 2
  • 点赞 0
  • 评论 0

腾讯云 学生专属云服务套餐 10元起购>>>   

创建项目

1.创建一个springboot项目 pas。随便写一个Controller

	@RequestMapping(value = "/oauth")
	public void authentication(HttpServletRequest request,
			HttpServletResponse response) {

     。。。。。
 }

2.打包部署pas。部署到linux 。目录结构如下:

[root@iZotm71x017bylZ pas]# ll
total 80
drwxr-xr-x 2 root root  4096 Sep  3 16:41 config
drwxr-xr-x 2 root root  4096 Sep  2 18:59 lib
-rw-r--r-- 1 root root 67926 Aug 28 10:59 pas-1.0.0.jar

3.运行项目,看到pas 项目已经正常启动。

java -jar pas-1.0.0.jar  &
[1] 10390
[root@scss-tcsdev-tomcat1 pas]# 2017-09-03 17:15:22.066 [main] INFO  com.wtsd.pas.PasApplication - Starting PasApplication v1.0.0 on localhost with PID 10390 (/scss/pas/pas-1.0.0.jar started by root in /scss/pas)

 

4.对上述pas 整个项目进行打成tar.gz压缩包,为创建pas 镜像做准备。config目录下application.properties 和 logback.xml不需要打包进去。在下面文中,我们会把配置文件放在卷目录config下。

 [root@scss-tcsdev-tomcat1 pas]# tar -zcvf pas.tar.gz pas/

创建镜像

1.首先创建并进入/opt/pas 文件夹。创建Dockerfile 文件:

# This is  my first web project with Docker.
FROM openjdk:8-jdk-alpine
#将项目包复制到镜像/usr/local目录下
ADD pas.tar.gz /usr/local/
ENV JAVA_OPTS=""
#暴露84端口
EXPOSE 84
#切换工作目录
WORKDIR  /usr/local/pas/
ENTRYPOINT ["sh","-c","java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar  pas-1.0.0.jar"]

2. 将pas.tar.gz 放入该目录下,创建券目录 logs 和config ,最终结构如下:

[root@iZotm71x017bylZ pas]# ll
total 24244
-rw-r--r-- 1 root root      273 Sep  3 17:43 Dockerfile
drwxr-xr-x 2 root root     4096 Sep  3 15:57 config
drwxr-xr-x 2 root root     4096 Sep  3 18:09 logs
-rw-r--r-- 1 root root 24784743 Sep  3 16:41 pas.tar.gz

 注意: config 和logs两个目录会挂载到容器config和logs目录下。容器运行从config目录加载application.properties 和 logback.xml 配置,同时容器会将日志文件会输出到该logs目录下。

3. docker build -t chenyaochn/pas .  如下显示:

[root@iZotm71x017bylZ pas]# docker build -t chenyaochn/pas .
Sending build context to Docker daemon  52.75MB
Step 1/7 : FROM openjdk:8-jdk-alpine
 ---> 478bf389b75b
Step 2/7 : ADD pas.tar.gz /usr/local/
 ---> fc280ddd785a
Removing intermediate container 2075aabffedc
Step 3/7 : ENV JAVA_OPTS ""
 ---> Running in 681af64dfa9b
 ---> bad8ae41f58c
Removing intermediate container 681af64dfa9b
Step 4/7 : VOLUME /opt/pas/logs
 ---> Running in bcdd0fac9a14
 ---> a424c0f6c7c2
Removing intermediate container bcdd0fac9a14
Step 5/7 : EXPOSE 84
 ---> Running in dc7f6eff63c1
 ---> f21fd6919fd5
Removing intermediate container dc7f6eff63c1
Step 6/7 : WORKDIR /usr/local/pas/
 ---> 121ac70d7e82
Removing intermediate container 84f20985d008
Step 7/7 : ENTRYPOINT sh -c java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar  pas-1.0.0.jar
 ---> Running in 6212828c9d53
 ---> 3efcebec6a4d
Removing intermediate container 6212828c9d53
Successfully built 3efcebec6a4d
Successfully tagged chenyaochn/pas:latest

4.docker images查看镜像 ,已经创建成功:

[root@iZotm71x017bylZ pas]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
chenyaochn/pas      latest              3efcebec6a4d        45 minutes ago      129MB

 

部署

部署zookeeper

1.从docker hub上下载最新zookeeper镜像。

[root@iZotm71x017bylZ ~]# docker pull zookeeper 
Using default tag: latest

2.运行

[root@iZotm71x017bylZ ~]# docker run  -d -p 2181:2181 --name zookeeper zookeeper 
2defe46356fe8f28126350360f75c06cced32eb5154a391027e574b88bf3245a

可以将,zookeeper conf/目录挂载到宿主服务器,容器和宿主服务器共享zoo.cfg配置文件,方便对zookeeper配置的修改。

3. 查看 运行结果:

[root@iZotm71x017bylZ ~]# docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                                        NAMES
2defe46356fe        zookeeper           "/docker-entrypoin..."   About a minute ago   Up About a minute   2888/tcp, 0.0.0.0:2181->2181/tcp, 3888/tcp   zookeeper

 

部署springboot项目

1.docker run -p 84:84 -v /opt/pas/config/:/usr/local/pas/config  -v  /opt/pas/logs/:/usr/local/pas/logs/ --name pas chenyaochn/pas  运行镜像:

#发布84端口, 创建两个卷,一个是配置文件目录,一个是日志文件目录
[root@iZotm71x017bylZ pas]# docker run -p 84:84 -v /opt/pas/config/:/usr/local/pas/config  -v  /opt/pas/logs/:/usr/local/pas/logs/ --name pas chenyaochn/pas

Starting PasApplication v1.0.0 on 35e4c217b9d4 with PID 5 (/usr/local/pas/pas-1.0.0.jar started by root in /usr/local/pas)

2. docker ps 可看到容器已经启动:

[root@iZotm71x017bylZ ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                        NAMES
35e4c217b9d4        chenyaochn/pas      "sh -c 'java $JAVA..."   15 hours ago        Up 15 hours         0.0.0.0:84->84/tcp                           pas

3. tail -f logs/pas.log 查看 日志,可看到日志文件已经在宿主服务器上生成:

[root@iZotm71x017bylZ pas]# tail -f logs/pas.log 
2017-09-03 10:09:35.271 INFO [main] o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-09-03 10:09:35.318 INFO [main] o.s.w.s.h.SimpleUrlHandlerMapping - Mapped URL path [/webjars/**] onto ha

4.  curl  -I   http://localhost:84/pas/oauth  。成功访问pas项目 

[root@iZotm71x017bylZ pas]# curl  -I   http://localhost:84/pas/oauth
HTTP/1.1 200 
Content-Length: 0
Date: Sun, 03 Sep 2017 10:37:38 GMT

 

push 镜像到仓库

 1. docker push chenyaochn/pas

[root@iZotm71x017bylZ ~]# docker push chenyaochn/pas
The push refers to a repository [docker.io/chenyaochn/pas]
6cfcac5463b4: Pushed 
e378420761b2: Layer already exists 
b0f743408169: Layer already exists 
5bef08742407: Layer already exists 
latest: digest: sha256:1e3f1f1620cda285f62b10fec1f582a6b31d768dc7e98322cb96bb976a958333 size: 1159

2.查看

标签: docker
共有 人打赏支持
粉丝 3
博文 16
码字总数 30773
×
trntaken
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: