通過 docker-compose 一鍵部署一個微服務專案

weixin_39904272發表於2020-10-01

強推文章:

10大免費的白嫖網站

打包,移包

  1. 進入到專案根目錄,通過 mvn clean test package install 生成 jar/war 包。
    在這裡插入圖片描述

  2. 在 target 目錄內,找到打包好的 jar 包 / war 包。複製到 Linux 系統的指定目錄

把 jar包放入對應的目錄內,以下是放好jar包的目錄結構。

[root@localhost supershop]# tree
.
├── docker-compose
│   ├── service
│   │   └── docker-compose.yml
│   └── web
│       └── docker-compose.yml
├── supershop-service-admin
│   ├── Dockerfile
│   └── supershop-service-admin.jar
├── supershop-service-portal
│   ├── Dockerfile
│   └── supershop-service-portal.jar
├── supershop-service-redis
│   ├── Dockerfile
│   └── supershop-service-redis.jar
├── supershop-web-admin
│   ├── Dockerfile
│   └── supershop-web-admin.jar
└── supershop-web-portal
├── Dockerfile
└── supershop-web-portal.jar
7 directories, 12 files
[root@localhost supershop]# 

編寫 Dockerfile

supershop-service-admin 目錄內的 Dockerfile

[root@localhost supershop]# cat ./supershop-service-admin/Dockerfile 
FROM openjdk:8-jre

MAINTAINER Flying <814110433@qq.com>

RUN mkdir /app

WORKDIR /app

COPY supershop-service-admin.jar /app

ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app/supershop-service-admin.jar"]

EXPOSE 20880 8510
[root@localhost supershop]#

supershop-web-admin 目錄內的 Dockerfile

[root@localhost supershop]# cat ./supershop-web-admin/Dockerfile 
FROM openjdk:8-jre

MAINTAINER Flying <814110433@qq.com>

RUN mkdir /app

WORKDIR /app

COPY supershop-web-admin.jar /app

ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app/supershop-web-admin.jar"]

EXPOSE 8100
[root@localhost supershop]#

supershop-service-redis 目錄內的 Dockerfile

[root@localhost supershop]# cat ./supershop-service-redis/Dockerfile 
FROM openjdk:8-jre

MAINTAINER Flying <814110433@qq.com>

RUN mkdir /app

WORKDIR /app

COPY supershop-service-redis.jar /app

ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app/supershop-service-redis.jar"]

EXPOSE 20882 8512
[root@localhost supershop]#

編寫 docker-compose.yml

根據啟動先後順序和服務型別配置一個或多個 docker-compose.yml 。此處分為 service 和 web 。先啟動 service ,再啟動 web 。

存放 docker-compose.yml 的目錄

[root@localhost docker-compose]# tree

├── service
│   └── docker-compose.yml
└── web
    └── docker-compose.yml

2 directories, 2 files
[root@localhost docker-compose]# pwd
/root/supershop/docker-compose
[root@localhost docker-compose]# ls
service  web		
[root@localhost docker-compose]# 

service 的 docker-compose 內容

[root@localhost docker-compose]# cd service/
[root@localhost service]# 
[root@localhost service]# ls
docker-compose.yml
[root@localhost service]# 
[root@localhost service]# cat docker-compose.yml 	# service 的 docker-compose 內容
version: '3'
services:
    supershop-service-redis:
      build:
        context: ../../supershop-service-redis
        dockerfile: Dockerfile
      image: supershop-service-redis:latest
      container_name: supershop-service-redis
      ports:
        - 20882:20882
        - 8512:8512
      networks:
        supershop_net:
          aliases:
            - supershop-service-redis
      restart: always
      healthcheck:
        test: "/bin/netstat -anpt|grep 8512"
        interval: 30s
        timeout: 3s
        retries: 1
    supershop-service-admin:
      build:
        context: ../../supershop-service-admin
        dockerfile: Dockerfile
      image: supershop-service-admin:latest
      container_name: supershop-service-admin
      ports:
        - 20880:20880
        - 8510:8510
      networks:
        supershop_net:
          aliases:
            - supershop-service-admin
      restart: always
      depends_on:
        - "supershop-service-redis"
      healthcheck:
        test: "/bin/netstat -anpt|grep 8510"
        interval: 30s
        timeout: 3s
        retries: 1
    supershop-service-portal:
      build:
        context: ../../supershop-service-portal
        dockerfile: Dockerfile
      image: supershop-service-portal:latest
      container_name: supershop-service-portal
      ports:
        - 20881:20881
        - 8511:8511
      networks:
        supershop_net:
          aliases:
            - supershop-service-portal
      restart: always
      depends_on:
        - "supershop-service-redis"
      healthcheck:
        test: "/bin/netstat -anpt|grep 8511"
        interval: 30s
        timeout: 3s
        retries: 1
networks:
    supershop_net:
      external: true
[root@localhost service]# 

web 的 docker-compose 內容

[root@localhost service]# cd ../web/ && ll && cat docker-compose.yml 	# web 的 docker-compose 內容
總用量 4
-rwxr-xr-x. 1 root root 1010 9月  27 00:06 docker-compose.yml
version: '3'
services:
    supershop-web-admin:
      build:
        context: ../../supershop-web-admin
        dockerfile: Dockerfile
      image: supershop-web-admin:latest
      container_name: supershop-web-admin
      ports:
        - 8100:8100
      networks:
        supershop_net:
          aliases:
            - supershop-web-admin
      restart: always
      healthcheck:
        test: "/bin/netstat -anpt|grep 8100"
        interval: 30s
        timeout: 3s
        retries: 1
    supershop-web-portal:
      build:
        context: ../../supershop-web-portal
        dockerfile: Dockerfile
      image: supershop-web-portal:latest
      container_name: supershop-web-portal
      ports:
        - 8101:8101
      networks:
        supershop_net:
          aliases:
            - supershop-web-portal
      restart: always
      healthcheck:
        test: "/bin/netstat -anpt|grep 8101"
        interval: 30s
        timeout: 3s
        retries: 1
networks:
    supershop_net:
      external: true
[root@localhost web]# 

啟動

先進入 service 目錄: docker-compose build && docker-compose up -d
再進入 web 目錄: docker-compose build && docker-compose up -d

待解決的問題?

  1. npm run dev 如何在 Linux 上執行?
  2. npm run dev 如何在 Linux 上持久執行?
  3. 多個 npm run dev 如何在 Linux 上持久執行?

相關文章