0
I created a file docker-compose.yml
to load a Mysql database and a Nodejs server:
version: '3'
services:
database:
image: mysql:5.7
container_name: 'database'
restart: 'always'
volumes:
- ./src/database/data:/var/lib/mysql
ports:
- '3306:3306'
expose:
- '3306'
networks:
- backend
environment:
MYSQL_USER: 'root'
MYSQL_ROOT_PASSWORD: 'root'
MYSQL_DATABASE: 'data-mysql'
api:
image: node:10.16-slim
container_name: 'api'
ports:
- '8000:8000'
working_dir: '/home/node/app'
volumes:
- ./:/home/node/app
command: 'yarn server'
networks:
- backend
networks:
backend:
driver: 'bridge'
In my terminal, when I give the command: docker-compose up -d
, appears:
Creating network "backend_backend" with driver "bridge"
Creating node-api ...
Creating my-database ...
Creating my-database
Creating my-database ... done
Viewing active containers with the command docker ps
, appears:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fca4c78e70c5 node:10.16-slim "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 0.0.0.0:8000->8000/tcp node-api
f9702dbec8c5 mysql:5.7 "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp my-database
After all this, when will I see the container logs node-api
, with the command docker logs -f fca4c78e70c5
, appears:
[nodemon] starting `ts-node src/server.ts src/server.ts`
(node:41) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)
--------------------
at Protocol._enqueue (/home/node/app/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/home/node/app/node_modules/mysql/lib/protocol/Protocol.js:51:23)
at PoolConnection.connect (/home/node/app/node_modules/mysql/lib/Connection.js
I’m doing a project with Typeorm, a Typescript ORM, the configuration file and connection to the database is:
{
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "root",
"password": "root",
"database": "data-mysql",
"logging": true,
"entities": [
"src/models/**/*.ts"
],
"migrations": [
"src/database/migrations/**/*.ts"
],
"cli": {
"entitiesDir": "src/models",
"migrationsDir": "src/database/migrations"
}
}
I’ve done a lot of research and I can’t find anything to solve this problem, someone can help me?
Thanks for all your answers! :)