简体   繁体   English

Dockerfile、docker-compose.yml和application.properties混淆

[英]Dockerfile , docker-compose.yml and application.properties confusion

I am developing a Spring Boot app and want to keep the database on a Docker container.我正在开发一个 Spring 启动应用程序,并希望将数据库保存在一个 Docker 容器中。 However, after making a search and try to install the databse and connect, I am really very confused with docker-compose.yml , Dockerfile and application.properties in the app.然而,在进行搜索并尝试安装数据库并连接之后,我真的很困惑docker-compose.ymlDockerfileapplication.properties中的 application.properties 。 Could you please clarify me shortly about the following points?能否请您尽快澄清以下几点?

1. AS far as I see, docker-compose.yml is used to create Docker containers eg creating a database container on Docker. If we do not run it via docker-compose up -d , docker-compose.yml is not executed ehen we run the Spring Boot app. 1.据我所知, docker-compose.yml用于创建 Docker 容器,例如在 Docker 上创建数据库容器。如果我们不通过docker-compose up -d运行它, docker-compose.yml不会在我们运行 docker-compose.yml 时运行 docker-compose.yml . Is that true?真的吗?

2. When Dockerfile is executed? 2、Dockerfile什么Dockerfile执行? As it is used to build necessary images for our apps, I think it is used for publishing phase.由于它用于为我们的应用程序构建必要的图像,我认为它用于发布阶段。 Is that true?真的吗?

3. As far as I see, we use database connection string in our application.properties (or yml version) and when we run the app, it is used. 3.据我所知,我们在application.properties (或 yml 版本)中使用数据库连接字符串,当我们运行应用程序时,它会被使用。 Is the difference between application.properties and docker-compose.yml , the former is executed when app is running and the latter is used only we execute. application.propertiesdocker-compose.yml的区别,前者在app运行时执行,后者只在我们执行时使用。 Is that true?真的吗?

I have the following but although the table creation string is seen on the console after app is run, it cannot create table (I have @Entity and @Repository annotations for the related entity in my app).我有以下内容,但尽管在运行应用程序后在控制台上可以看到表创建字符串,但它无法创建表(我的应用程序中的相关实体有 @Entity 和 @Repository 注释)。 So, what is the problem?那么,问题是什么?

spring.datasource.url=jdbc:postgresql://localhost:5432/product_db
spring.datasource.username=postgres
spring.datasource.password=********

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQL92Dialect
spring.jpa.properties.hibernate.default_schema = public
spring.jpa.hibernate.ddl-auto = create
spring.jpa.show-sql = true

Update: Here is the log message on the console indicating that the product table is creating, etc.更新:这是控制台上的日志消息,指示正在创建产品表等。

Hibernate: drop table if exists public.product cascade Hibernate:如果存在 public.product 级联则删除表
Hibernate: create table public.product (id bigserial not null, name varchar(255), code varchar(255), primary key (id)) Hibernate:创建表 public.product(id bigserial 不是 null,名称 varchar(255),代码 varchar(255),主键(id))

  1. In fact, the docker-compose.yml file is only used by docker-compose up and other docker-compose commands.实际上, docker-compose.yml文件仅供docker-compose updocker-compose命令使用。 Plain docker commands and non-Docker java commands don't look at it at all.普通docker命令和非 Docker java命令根本不看。

  2. The Dockerfile is read when you run docker-compose build , or docker-compose up --build , or docker build .当您运行Dockerfile docker-compose builddocker-compose up --builddocker build时,将读取 Dockerfile。 There are also Maven and Gradle extensions to build Docker images.还有 Maven 和 Gradle 扩展来构建 Docker 图像。 It needs to run before you can run a container, for example for manual or integration testing.它需要先运行,然后才能运行容器,例如用于手动或集成测试。 Note that most settings in the docker-compose.yml file are not visible in the Dockerfile, and the image build can't access other containers.请注意, docker-compose.yml文件中的大多数设置在 Dockerfile 中是不可见的,镜像构建无法访问其他容器。

  3. Spring has several ways to set properties. Spring有几种设置属性的方法。 Note that things like the database URL will be different running in a container vs. running directly on the host, and could be different again in a cloud environment;请注意,像数据库 URL 这样的东西在容器中运行与直接在主机上运行是不同的,并且在云环境中可能再次不同; that makes this something that's inappropriate to set in an application.properties file built into the jar file.这使得它不适合在 jar 文件中内置的application.properties文件中设置。 You can set a $SPRING_DATASOURCE_URL environment variable in your docker-compose.yml instead.您可以在docker-compose.yml中设置$SPRING_DATASOURCE_URL环境变量。

So a typical Compose setup for what you've shown above might look something like:因此,您上面显示的内容的典型 Compose 设置可能类似于:

version: '3.8'
services:
  application:
    build: .             # `docker-compose up --build` will read the Dockerfile
    ports: ['8080:8080'] # host:container port
    environment:
      - SPRING_DATASOURCE_URL=jdbc:postgresql://db/product_db
      - SPRING_DATASOURCE_USERNAME=postgres
      - SPRING_DATASOURCE_PASSWORD=********
  db:
    image: postgres:14   # from Docker Hub, not built locally
    ports: ['5432:5432'] # optional
    volumes:             # persist database data across restarts
      - dbdata:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=product_db 
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=********
volumes:
  dbdata:                # no particular settings for this volume

The SPRING_DATASOURCE_* environment variables set the corresponding spring.datasource.* properties and override the application.properties file. SPRING_DATASOURCE_*环境变量设置相应的spring.datasource.*属性并覆盖application.properties文件。

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

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