I am creating a simple Spring Boot application using PostgreSQL. I create 2 images and containers using ‘docker-compose up’ and I have also migrated my database table into container using Flyway.
The problem is how do I move my data or how do I access the database data that is on my local machine on my Docker container application?
I run my application on Docker container and use Get HTTP method and there is no data. I get returned an empty JSON, while if I run it not using Docker I get the data. Maybe anyone knows how I can migrate the data or somehow access it that I have stored on my local machine?
My docker-compose.yaml file looks like this:
version: '3.8'
services:
app:
container_name: docker-meetings
image: docker_meetings
build: ./
ports:
- "8082:8080"
depends_on:
- postgresqldb
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://postgresqldb:5432/meetingsdb?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true&stringtype=unspecified
- SPRING_DATASOURCE_USERNAME=postgres
- SPRING_DATASOURCE_PASSWORD=DB
- SPRING_JPA_HIBERNATE_DDL_AUTO=none
postgresqldb:
image: postgres
restart: always
ports:
- "5432:5432"
volumes:
- ./Program Files/PostgreSQL/14/data
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=DB
- POSTGRES_DB=meetingsdb
- PGDATA=./Program Files/PostgreSQL/14/data
volumes:
postgresqldb: {}
And my Dockerfile looks like this:
FROM openjdk:11
ADD target/visma-meeting-app-withdb-0.0.1-SNAPSHOT.jar visma-meeting-app-withdb-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java","-jar","visma-meeting-app-withdb-0.0.1-SNAPSHOT.jar"]
2
Answers
Maybe, the problem is about volumes
You can try this, Data will store in ./pgdata in local mechine and store in postgres in docker container.
Reusing the same storage that you already have for a local postgresql installed directly in your computer might be possible (I am not sure about that). Probably both postgresql must have exactly the same version, and additionally you will only be able to have only one installation running at the same time.
Nevertheless the recommended way would be to have two different storages, one for each installation. Then, as you say, you will need to export data from your origin database in your computer, and import it into your container database.
You can do that using pgadmin, or pgdump. Maybe the more user friendly way is to use pgadmin. You can do that from a locally installed pgadmin that connects to both your local (not-containerized) postgresql, and to the containerized postgresql.
In order to access both of them easily you will need to map the port used by the containerized postgresql to a local port different than the one used by your non-container postgresql. For that, you might want to change the 5432:5432 default mapping (just if your other postgresql is already using that port).
Then if you have remote access to both of them, you can configure pgadmin in order to manage both databases, and the export-import task is the same as with any other two postgresql installations.