使用Docker Compose的Spring应用程序中是否有任何端口映射特定功能?

我对dockerizing Spring Boot应用有问题。我的docker-compose项目包含4个部分:

  • 返回-它只是8080上带有Tomcat的Spring Boot应用程序。这是我的前端应用程序控制器。
  • 前-Nginx + Angular
  • 核心-主要由一个TCP服务器组成,用于通过一些简单的Java套接字在数据库应用中向数据库接收一些信息。
  • database-Postgres,我刚刚从Docker Hub下载并配置为创建后向应用程序所需的数据库。

我的目标是使用在主机浏览器中打开的前台应用程序,通过后台应用程序的控制器处理来自数据库应用程序的数据库中的数据。

So, I don't have any problems with building and running. Ports mapping for core, database and front apps works excellent. But not for back. I don't have any access from host to back-container from localhost:8080(curl requests from the host to container return an empty response, but curl in container bash works fine). In back-app I used Spring Security, so CORS is configured to allow all requests, and CSRF is disabled, if it's matter.

谨对我的英语受损深表歉意!

返回Dockerfile

FROM maven:3.5-jdk-8 AS build
COPY src /usr/src/app/src  
COPY pom.xml /usr/src/app  
RUN mvn -f /usr/src/app/pom.xml clean package

FROM gcr.io/distroless/java  
ARG JAR_FILE=target/*.jar
COPY --from=build /usr/src/app/${JAR_FILE} /usr/app/back.jar
ENTRYPOINT ["java","-jar","/usr/app/back.jar"]

核心Dockerfile

FROM maven:3.5-jdk-8 AS build
COPY src /usr/src/app/src  
COPY pom.xml /usr/src/app  
RUN mvn -f /usr/src/app/pom.xml clean package

FROM gcr.io/distroless/java  
ARG JAR_FILE=target/*.jar
COPY --from=build /usr/src/app/${JAR_FILE} /usr/app/core.jar
ENTRYPOINT ["java","-jar","/usr/app/core.jar"]

前端Dockerfile

FROM node:12 as builder
COPY package.json package-lock.json ./
RUN npm install && mkdir /app && mv ./node_modules ./app
WORKDIR /app
COPY . .
RUN npm run ng build -- --deploy-url=/ --prod

FROM nginx
COPY ./.nginx/nginx.conf /etc/nginx/nginx.conf
RUN rm -rf /usr/share/nginx/html/*
COPY --from=builder /app/dist/snsr-front-app /usr/share/nginx/html
ENTRYPOINT ["nginx", "-g", "daemon off;"]

docker-compose.yml

version: '3'

services:

    snsr-front-app:
        build: ./snsr-front-app
        ports: 
            - 80:80
        depends_on: 
            - snsr-back-app

    snsr-back-app:
        build: ./snsr-back-app
        ports: 
            - 8080:8080
        depends_on: 
            - database

    snsr-core-app:
        build: ./snsr-core-app
        ports: 
            - 3000:3000
        depends_on: 
            - database


    database: 
        image: postgres
        environment: 
            POSTGRES_USER: postgres
            POSTGRES_PASSWORD: masterkey
            POSTGRES_DB: snsr
        ports: 
            - 5433:5432

nginx.conf

worker_processes 4;

events { worker_connections 1024; }

http {
    server {
        listen 80;
        root  /usr/share/nginx/html;
        include /etc/nginx/mime.types;

        location / {
            try_files $uri /index.html;
        }
    }
}
评论