You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行docker-compose up时Spring Boot应用提示无法访问application.jar如何解决?

Spring Boot项目Docker化后无法找到application.jar容器退出问题

尝试将Spring Boot项目Docker化,使用PostgreSQL作为数据库。执行docker-compose up后,数据库容器运行正常,但应用容器因「Can not access application.jar file」错误退出。本地用java -jar运行项目完全正常。

相关配置文件及日志

Dockerfile

FROM openjdk:latest
RUN mkdir /app
WORKDIR /app
EXPOSE 8080
ARG JAR_FILE=./target/*.jar
COPY ${JAR_FILE} /app/application.jar
ARG HOTEL_CSV=./src/main/resources/data/hotels.csv
COPY ${HOTEL_CSV} /app/data/hotels.csv
ARG OFFERS=./src/main/resources/data/offers.csv
COPY ${OFFERS} /app/data/offers.csv

ENTRYPOINT ["java", "-jar", "application.jar"]

docker-compose.yml

version: '3.8'
services:
  app:
    build: ./
    image: holiday-backend-app-1
    ports:
      - "8080:8080"
    volumes:
      - .:/app
      - ./data:/app/data
    depends_on:
      - db
    environment:
      SPRING_DATASOURCE_URL: jdbc:postgresql://0.0.0.0:5432/postgres
      SPRING_DATASOURCE_USERNAME: postgres
      SPRING_DATASOURCE_PASSWORD: postgres
      SPRING_DATASOURCE_DRIVER_CLASS_NAME: org.postgresql.Driver
      SPRING_JPA_SHOW_SQL: "true"
      SPRING_JPA_HIBERNATE_DDL_AUTO: create-drop
      SPRING_JPA_PROPERTIES_HIBERNATE_DIALECT: org.hibernate.dialect.PostgreSQLDialect
      server.port: 8080
      server.address: 0.0.0.0

  db:
    image: postgres
    ports:
      - "5432:5432"
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: postgres

数据库容器日志(供参考)

The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/postgresql/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Etc/UTC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok


Success. You can now start the database server using:

    pg_ctl -D /var/lib/postgresql/data -l logfile start
initdb: warning: enabling "trust" authentication for local connections
initdb: hint: You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb.

waiting for server to start....2023-05-22 05:17:58.982 UTC [48] LOG:  starting PostgreSQL 15.3 (Debian 15.3-1.pgdg110+1) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
2023-05-22 05:17:58.984 UTC [48] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2023-05-22 05:17:58.990 UTC [51] LOG:  database system was shut down at 2023-05-22 05:17:58 UTC
2023-05-22 05:17:58.997 UTC [48] LOG:  database system is ready to accept connections
 done
server started

/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*

waiting for server to shut down...2023-05-22 05:17:59.103 UTC [48] LOG:  received fast shutdown request
.2023-05-22 05:17:59.105 UTC [48] LOG:  aborting any active transactions
2023-05-22 05:17:59.109 UTC [48] LOG:  background worker "logical replication launcher" (PID 54) exited with exit code 1
2023-05-22 05:17:59.112 UTC [49] LOG:  shutting down
2023-05-22 05:17:59.113 UTC [49] LOG:  checkpoint starting: shutdown immediate
2023-05-22 05:17:59.122 UTC [49] LOG:  checkpoint complete: wrote 3 buffers (0.0%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.003 s, sync=0.001 s, total=0.010 s; sync files=2, longest=0.001 s, average=0.001 s; distance=0 kB, estimate=0 kB
2023-05-22 05:17:59.130 UTC [48] LOG:  database system is shut down
 done
server stopped

PostgreSQL init process complete; ready for start up.

2023-05-22 05:17:59.234 UTC [1] LOG:  starting PostgreSQL 15.3 (Debian 15.3-1.pgdg110+1) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
2023-05-22 05:17:59.234 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2023-05-22 05:17:59.234 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2023-05-22 05:17:59.237 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2023-05-22 05:17:59.242 UTC [62] LOG:  database system was shut down at 2023-05-22 05:17:59 UTC
2023-05-22 05:17:59.248 UTC [1] LOG:  database system is ready to accept connections

解决方案

核心原因

docker-compose.yml中配置的- .:/app本地目录挂载,会覆盖容器内/app目录下的所有内容。本地项目根目录下没有application.jar(jar包存放在target目录),导致容器启动时找不到该文件。

修复步骤

1. 移除冲突的目录挂载

修改docker-compose.yml的volumes配置,删除- .:/app行。如果需要实时同步csv文件,保留对应目录挂载即可:

volumes:
  - ./src/main/resources/data:/app/data

如果不需要实时同步csv,可完全移除volumes配置(Dockerfile已将csv复制到容器内)。

2. 确保jar包已正确构建

先执行构建命令生成jar包:

# Maven项目
mvn clean package
# Gradle项目
./gradlew build

再重新构建镜像并启动容器:

docker-compose down
docker-compose build --no-cache
docker-compose up

3. 修正数据库连接地址

容器间需通过服务名通信,将数据源地址改为db(docker-compose中数据库服务的名称):

SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/postgres

4. 可选:添加数据库就绪检查

depends_on仅保证容器启动顺序,不确保数据库服务就绪。可添加健康检查确保数据库就绪后再启动应用:
在db服务中添加healthcheck:

db:
  image: postgres
  ports:
    - "5432:5432"
  environment:
    POSTGRES_USER: postgres
    POSTGRES_PASSWORD: postgres
    POSTGRES_DB: postgres
  healthcheck:
    test: ["CMD-SHELL", "pg_isready -U postgres"]
    interval: 5s
    timeout: 5s
    retries: 5

在app服务中修改depends_on:

depends_on:
  db:
    condition: service_healthy

内容的提问来源于stack exchange,提问作者duckoid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 06:37:36