Hi!
Im fairly new to docker, and today i run into this issue. I have a spring boot app, an angular frontend and a MySQL database, which works fine in my local environment. The spring boot app can connect to the database, i see it in MySQL Workbench.
I tried to dockerise this app, and it seems that my spring app can’t connect to the database in the docker container.
Here is my compose file:
services:
mysql:
image: mysql:8.0
container_name: mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: moa_db
ports:
- "3307:3306"
volumes:
- mysql_data:/var/lib/mysql
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "mysql", "--silent"]
interval: 10s
retries: 5
start_period: 30s
timeout: 5s
restart: unless-stopped
networks:
- mynetwork
backend:
build:
context: ./backend
dockerfile: dockerfile
container_name: spring-boot-app
ports:
- "8080:8080"
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://mysql:3306/moa_db?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&createDatabaseIfNotExist=true
SPRING_DATASOURCE_USERNAME: root
SPRING_DATASOURCE_PASSWORD: root
depends_on:
mysql:
condition: service_healthy
restart: on-failure
networks:
- mynetwork
frontend:
build:
context: ./frontend
dockerfile: dockerfile
container_name: angular-app
ports:
- "4200:80"
depends_on:
- backend
networks:
- mynetwork
volumes:
mysql_data:
networks:
mynetwork:
driver: bridge
And here is my 2 dockerfile:
Backend:
FROM gradle:7.4.2-jdk11 AS builder
WORKDIR /app
COPY . .
RUN gradle build --no-daemon
FROM openjdk:11-jdk-slim
WORKDIR /app
COPY --from=builder /app/build/libs/moa-backend-0.0.1-SNAPSHOT.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]
FrontEnd:
FROM node:14-alpine as build
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build --prod
FROM nginx:alpine
COPY --from=build /app/dist/moa-frontend /usr/share/nginx/html
EXPOSE 80
I tried with creating a network bridge, but it didn’t help and i also read that docker now has a built in network.
A fun part is, i tried changing the connection url in the compose file to
SPRING_DATASOURCE_URL: jdbc:mysql://host.docker.internal:3306/moa_db?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&createDatabaseIfNotExist=true
And this resulted in a short success, if i had MySQL workbench open, it connected to it locally, but when i closed it, it also didn’t work.
I have the ports like this: “3307:3306” because it told me that locally i use 3306.
Any suggestions what am i doing wrong?
Thanks for the help in advance!