欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

centos7下docker发布第一个微服务应用(Eureka)

程序员文章站 2022-07-22 23:40:34
1.在windows下打包 微服务应用通过maven进行打包,在项目的pom.xml执行mvn clean package,或者直接通过idea或者eclipse进行maven打包 之上操作将在项目的 target目录生成文件microservice-discovery-eureka-0.0.1-S ......

1.在windows下打包

微服务应用通过maven进行打包,在项目的pom.xml执行mvn clean package,或者直接通过idea或者eclipse进行maven打包

之上操作将在项目的 target目录生成文件microservice-discovery-eureka-0.0.1-snapshot.jar 包

2.将microservice-discovery-eureka-0.0.1-snapshot.jar拷贝到linux环境(可以通过crt等连接方式)

3.在microservice-discovery-eureka-0.0.1-snapshot.jar所在的目录创建dockerfile文件,内容如下

 1 from java:8
 2 
 3 volume /tmp
 4 
 5 add microservice-discovery-eureka-0.0.1-snapshot.jar app.jar
 6 
 7 run bash -c 'touch /app.jar'
 8 
 9 expose 8761
10 
11 entrypoint ["java","-djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

 4.通过docker build命令构建新的镜像

(注意:dockerfile和jar包要在同一个文件夹下)

 执行命令 docker build -t cakin24/microservice-discovery-eureka-0.0.1 .   

 1 [root@localhost microfile]# ls
 2 dockerfile  microservice-discovery-eureka-0.0.1-snapshot.jar
 3 [root@localhost microfile]# docker build -t cakin24/microservice-discovery-eureka-0.0.1 .
 4 sending build context to docker daemon  44.79mb
 5 step 1/6 : from java:8
 6 8: pulling from library/java
 7 5040bd298390: pull complete 
 8 fce5728aad85: pull complete 
 9 76610ec20bf5: pull complete 
10 60170fec2151: pull complete 
11 e98f73de8f0d: pull complete 
12 11f7af24ed9c: pull complete 
13 49e2d6393f32: pull complete 
14 bb9cdec9c7f3: pull complete 
15 digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d
16 status: downloaded newer image for java:8
17  ---> d23bdf5b1b1b
18 step 2/6 : volume /tmp
19  ---> running in c6c8df634611
20 removing intermediate container c6c8df634611
21  ---> 1bdb7dd203b9
22 step 3/6 : add microservice-discovery-eureka-0.0.1-snapshot.jar app.jar
23  ---> d88f6144fb16
24 step 4/6 : run bash -c 'touch /app.jar'
25  ---> running in d217d42a3713
26 removing intermediate container d217d42a3713
27  ---> b24c68563734
28 step 5/6 : expose 8761
29  ---> running in 2a2663de2b31
30 removing intermediate container 2a2663de2b31
31  ---> 305d7a1bed02
32 step 6/6 : entrypoint ["java","-djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
33  ---> running in c7b7c777ee72
34 removing intermediate container c7b7c777ee72
35  ---> 6e55e8eef9b1
36 successfully built 6e55e8eef9b1
37 successfully tagged cakin24/microservice-discovery-eureka-0.0.1:latest
38 [root@localhost microfile]# docker images
39 repository                                           tag                 image id            created             size
40 cakin24/microservice-discovery-eureka-0.0.1          latest              6e55e8eef9b1        24 seconds ago      733mb
41 mysql                                                5.7                 e47e309f72c8        13 days ago         372mb
42 hello-world                                          latest              fce289e99eb9        7 weeks ago         1.84kb
43 java                                                 8                   d23bdf5b1b1b        2 years ago         643mb
44 registry.cn-hangzhou.aliyuncs.com/acs-sample/mysql   5.7                 ec7e75e5260c        3 years ago         360mb

5.执行以下命令 运行微服务

查看镜像 docker images  通过上面步骤可以看到当前多了一个eureka的镜像,这个就是我们想要的

运行容器 docker run -d -p 8761:8761 cakin24/microservice-discovery-eureka-0.0.1

查看已经运行的容器情况  docker ps   可以看到eureka已经在运行当中

 1 [root@localhost microfile]# docker images
 2 repository                                           tag                 image id            created             size
 3 cakin24/microservice-discovery-eureka-0.0.1          latest              6e55e8eef9b1        24 seconds ago      733mb
 4 mysql                                                5.7                 e47e309f72c8        13 days ago         372mb
 5 hello-world                                          latest              fce289e99eb9        7 weeks ago         1.84kb
 6 java                                                 8                   d23bdf5b1b1b        2 years ago         643mb
 7 registry.cn-hangzhou.aliyuncs.com/acs-sample/mysql   5.7                 ec7e75e5260c        3 years ago         360mb
 8 [root@localhost microfile]# docker run -d -p 8761:8761 cakin24/microservice-discovery-eureka-0.0.1
 9 bc05ffb47756bc1193f71c668acd2ddb51392de883dba3d6f26a786723d78397
10 [root@localhost microfile]# docker ps
11 container id        image                                         command                  created             status              ports                               names
12 bc05ffb47756        cakin24/microservice-discovery-eureka-0.0.1   "java -djava.securit…"   11 seconds ago      up 8 seconds        0.0.0.0:8761->8761/tcp              flamboyant_golick
13 e778509d7366        mysql:5.7                                     "docker-entrypoint.s…"   about an hour ago   up about an hour    0.0.0.0:3306->3306/tcp, 33060/tcp   pwc-mysql

6.查看运行效果

 访问发布的容器对应的ip和端口 效果如下,centos7下docker发布第一个微服务应用(Eureka)