简体   繁体   中英

How to access an external server folder from a spring boot docker container?

I'm using docker-compose to run spring boot application inside a docker container. A spring boot uses embedded tomcat to run.

There is an external folder on a server (/opt/cp/uploads) with images which I would like to access from a spring boot docker container.

Within the docker-compose.yaml file there are following containers defined:

  • nginx
  • mysql
  • springboot-app

nginx and CloudFlare redirect the domain to the spring app on the port 8080. I'd like to access images like this: https://domainname.com/uploads/imageName.png

Using tomcat this was done in server.xml:

<Context docBase="/opt/uploads" path="/uploads"/>

but this option is obviously not available via application.properties with embedded tomcat.

How does one come about this?

My docker-compose.yaml:

version: '3.8'
services:
  nginx:
    container_name: some-nginx
    hostname: nginx
    image: nginx:1.19.2-alpine
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx/conf.d:/etc/nginx/conf.d
    restart: unless-stopped
    network_mode: host
  mysqldb:
    image: mysql:8.0.20
    hostname: mysqldb
    container_name: cp-mysqldb
    environment:
      - MYSQL_ROOT_PASSWORD=pass1234
      - MYSQL_DATABASE=db_name
      - MYSQL_USER=root
      - MYSQL_PASSWORD=pass1234
    ports:
      - "3306:3306"
    volumes:
      - cp-mysqldb-data:/opt/mysql
    restart: unless-stopped
  springboot-app:
    image: openjdk:8
    hostname: cp
    container_name: cp-springboot
    environment:
      - SPRING_DATASOURCE_URL=jdbc:mysql://mysqldb:3306/db_name?autoReconnect=true&useSSL=false&useUnicode=yes&characterEncoding=UTF-8&characterSetResults=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
    ports:
      - "8080:8080"
    depends_on:
      - mysqldb
    volumes:
      - cp-springboot-data:/opt/cp
      - ./target/cp-springboot-0.0.1-SNAPSHOT.war:/ROOT.war
    command: ["java", "-jar",
              "-Dspring.profiles.active=prod",
              "ROOT.war"]
    restart: always
  
volumes:
  cp-springboot-data: {
    }
  cp-mysqldb-data: {
    }

Maybe better use Tomcat for docker image and not OpenJDK.

That way you can setup your Tomcat configuration for the external path

Take a look here: https://medium.com/@iamvickyav/deploying-spring-boot-war-in-tomcat-based-docker-2b689b206496

Actually, this option IS available via application.properties with embedded tomcat:

spring.mvc.static-path-pattern=/uploads/**
spring.resources.static-locations=file:/opt/uploads

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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