使用docker-compose部署springboot项目

由于是单机测试,没有测试多主机的跨网络分布式请求。

项目是前后分离的,所以使用nginx作为前端服务器,后端是springboot则直接基于java8环境的容器上跑,cache用的redis,mysql单独运行一个容器不在docker-compose里面。

使用docker-compose部署springboot项目_第1张图片

 

 

 大概的架构图如上面所示。

没有打包过springboot也没有用docker部署过,有问题找官网。https://spring.io/guides/gs/spring-boot-docker/

按照官网给出的指南,很快就写一个Dockerfile和创建一个项目的容器。

下面是docker-compose的内容和Dockerfile的内容:

Dockerfile:

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:

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跑目前的项目是出不来验证码图片。

后来添加了jvm运行调试参数,远程调试才知道验证码出来了就是不显示图片。

后来的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会自动添加当前文件夹的名字来当做是项目?所以最好先看清楚网络名称再加入进来

然后直接使用容器名访问即可,如:

 

 本来是一次很简单的部署,最后因为一个验证码搞了一整天。

 

你可能感兴趣的:(使用docker-compose部署springboot项目)