0

I have created a NodeJS app and built an image and ran it. In that container app, I am trying to access the MySQL database from localhost (MySQL is in the host). But, when I try to access it, I am getting the following error

enter image description here

I have looked for so many solutions from StackOverflow and other resources. But, couldn't solve the issue. Even I tried to grant access to user root from all the hosts using the below

CREATE USER 'root'@'%' IDENTIFIED BY 'passw0rd';

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;

Still, I am facing the same issue.

Docker file:

FROM node:14
WORKDIR /usr/testapp
COPY package*.json ./
RUN npm install && npm install typescript -g
COPY . .
RUN tsc
CMD [ "node", "./dist/app.js" ]

Can you please help me solve this issue?

Thank you...

EDIT:

ormconfig.json

{
   "type": "mysql",
   "host": "localhost",
   "port": 3306,
   "username": "root",
   "password": "passw0rd",
   "database": "hostelme",
   "synchronize": true,
   "logging": false,
   "entities": [
      "src/entity/**/*.ts"
   ],
   ...
}

Commands to build and run the container:

docker build -t hnb-app .    # To build
docker run -p 3000:3000  hnb-app    # To run
O. Jones
  • 92,698
  • 17
  • 108
  • 152
Sai
  • 2,590
  • 4
  • 31
  • 42
  • 2
    `ECONNREFUSED` refused doesn't mean, that MySQL server refused connection because of credentials, but that there is no process listening on `127.0.0.1:3306` – derpirscher May 28 '21 at 09:41
  • @derpirscher, can you please explain it and let me know how can I solve it? – Sai May 28 '21 at 09:51
  • How did you start your container? Can you try starting the mysql container without your app verifiying that mysql correctly starts within the container? – derpirscher May 28 '21 at 09:51
  • 1
    If the database is running on host (eg. a service, but not as a docker container) then from your dockerized app you should access it under `host.docker.internal:3306`. If it's running in a container, then you should connect using docker dns with container name eg. `http://mysql-container:3306`. Remember to specify correct port as well. – nluk May 28 '21 at 09:52
  • @derpirscher, I am not sure how can I verify it when starting the app. Can you please help me with that? – Sai May 28 '21 at 09:54
  • @nluk, Do I need to use `host.docker.internal` in place of `localhost`? – Sai May 28 '21 at 09:55
  • Did you expose mysql port? – Schwarz54 May 28 '21 at 10:03
  • @Schwarz54, MySQL is not in the docker container. It is in the host. – Sai May 28 '21 at 10:09
  • can you share the command you are running to run your container app? – Schwarz54 May 28 '21 at 10:14
  • @Schwarz54, I updated the question. please check and let me know if any further info is needed. – Sai May 28 '21 at 10:18
  • If MySql is on the host, you cannot use 127.0.0.1 as ipaddress because that would be the container itself. You have to use the ipaddress of the host machine ... – derpirscher May 28 '21 at 10:22
  • Try this: `docker run -p 3000:3000 --network="host" hnb-app` – Schwarz54 May 28 '21 at 10:25

0 Answers0