Borzi
Borzi

Reputation: 577

Access to local database denied through docker container

I am having a problem connecting my compiled Spring-Boot app to the database that I have running on another container on my server.

I have tried different configurations, changing from localhost to the IP address of my server for the connection. I also double checked that the credentials matched by logging in via Adminer. Finally, I did a rebuild of the compose and image files several times to ensure that I have all the latest versions.

Compose file:

version: '3.1'
services:
 db:
  image: mariadb
  restart: always
  environment:
   MYSQL_ROOT_PASSWORD: mypassword
   MYSQL_DATABASE: marketingappdb
  ports:
   - "3306:3306"
  expose:
   - 3306
  volumes:
   - ./mariadbvolume:/var/lib/mariadb
  networks:
   - marketingapp
 adminer:
  image: adminer
  restart: always
  ports:
   - "8086:8080"
  expose:
   - 8086
  depends_on:
   - db
  networks:
   - marketingapp
 springserver:
  image: marketingapp
  restart: always
  ports:
   - "8091:8091"
  expose:
   - 8091
  depends_on:
   - db
  networks:
   - marketingapp
networks:
  marketingapp:

Spring Server Image:

FROM openjdk:latest
COPY /marketing-app-final.jar .
EXPOSE 8091
ENTRYPOINT ["java", "-jar", "marketing-app-final.jar"]

Application properties for Spring:

server.port = 8091
spring.datasource.url=jdbc:mariadb://0.0.0.0:3306/marketingappdb
spring.datasource.username=root
spring.datasource.password=mypassword
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update

I can connect from my PC to the database using the app from the remote same configuration (obviously replacing localhost with the IP) and don't see why I shouldn't be able to do the same from the actual server. Thanks in advance for any help!

Upvotes: 0

Views: 385

Answers (1)

dschuldt
dschuldt

Reputation: 657

Use the docker dns to connect your spring App to the mariabd:

jdbc:mariadb://db:3306/marketingappdb

Just a few other hints: you don't need to expose port 3306, you already bind it to 3306 on Host (if you just want to use it from within the docker Services you don't need to bind/expose it at all). And the mariabd persistent storage is var/lib/mysql and not var/lib/mariadb

Upvotes: 2

Related Questions