skip to Main Content

Spring Boot + MySQL + Docker here. I have the following docker-compose.yml:

version: "3.8"

services:
  myappws_mono_db:
    image: mysql:8.0.32
    restart: unless-stopped
    environment:
      - MYSQL_ROOT_PASSWORD=$MAIN_DB_ROOT_PASSWORD
      - MYSQL_DATABASE=$MAIN_DB_DATABASE
      - MYSQL_CHARSET=utf8mb4
      - MYSQL_COLLATION=utf8mb4_0900_ai_ci
      - MYSQL_ENGINE=InnoDB
    ports:
      - $MAIN_DB_PORT:$MAIN_DB_DOCKER_PORT
    volumes:
      - db:/var/lib/mysql
  myappws:
    depends_on:
      - myappws_mono_db
    build: .
    restart: on-failure
    environment:
      - SERVER_PORT=$SERVER_PORT
      - MAIN_DB_HOST=myappws_mono_db
      - MAIN_DB_PORT=$MAIN_DB_DOCKER_PORT
      - MAIN_DB_DATABASE=$MAIN_DB_DATABASE
      - MAIN_DB_USERNAME=$MAIN_DB_USERNAME
      - MAIN_DB_PASSWORD=$MAIN_DB_PASSWORD
    ports:
      - $SERVER_PORT:$WS_DOCKER_PORT
volumes:
  db:

The following .env file:

MAIN_DB_HOST=127.0.0.1
MAIN_DB_PORT=3307
MAIN_DB_DOCKER_PORT=3306
MAIN_DB_USERNAME=root
MAIN_DB_PASSWORD=123456
MAIN_DB_ROOT_PASSWORD=123456
MAIN_DB_DATABASE=myapp_db_local
SERVER_PORT=9200
WS_DOCKER_PORT=9201

The following Dockerfile:

FROM eclipse-temurin:11
MAINTAINER myapp.io

RUN mkdir /opt/app

COPY app/build/libs/myapp-ws.jar myapp-ws.jar

ENTRYPOINT ["java","-jar","myapp-ws.jar"]

When I run ./gradlew clean bootJar && docker-compose --env-file .env up -d both MySQL and Spring Boot containers start up just fine but the Spring Boot API is not available from my host machine via curl:

curl -ik -H "Accept: application/json" -H "Content-Type: application/json" -d '{"name":"ACME, Inc.", "website":"http://example.com"}' -X POST 'http://127.0.0.1:9201/ap1/v1/vendors/'
curl: (7) Failed to connect to 127.0.0.1 port 9201: Connection refused

When I try port 9200 (just in case) I get a similar but different error:

curl -ik -H "Accept: application/json" -H "Content-Type: application/json" -d '{"name":"ACME, Inc.", "website":"http://example.com"}' -X POST 'http://127.0.0.1:9200/ap1/v1/vendors/'
curl: (52) Empty reply from server

So clearly the Spring Boot container is not exposing its port to my host machine, but for the life of me, I’m not sure how/where to change what!?

2

Answers


  1. The myappws is trying to connect to a MySQL db running inside the same container as the app (which is not successful since the MySQL runs in a different container).

    Instead of using 127.0.0.1, try to use the service name myappws_mono_db as the MySQL hostname: MAIN_DB_HOST=myappws_mono_db since this will resolve to the MySQL container address by docker-compose.

    Login or Signup to reply.
  2. The issue is with the ports defined in your docker-compose file (they are flipped).

    In your case 9200 is the internal port (where you are running spring-boot) and 9201 the external.

    The internal port should go on the right side, and the external port (the one you want to expose) should go on the left side.

      ports:
      - $EXTERNAL_PORT:$INTERNAL_PORT
    

    More info here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search