繁体   English   中英

使用Gradle和Docker部署Spring Boot / PostgreSQL项目

[英]Deploy of Spring Boot/PostgreSQL project with Gradle and Docker

大家。 我在项目部署方面遇到一些问题,并且已经花了几天的时间来解决它。 我在堆栈上开发移动后端:

  1. Spring Boot 1.5 +开发工具
  2. PostgreSQL的
  3. 码头工人
  4. 摇篮

docker-compose.yml

version: '3'
services:
    web:
      image: mobilebackend      
      ports:
          - 8088:8080
      depends_on:
          - db
      links:
         - db
    db:
        container_name: transneft_db
        image: postgres
        restart: always
        volumes:
            - transneft_db:/var/lib/postgresql/data
        environment:
            - POSTGRES_PASSWORD=pass
            - POSTGRES_USER=user
            - POSTGRES_DB=db
            - PGDATA=/var/lib/postgresql/data/pgdata
        ports:
            - 54320:5432
    adminer:
        image: adminer
        restart: always
        ports:
            - 8082:8080
volumes:
    transneft_db: {}

application.properties

spring.datasource.url=jdbc:postgresql://localhost:54320/db
spring.datasource.username=user
spring.datasource.password=pass
spring.datasource.platform=postgres
spring.datasource.driver-class-name=org.postgresql.Driver

spring.jpa.database=POSTGRESQL
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect

jwt.secret =aotransneftsibir

logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR

build.gradle

buildscript {
    ext {
        springBootVersion = '1.5.10.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath('se.transmode.gradle:gradle-docker:1.2')
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'docker'
apply plugin: 'application'

repositories {
    mavenCentral()
}

compileJava {
    sourceCompatibility = 1.8
    targetCompatibility = 1.8
    mainClassName       = "com.backend.MobilebackendApplication"
}

jar {
    baseName = "backend-api"
    group    = "com.backend"
    version  = "0.0.1-SNAPSHOT"
    manifest { attributes "Main-Class": "com.backend.mobilebackend.MobilebackendApplication" }
}

docker {
    baseImage "frolvlad/alpine-oraclejdk8:slim"
    maintainer 'Alex Scrobot "scrobot91@gmail.com"'
}

dependencies {
    // spring boot
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-data-rest')
    compile('org.springframework.boot:spring-boot-starter-web')
    runtime('org.springframework.boot:spring-boot-devtools')

    //postgresql
    runtime('org.postgresql:postgresql')

    //gson
    compile group: 'com.google.code.gson', name: 'gson', version: '2.7'

    // JWT
    compile 'io.jsonwebtoken:jjwt:0.9.0'

    //test env
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile('org.springframework.security:spring-security-test')
}

在localhost上运行良好,项目正在使用DevTools构建并且运行良好。 具有postgresql的Docker容器已启动,我可以使用localhost:port访问与之的连接,一切都很好。

问题开始,然后我尝试致电:

./gradlew build distDocker --refresh-dependencies

在这种情况下, spring.datasource.url属性必须包含localhost值,构建将失败。 我输入localhost值,获取成功消息,该映像已构建。 因此,然后我尝试使用Spring Boot .jar运行容器,但出现“连接被拒绝错误”。 对于这种情况,我有一些想法:具有.jar的容器正在运行,它试图通过localhost:db_port获取数据库访问权限,但是在此容器内部不能是与其本地主机的db连接。 所以,然后我放入application.property

spring.datasource.url=jdbc:postgresql://db:54320/db

但是,我无法建立更新的图像。 然后gradle尝试引导组装的.jar,它无法访问db:54320,因为引导不在容器中运行,并且正确连接了localhost:54320。 您是否感觉到我的恶性循环?))

我不明白,我在做什么错...请帮助我解决此问题。谢谢。

快速而肮脏的解决方案是将127.0.0.2 db放入/ etc / hosts文件中,并使用jdbc:postgresql:// db:54320 / db作为数据库URL,然后一切正常。

更好的解决方案是为您的应用程序使用构建容器。 因此,您的docker-compose如下所示:

version: '3'
services:
  web:
    image: mobilebackend
    # The value of build refers to a directory at where your compose file is.
    build: mobilebackend
    ...

在mobilebackend目录中,您可以这样创建一个Dockerfile:

FROM gradle AS build
RUN gradle build ...

FROM openjdk
COPY --from=build mobilebackend.jar .
RUN java -jar mobilebackend.jar

由于您不需要为此公开数据库端口,因此甚至可以使用标准端口:jdbc:postgresql:// db / db

现在,您可以使用以下命令编译和运行整个应用程序

docker-compose build
docker-compose up

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM