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和埠 效果如下,