搭环境 Postgres + Spring Boot + React 之 Postgres

尝试搭建 Postgres + Spring Boot + React 环境

Staging #1

Postgres + Spring Boot + React

Reference link

Run Postgres Container

docker pull postgres
mkdir -p $HOME/docker/volumes/postgres
docker run --rm --name pg-docker -d -p 5432:5432 \
            -e POSTGRES_PASSWORD=docker \
            -v $HOME/docker/volumes/postgres:/var/lib/postgresql/data \
            postgres

Here is the Postgres info

{
  user: 'postgres',
  host: 'localhost',
  database: 'postgres',
  password: 'docker',
  port: 5432
}

Test connection

By pgAdmin

Run pgAdmin Container (Web based DB client)

docker pull dpage/pgadmin4
docker run -p 80:80 \
    -e 'PGADMIN_DEFAULT_EMAIL=user@domain.com' \
    -e 'PGADMIN_DEFAULT_PASSWORD=SuperSecret' \
    -d dpage/pgadmin4
  • Open http://localhost:80; login with user@domain.com/SuperSecret.

  • Tips:
    To create a connection, we have to get the IP of pg-docker, which is not 127.0.0.1.
    Run this docker inspect to get Postgres ip.

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' pg-docker

By NodeJS Script

1 Like

这只是安装 Postgres 对吧?

Staging #1-1

The goal of Staging # 1 is to setup a dummy project (Postgres + Spring Boot + React).
To learn and build this demo easily, here we break it into 3 small pieces.

  1. Containerize Postgres
  2. Create SpringBoot APIs (:sparkles: we are here)
  3. Visualize data in React

Create SpringBoot APIs

  1. Sketch Restful SpringBoot APIs
  2. Containerize SpringBoot
  3. docker-compose file

Reference

Sketch Restful SpringBoot APIs

SpringBoot(localhost) connect to Postgres(Container).
By following the 1st link, create Restful APIs which write/read data to DB.
SpringBoot is in localhost; Postgres is in container. So, the config of DB is

spring.datasource.url=jdbc:postgresql://localhost:5432/postgres

However, if containerize the SpringBoot, the hostname will be container ip or host name rather than localhost.

Validate the connection b/w Spring and Postgres.

  1. no sql connection issue in console
  2. check the DB session

Swagger

Containerize SpringBoot

Read the 3st link; copy the finalized
docker file; past into the root of project Dockerfile.

FROM openjdk:8-jdk-alpine as build
...
RUN mkdir -p target/dependency && (cd target/dependency; jar -xf ../*.jar)

FROM openjdk:8-jdk-alpine
...
ENTRYPOINT ["java","-cp","app:app/lib/*","com.example.postgresdemo.PostgresDemoApplication"]

Build image:

DOCKER_BUILDKIT=1 docker build -t myorg/myapp .

Write docker-compose file

To run these containers easily, put instance info into docker-compose.yml

version: '3'
services:
  web:
    ...

  db:
    ...

  pgadmin:
    ...

volumes:
  postgres-data:

Don’t forget to change the DB connection url to spring.datasource.url=jdbc:postgresql://db:5432/postgres

Staging #1-0

The goal of Staging # 1 is to setup a dummy project (Postgres + Spring Boot + React).
To learn and build this demo easily, here we break it into 3 small pieces.

  1. Containerize Postgres (:sparkles: we are here)
  2. Create SpringBoot APIs
  3. Visualize data in React

Containerize Postgres

Reference link

Run Postgres Container

docker pull postgres
mkdir -p $HOME/docker/volumes/postgres
docker run --rm --name pg-docker -d -p 5432:5432 \
            -e POSTGRES_PASSWORD=docker \
            -v $HOME/docker/volumes/postgres:/var/lib/postgresql/data \
            postgres

Here is the Postgres info

{
  user: 'postgres',
  host: 'localhost',
  database: 'postgres',
  password: 'docker',
  port: 5432
}

Test connection

Run pgAdmin Container (Web based DB client)

docker pull dpage/pgadmin4
docker run -p 80:80 \
    -e 'PGADMIN_DEFAULT_EMAIL=user@domain.com' \
    -e 'PGADMIN_DEFAULT_PASSWORD=SuperSecret' \
    -d dpage/pgadmin4
  • Open http://localhost:80; login with user@domain.com/SuperSecret.

  • Tips:
    To create a connection, we have to get the IP of pg-docker, which is not 127.0.0.1.
    Run this docker inspect to get Postgres ip.

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' pg-docker

By NodeJS Script