使用docker-compose部署springboot項目

因爲是單機測試,沒有測試多主機的跨網絡分佈式請求。php

項目是先後分離的,因此使用nginx做爲前端服務器,後端是springboot則直接基於java8環境的容器上跑,cache用的redis,mysql單獨運行一個容器不在docker-compose裏面。html

 

 

 大概的架構圖如上面所示。前端

沒有打包過springboot也沒有用docker部署過,有問題找官網。https://spring.io/guides/gs/spring-boot-docker/java

按照官網給出的指南,很快就寫一個Dockerfile和建立一個項目的容器。mysql

下面是docker-compose的內容和Dockerfile的內容:nginx

Dockerfile:web

FROM openjdk:8-jdk-alpine
VOLUME /tmp
COPY myapp.jar app.jar
ENTRYPOINT [ "java","-agentlib:jdwp=transport=dt_socket,server=y,address=8000,suspend=n","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar" ]

docker-compose.yml:redis

version: '2'
services:
  proxy:
    image: nginx:1.16.1
    ports:
      - 80:80
      - 443:443
    privileged: true
    restart: always
    networks:
      webapp:
        ipv4_address: 172.16.238.10
    volumes:
      - /opt/acme/nginx/conf/nginx.conf:/etc/nginx/nginx.conf
      - /opt/acme/nginx/conf/keys:/etc/nginx/keys
      - /opt/acme/nginx/conf/conf.d:/etc/nginx/conf.d
      - /opt/acme/nginx/www:/usr/share/nginx/html
      - /opt/acme/nginx/logs:/var/log/nginx
  cache:
    image: redis:5.0
    privileged: true
    restart: always
    networks:
      webapp:
        ipv4_address: 172.16.238.12
    ports:
      - 6379:6379
    volumes:
      - /opt/acme/redis/conf/redis.conf:/usr/local/etc/redis/redis.conf
      - /opt/acme/redis/data:/data
  cms:
    build: .
    restart: always
    networks:
      webapp:
        ipv4_address: 172.16.238.13
    ports:
      - 8888:8888
      - 8889:8889
      - 8000:8000 #use for debug
networks:
  webapp:
    driver: bridge
    ipam:
      config:
        - subnet: 172.16.238.0/24
          gateway: 172.16.238.1

一看好像挺和諧的。問題出如今個人項目須要用到一個驗證碼,須要用rt包生成,不知道openjdk和sunjdk的區別在哪裏,可是若是相似上面的Dockerfile跑目前的項目是出不來驗證碼圖片。spring

後來添加了jvm運行調試參數,遠程調試才知道驗證碼出來了就是不顯示圖片。sql

後來的Dockerfile換成了:

FROM java:8
VOLUME /tmp
COPY myapp.jar app.jar
ENTRYPOINT [ "java","-agentlib:jdwp=transport=dt_socket,server=y,address=8000,suspend=n","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar" ]

可能處處都是知識盲區,致使只知道這樣能夠。具體是什麼緣由殊不知道。

這樣前端nginx直接使用容器名訪問後端便可。下面是nginx的default.conf

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html/dist;
        index  index.html index.htm;
    }
    
    location /platform-plus{
        proxy_pass http://cms:8888/platform-admin;
        proxy_set_header  Host             $host;
        proxy_set_header  X-Real-IP        $remote_addr;
        proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;

    }
    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

後端須要訪問數據庫的話就直接把mysql加入到當前網絡

docker network connect acme_webapp mysql

由於docker-compose會自動添加當前文件夾的名字來當作是項目?因此最好先看清楚網絡名稱再加入進來

而後直接使用容器名訪問便可,如:

 

 原本是一次很簡單的部署,最後由於一個驗證碼搞了一成天。

相關文章
相關標籤/搜索