简体   繁体   中英

Java container cant connect to MYSQL container with docker-compose

I was given a multy-steps task and im stuck !!

im trying to connect my Java container to my MYSQL container,but im getting 503 ERROR

HTTP ERROR 503
Problem accessing /. Reason:
    Service Unavailable

docker-compose file:

version: "3.3" 

services: 
    lavagna:
        build: .
        ports:
            - "8080:8080"
        networks: 
            - back_net
        depends_on:
            - my_db
        environment: 
            spring.datasource.url: "jdbc:mysql://my-db:3306/lavagna" 
    my_db: 
        image: mysql:5.7
        ports: 
            - "3306:3306" 
        networks: 
            - back_net
        volumes: 
            - $PWD/mysql:/var/lib/mysql
        environment: 
            MYSQL_ROOT_PASSWORD: 123
            MYSQL_USER: eyal
            MYSQL_PASSWORD: 123
networks: 
    back_net:
        driver: bridge

I got the JAVA src files,i just used maven localy to build it and use target for the Java Dockerfile

java app dockerfile:

FROM openjdk:8-jre-alpine
EXPOSE 8080
COPY ./target/. .
COPY ./entrypoint.sh . 
ENV DB_DIALECT MYSQL
ENV DB_URL jdbc:mysql://localhost:3306/lavagna
ENV DB_USER "root"
ENV DB_PASS "123"
ENV SPRING_PROFILE dev
RUN apk update \
    && apk add ca-certificates \
    && update-ca-certificates && apk add openssl
RUN chmod 774 entrypoint.sh
ENTRYPOINT [ "./entrypoint.sh" ]

I think you need a combination of comments and answers given already. Your containers are on the same network, so it appears to boil down to configuration.

In your docker file update your DB_URL to:

ENV DB_URL jdbc:mysql://my_db:3306/lavagna

If you use localhost your container will loopback to itself, and never hit the network.

In your docker-compose yml file, you have a typo in the url, try updating to:

spring.datasource.url: "jdbc:mysql://my_db:3306/lavagna"

As an aside, using depends_on does not wait for the service to be ready. It simply dictates start order as the documentation states :

There are several things to be aware of when using depends_on:

depends_on does not wait for db and redis to be “ready” before starting web - only until they have been started. If you need to wait for a service to be ready...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM