1

Any help will be appreciated. I am trying to run mysql and spring-boot app in two container but, in same netowrk. Using following command two container communicate each other. mysql command docker run --name mysql-local --network=spring-boot-network -p 4000:3306 -d -e MYSQL_ROOT_PASSWORD=password -e MYSQL_USER=root-user -e MYSQL_PASSWORD=password -e MYSQL_DATABASE=testdb mysql application command docker run --network=spring-boot-network -p 8080:8080 --name test-app -e RDH_HOSTNAME:mysql-local -e RDS_PORT:4000 -e RDS_USER:root-user -e -e RDS_PASSWORD:password 171ce195f461

using above tow docker command two containers are created in spring-boot-network and they can communicate each other. problem arise when I tried to use docker-compose. I passed same environments as command line but, my app refuse to connect with mysql.

I am using spotify plugin to create an image.

<plugin>
                <groupId>com.spotify</groupId>
                <artifactId>dockerfile-maven-plugin</artifactId>
                <version>1.4.3</version>
                <executions>
                    <execution>
                        <id>default</id>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <repository>rajesh132/test-docker</repository>
                    <tag>${project.version}</tag>
                    <buildArgs>
                        <JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
                    </buildArgs>
                </configuration>
            </plugin>

when I startup container mysql container is created and started. I can connect to it using mysqlsh client and mysql workbench as well. However, I cannot connect to my application. Following are properties and docker files that I used in my project. Docker file

ADD target/docker-test.jar docker-test.jar
ENTRYPOINT ["java", "-jar", "docker-test.jar"]

Docker Compose file

version: '3.7'
services:
  spirng-boot-test-application:
    image: rajesh132/test-docker:0.0.1-SNAPSHOT
      #build:
      #context: .
    #dockerfile: Dockerfile
    ports:
      - "8080:8080"
    restart: always
    depends_on: # Start the depends_on first
      - mysql-app
    environment:
      RDS_HOSTNAME: mysql-app
      RDS_PORT: 4000
      RDS_DB_NAME: testdb
      RDS_USERNAME: test-user
      RDS_PASSWORD: password
    networks:
      - web-application-network

  mysql-app:
    image: mysql
    ports:
      - "4000:3306"
    restart: always
    environment:

      MYSQL_ROOT_PASSWORD: password
      MYSQL_USER: test-user
      MYSQL_PASSWORD: password
      MYSQL_DATABASE: testdb
    volumes:
      - mysql-database-data-volume:/var/lib/mysql
    networks:
      - web-application-network

# Volumes
volumes:
  mysql-database-data-volume:

networks:
  web-application-network:

application.yml

  datasource:
    #  use this url for localhost
    #use docker container name instead 'localhost'
    #  url: jdbc:mysql://localhost:3306/authorizationDb?createDatabaseIfNotExist=true
    url: jdbc:mysql://${RDS_HOSTNAME:localhost}:${RDS_PORT:3306}/${RDS_DB_NAME:test}?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true
    username: ${RDS_USER:root}
    password: ${RDS_PASSWORD:password}
    driver-class-name: com.mysql.cj.jdbc.Driver
    initialization-mode: always
    tomcat:
      test-on-borrow: true
      validation-query: SELECT 1
  jpa:
    hibernate:
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
      ddl-auto: update
    properties:
      hibernate:
        show_sql: true
        format_sql: true
    database-platform: org.hibernate.dialect.MySQL8Dialect

logging:
  level:
    org:
      hibernate:
        type: trace

I followed so many blogs, documentation, however coundnt succeed to build. Any help much appreciate.

1 Answer 1

1

Your RDS_PORT: 4000 must be RDS_PORT: 3306 instead.

When you expose a container port as "4000:3306", the port 4000 can only be used from the host machine. Within the container network, you need to use 3306.

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

2 Comments

Mysql is running on my host machine. I changed port to 3306 and docker engine gives an error like port already used.
Damon... finally, I get it. For outside docker using 4000 port it can communicate to container. Docker container internally communicate to 3306 port. Damn....

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.