'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. 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. 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 e.g. 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. Is that true?

2. When Dockerfile is executed? 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. 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. 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). 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: create table public.product (id bigserial not null, name varchar(255), code varchar(255), primary key (id))



Solution 1:[1]

  1. In fact, the docker-compose.yml file is only used by docker-compose up and other docker-compose commands. Plain docker commands and non-Docker java commands don't look at it at all.

  2. The Dockerfile is read when you run docker-compose build, or docker-compose up --build, or docker build. There are also Maven and Gradle extensions to build Docker images. 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.

  3. Spring has several ways to set properties. 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; that makes this something that's inappropriate to set in an application.properties file built into the jar file. You can set a $SPRING_DATASOURCE_URL environment variable in your docker-compose.yml instead.

So a typical Compose setup for what you've shown above might look something like:

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David Maze