1

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" ]
4
  • MySQL might be getting started after your java app, spring tries to connect to database at startup. Commented Jun 8, 2021 at 14:04
  • Hmmm..even if there is "depends_op" ? how do you suggest to fix this Commented Jun 8, 2021 at 14:10
  • You are using my-db in your connection string as host name but the hostname is my_db based on the service definition. Commented Jun 8, 2021 at 15:21
  • thanks for the suggestion,fixed it but still not working.. Commented Jun 8, 2021 at 15:28

1 Answer 1

1

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...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.