Adonis + docker + mysqkl : connect ECONNREFUSED

Hello,

I dockerise an Adonis App with a mysql connexion.

Migration works fine. I see my user table on mysql.
But when I try to acces on the webrowser on “http://0.0.0.0:3333/db” :

I have this error :

connect ECONNREFUSED 127.0.0.1:3306

My files :

routes.js
Route.get(’/db’, async () => {
return await Database.table(‘users’).select(’*’)
})

docker-compose.yml
version: ‘3’

services:
adonis-mysql:
image: mysql:5.7
ports:
- ‘3306:3306’
volumes:
- $PWD/data:/var/lib/mysql
environment:
MYSQL_USER: ${DB_USER}
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_ALLOW_EMPTY_PASSWORD: ${DB_ALLOW_EMPTY_PASSWORD}
networks:
- api-network
adonis-api:
container_name: “${APP_NAME}-api”
build:
context: .
dockerfile: Dockerfile
volumes:
- .:/app
- /app/node_modules
ports:
- “3333:3333”
depends_on:
- adonis-mysql
networks:
- api-network

networks:
api-network:

.env
HOST=0.0.0.0
PORT=3333
NODE_ENV=development
APP_URL=http://${HOST}:${PORT}
CACHE_VIEWS=false
APP_KEY=************************
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_USER=root
DB_PASSWORD=
DB_DATABASE=adonis
SESSION_DRIVER=cookie
HASH_DRIVER=bcrypt
DB_ALLOW_EMPTY_PASSWORD=yes
APP_NAME=tool

database.js
connection: Env.get(‘DB_CONNECTION’, ‘mysql’),
mysql: {
client: ‘mysql’,
connection: {
host: Env.get(‘DB_HOST’, ‘127.0.0.1’),
port: Env.get(‘DB_PORT’, ‘3306’),
user: Env.get(‘DB_USER’, ‘root’),
password: Env.get(‘DB_PASSWORD’, ‘’),
database: Env.get(‘DB_DATABASE’, ‘adonis’)
}
},

Can you help me ?
Thank you

Hi @maxime and welcome to community!

You can use triple ` to format your code

```
Code here
```

// docker-compose.yml
version: ‘3’

services:
  adonis-mysql:
    image: mysql:5.7

When using docker-compose DB host must be same as service name. adonis-mysql in current case.
Set DB host from DB_HOST=127.0.0.1 to adonis-mysql

2 Likes

Hello,

It works great now.
Thank you so much McSneaky