为什么我的docker compose文件没有创建一个名为cucurbit的数据库?

9gm1akwq  于 2023-03-29  发布在  Docker
关注(0)|答案(1)|浏览(127)

我有docker compose文件。容器cucurbit后端依赖于db。它是一个java springboot后端,以postgres作为rdms。当我运行docker-compose up时,它说cucurbit数据库不存在。

cucurbit-backend |   .   ____          _            __ _ _
cucurbit-backend |  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
cucurbit-backend | ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
cucurbit-backend |  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
cucurbit-backend |   '  |____| .__|_| |_|_| |_\__, | / / / /
cucurbit-backend |  =========|_|==============|___/=/_/_/_/
cucurbit-backend |  :: Spring Boot ::                (v2.5.7)
cucurbit-backend | 
cucurbit-backend | 2023-03-17 16:44:06.632  INFO 1 --- [           main] c.z.cucurbit.CucurbitApplication         : Starting CucurbitApplication using Java 11.0.18 on d841a735fe8f with PID 1 (/app.jar started by root in /)
cucurbit-backend | 2023-03-17 16:44:06.634  INFO 1 --- [           main] c.z.cucurbit.CucurbitApplication         : No active profile set, falling back to default profiles: default
cucurbit-backend | 2023-03-17 16:44:07.289  INFO 1 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
cucurbit-backend | 2023-03-17 16:44:07.338  INFO 1 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 42 ms. Found 4 JPA repository interfaces.
cucurbit-backend | 2023-03-17 16:44:07.778  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
cucurbit-backend | 2023-03-17 16:44:07.787  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
cucurbit-backend | 2023-03-17 16:44:07.788  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.55]
cucurbit-backend | 2023-03-17 16:44:07.828  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
cucurbit-backend | 2023-03-17 16:44:07.829  INFO 1 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1145 ms
cucurbit-backend | 2023-03-17 16:44:07.930  INFO 1 --- [           main] com.zeitgeist.cucurbit.CORSFilter        : Initializing CORSFilter...
cucurbit-backend | 2023-03-17 16:44:08.030  INFO 1 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
cucurbit-db | 2023-03-17 16:44:08.059 UTC [28] FATAL:  database "cucurbit" does not exist
cucurbit-backend | 2023-03-17 16:44:09.079 ERROR 1 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Exception during pool initialization.
cucurbit-backend | 
cucurbit-backend | org.postgresql.util.PSQLException: FATAL: database "cucurbit" does not exist
cucurbit-backend |  at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2565) ~[postgresql-42.2.24.jar!/:42.2.24]
cucurbit-backend |  at org.postgresql.core.v3.QueryExecutorImpl.readStartupMessages(QueryExecutorImpl.java:2677) ~[postgresql-42.2.24.jar!/:42.2.24]
cucurbit-backend |  at org.postgresql.core.v3.QueryExecutorImpl.<init>(QueryExecutorImpl.java:147) ~[postgresql-42.2.24.jar!/:42.2.24]
cucurbit-backend |  at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:273) ~[postgresql-42.2.24.jar!/:42.2.24]
cucurbit-backend |  at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:51) ~[postgresql-42.2.24.jar!/:42.2.24]
cucurbit-backend |  at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:225) ~[postgresql-42.2.24.jar!/:42.2.24]
cucurbit-backend |  at org.postgresql.Driver.makeConnection(Driver.java:465) ~[postgresql-42.2.24.jar!/:42.2.24]
cucurbit-backend |  at org.postgresql.Driver.connect(Driver.java:264) ~[postgresql-42.2.24.jar!/:42.2.24]

我已经将这些添加到环境变量中,但它并没有创建数据库:

environment:
      - POSTGRES_DB:cucurbit
      - POSTGRES_USER:postgres
      - POSTGRES_PASSWORD:password

以下是我的完整docker compose文件:

version: '2'

services:
  app:
    image: 'docker-spring-boot-postgres:latest'
    build:
      context: .
    container_name: cucurbit-backend
    depends_on:
      - db
    environment:
      - CUCURBIT_ENVIRONMENT_PROFILE=development
      - CUCURBIT_DATABASE_URL=jdbc:postgresql://cucurbit-db:5432/cucurbit
      - CUCURBIT_DATASOURCE_USERNAME=postgres
      - CUCURBIT_DATASOURCE_PASSWORD=password
      - CUCURBIT_EMAIL_SENDER_USERNAME=noreply.cucurbit@gmail.com
      - CUCURBIT_EMAIL_SENDER_PASSWORD=wakeuptellme1234.
      - CUCURBIT_EMAIL_NOTIFICATION_SEND_TO=jobskreya@gmail.com
    expose:
      - 8080
    ports:
      - 8080

  db:
    image: 'postgres:13.1-alpine'
    container_name: cucurbit-db
    hostname: cucurbit-db
    expose:
      - '5432'
    ports:
      - 5432:5432
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=password
      - POSTGRES_DB=cucurbit
    command: postgres -c 'create database cucurbit' dbname=cucurbit

我的docker-compose文件有什么问题?

olmpazwi

olmpazwi1#

你应该删除docker compose文件的最后一行:

command: postgres -c 'create database cucurbit' dbname=cucurbit

由于传递了环境变量POSTGRES_DB,因此将创建名为cucurbit的数据库。
POSTGRES_DB
这个可选的环境变量可以用来为镜像第一次启动时创建的默认数据库定义一个不同的名称。如果没有指定,那么将使用POSTGRES_USER的值。来自docker hub description
此外,命令postgres -c 'create database cucurbit' dbname=cucurbit本身是错误的,因为您试图连接到名为cucurbit(dbname=cucurbit)的数据库并创建同名数据库。

相关问题