M
M
Maxim Isaev2021-12-11 12:30:30
Java
Maxim Isaev, 2021-12-11 12:30:30

How to create a container for postgresq and spring boot applications? And that they interact with each other?

Good afternoon.

There is

dockerfile

FROM adoptopenjdk/openjdk11:x86_64-ubuntu-jre-11.0.13_8
ARG JAR_FILE=target/ShopWarehouse-0.0.1-SNAPSHOT.jar
WORKDIR /opt/app
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]


docker-compose

version: "3.9"
services:
  app:
    build:
      context: .
      dockerfile: dockerfile
  postgres:
    image: "postgres:13.3-alpine"
    environment:
      - DB_DRIVER=org.postgresql.Driver
      - DB_URL=jdbc:postgresql://postgres:5432/shop_warehouse
      - POSTGRES_DB=shop_warehouse
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=12332112
    ports:
      - "5432:5432"


How to make the application see the database?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2021-12-11
Hasanly @azerphoenix

Good afternoon!
Firstly, when connecting jdbc, you need to specify the name of the service (postgres), instead
of localhost Secondly,
you need to add to yml

networks:
  myApp:
    name: myApp
    driver: bridge

And also add each service to the network
ports:
      - "5432:5432"
networks:
      - myApp

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question