简体   繁体   English

Java 容器无法使用 docker-compose 连接到 MYSQL 容器

[英]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我试图将我的 Java 容器连接到我的 MYSQL 容器,但我收到503 错误

HTTP ERROR 503
Problem accessing /. Reason:
    Service Unavailable

docker-compose file: docker-compose 文件:

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 I got the JAVA src files,i just used maven localy to build it and use target for the Java Dockerfile

java app dockerfile: java 应用程序 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:在您的 docker 文件中将您的 DB_URL 更新为:

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

If you use localhost your container will loopback to itself, and never hit the network.如果您使用 localhost,您的容器将回送至自身,并且永远不会访问网络。

In your docker-compose yml file, you have a typo in the url, try updating to:在您的 docker-compose yml 文件中,您在 url 中有错字,请尝试更新为:

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

As an aside, using depends_on does not wait for the service to be ready.顺便说一句,使用depends_on不会等待服务准备好。 It simply dictates start order as the documentation states :正如文档所述,它只是规定了开始顺序:

There are several things to be aware of when using depends_on:使用depends_on时有几点需要注意:

depends_on does not wait for db and redis to be “ready” before starting web - only until they have been started.在启动 web 之前,depends_on 不会等待 db 和 redis “准备好” - 仅在它们启动之前。 If you need to wait for a service to be ready...如果您需要等待服务准备好...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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