Shaunyl
Shaunyl

Reputation: 594

Connection refused error when starting dockerized Spring Boot and MariaDb with docker-compose

I am trying to dockerize a Spring Boot application alongside its MariaDB database using docker-compose, but I get the following error where I run containers using the command 'docker-compose up'

java.sql.SQLNonTransientConnectionException: Could not connect to address=(host=localhost)(port=3306)(type=master) : Connection refused (Connection refused)

The service api uses the following Dockerfile:

FROM java:8
EXPOSE 8080
ADD target/cm-website-0.0.1-SNAPSHOT.jar cm-website.jar
RUN bash -c 'touch /cm-website.jar'
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "-Dspring.profiles.active=development", "/cm-website.jar"]

My docker-compose.yml file is:

version: '3'

services:
  db:
    container_name: backend-mariadb
    image: mariadb:latest
    environment:
      - MYSQL_ROOT_PASSWORD=pass
      - MYSQL_DATABASE=cmdb
      - MYSQL_USER=cmdb_user
      - MYSQL_PASSWORD=pass
    ports:
      - 3306:3306
  api:
    container_name: backend-api
    image: shaunyl/backend-api
    depends_on:
      - db
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 8080:8080

My application-development.yml file is:

spring:
  profiles: development
  main:
    banner-mode: console
  jpa:
    show-sql: true
    generate-ddl: false
    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
  datasource:
    name: ds-dev-mariadb
    url: jdbc:mariadb://db:3306/cmdb&useSSL=false
    username: cmdb_user
    password: pass
    driver-class-name: org.mariadb.jdbc.Driver

Do you know what the problem could be? Thank you

EDIT 1: Output of docker logs backend-mariadb

2018-08-23 16:15:37 0 [Note] mysqld (mysqld 10.3.9-MariaDB-1:10.3.9+maria~bionic) starting as process 1 ...
2018-08-23 16:15:37 0 [Note] InnoDB: Using Linux native AIO
2018-08-23 16:15:37 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2018-08-23 16:15:37 0 [Note] InnoDB: Uses event mutexes
2018-08-23 16:15:37 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
2018-08-23 16:15:37 0 [Note] InnoDB: Number of pools: 1
2018-08-23 16:15:37 0 [Note] InnoDB: Using SSE2 crc32 instructions
2018-08-23 16:15:37 0 [Note] InnoDB: Initializing buffer pool, total size = 256M, instances = 1, chunk size = 128M
2018-08-23 16:15:37 0 [Note] InnoDB: Completed initialization of buffer pool
2018-08-23 16:15:37 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
2018-08-23 16:15:37 0 [Note] InnoDB: 128 out of 128 rollback segments are active.
2018-08-23 16:15:37 0 [Note] InnoDB: Creating shared tablespace for temporary tables
2018-08-23 16:15:37 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
2018-08-23 16:15:37 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
2018-08-23 16:15:37 0 [Note] InnoDB: 10.3.9 started; log sequence number 1630824; transaction id 21
2018-08-23 16:15:37 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
2018-08-23 16:15:37 0 [Note] Plugin 'FEEDBACK' is disabled.
2018-08-23 16:15:37 0 [Note] Server socket created on IP: '::'.
2018-08-23 16:15:37 0 [Warning] 'proxies_priv' entry '@% root@1b6a13ea879f' ignored in --skip-name-resolve mode.
2018-08-23 16:15:37 0 [Note] InnoDB: Buffer pool(s) load completed at 180823 16:15:37
2018-08-23 16:15:37 0 [Note] Reading of all Master_info entries succeded
2018-08-23 16:15:37 0 [Note] Added new Master_info '' to hash table
2018-08-23 16:15:37 0 [Note] mysqld: ready for connections.
Version: '10.3.9-MariaDB-1:10.3.9+maria~bionic'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  mariadb.org binary distribution

Upvotes: 3

Views: 4993

Answers (2)

mindex
mindex

Reputation: 1636

From the log your service is still trying to contact localhost but not db. Are you sure you've rebuilt your JAR file and / or docker image? Probably your docker-compose is still running an outdated image with old configurations.

Keep in mind that docker-compose up detects only your docker-compose file changes - it knows nothing about your JAR file or Dockerfile changes.

Upvotes: 4

Fabian Rivera
Fabian Rivera

Reputation: 1198

Sounds like you need to configure your DB for accepting non-localhost connections.

Inside of your container, locate this file: /etc/my.cnf and comment out skip-networking and bind-address properties.

[mysqld]
...
#skip-networking
...
#bind-address = <some ip-address>
...

After that, you need to GRANT access to your user (or any other user that you want to use for remote connections). The statement is something like this:

PRIVILEGES ON *.* TO 'root'@'192.168.100.%' IDENTIFIED BY 'my-new-password' WITH GRANT OPTION;

You can find more detailed information here: https://mariadb.com/kb/en/library/configuring-mariadb-for-remote-client-access/

Hope it helps!

Upvotes: 0

Related Questions