0

I am new to Docker and I'm trying to dockerize my application. I'm using MySQL and Spring Boot.
When I use docker-compose up, a few exception occurs:

todoapp-container_1  | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
todoapp-container_1  |  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_111]
todoapp-container_1  |  at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_111]
todoapp-container_1  |  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_111]

and a little bit down:

todoapp-container_1  | Caused by: java.net.ConnectException: Connection refused (Connection refused)
todoapp-container_1  |  at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_111]
todoapp-container_1  |  at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_111]
todoapp-container_1  |  at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_111]

Here is my docker-compose:

version: '3'
services:
    mysql-container:
        image: mysql:8
        environment:
            - MYSQL_ROOT_PASSWORD=password
            - MYSQL_DATABASE=todo
            - MYSQL_PASSWORD=todo
            - MYSQL_USER=todo
    todoapp-container:
        image: todoapp-service
        ports:
            - 8080:8080
        build:
            context: .
            dockerfile: Dockerfile
        depends_on:
            - mysql-container

and my application.properties:

#SERVER
server.port=8080
#MYSQL
spring.datasource.url=jdbc:mysql://mysql-container:3306/todo?createDatabaseIfNotExist=true
spring.datasource.username=todo
spring.datasource.password=todo
spring.jpa.generate-ddl=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
#JACKSON SERIALIZATION FOR NULL FIELDS
spring.jackson.default-property-inclusion=non_null

The thing is, if I run docker-compose up for the second time, it works flawless without any exceptions and can use the postman for requests. I assume there is a problem at the moment when the containers are starting, I think I have to make the todoapp-container to wait until the mysql-container starts but I am not really sure about it.

myeongkil kim
  • 2,465
  • 4
  • 16
  • 22

1 Answers1

0

You can control the order of service startup and shutdown with the depends_on option.

However, for startup Compose does not wait until a container is “ready” (whatever that means for your particular application) - only until it’s running.

To handle this, design your application to attempt to re-establish a connection to the database after a failure. If the application retries the connection, it can eventually connect to the database.

There are other suggestions in the official documentation that are worth to look at :)

ikos23
  • 4,879
  • 10
  • 41
  • 60