0

I'm building a tool to help me in my final paper at the uni, so I'm doing an executable script in Nodejs to help me dump data from a specific type of file to some databases. As the script can lead to a long period of time to finish the execution the idea was: use two containers and connect them, so I can to execute my script in background in some cloud services, but I can't make the connection to MySQL work.

Dockerfile

ubuntu is not required here, I already tested it with bash, debian and alpine

FROM ubuntu:18.04
RUN apt-get update && apt-get upgrade && apt-get clean && mkdir /usr/code
COPY ./my-tool /usr/code/
COPY ./data-to-dump /usr/code/
WORKDIR /usr/code
CMD ./my-tool --input ./data-to-dump -database database --user root --password root --hostname mysql_container

In the --hostname I already used mysql and host.docker.internal

Docker Compose

version: '3.7'
services:
  mysql:
    container_name: mysql_container
    image: mysql:8.0
    command: mysqld --default-authentication-plugin=mysql_native_password --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: 'root'
    volumes:
      - '.data/mysql-data/data:/var/lib/mysql'
      - '.data/mysql-data/my.cnf:/etc/mysql/conf.d/my.cnf'
      - '.data/mysql-data/sql:/docker-entrypoint-initdb.d'
    expose:
      - '3306'
  ubuntu:
    container_name: ubuntu_container
    build:
      context: .
      dockerfile: .Dockerfile
    depends_on:
      - mysql
    volumes:
      - '.data/ubuntu-data:/data'
    cap_add:
      - SYS_ADMIN
      - NET_ADMIN

Network also doesn't work

Nodejs File

Beforehand, I'm connecting MySQL like this:

import mysql from 'mysql2/promise'

// ...

class MySQLPlugin {

// ...

  async connect() {
    //...
    let connection = await mysql.createConnection({
    host: this.hostname,
    port: this.port,
    user: this.user,
    password: this.password,
  });

// ...
}

Error message

2023-07-14 00:07:10 node:internal/process/promises:288
2023-07-14 00:07:10             triggerUncaughtException(err, true /* fromPromise */);
2023-07-14 00:07:10             ^
2023-07-14 00:07:10 
2023-07-14 00:07:10 Error: connect ECONNREFUSED 192.168.0.2:3306
2023-07-14 00:07:10     at Object.createConnection (/snapshot/dist/my-tool.js)
2023-07-14 00:07:10     at MySQLPlugin._callee$ (/snapshot/dist/my-tool.js)
2023-07-14 00:07:10     at tryCatch (/snapshot/dist/my-tool.js)
2023-07-14 00:07:10     at Generator.<anonymous> (/snapshot/dist/my-tool.js)
2023-07-14 00:07:10     at Generator.next (/snapshot/dist/my-tool.js)
2023-07-14 00:07:10     at asyncGeneratorStep (/snapshot/dist/my-tool.js)
2023-07-14 00:07:10     at _next (/snapshot/dist/my-tool.js)
2023-07-14 00:07:10     at /snapshot/dist/my-tool.js
2023-07-14 00:07:10     at new Promise (<anonymous>)
2023-07-14 00:07:10     at MySQLPlugin.<anonymous> (/snapshot/dist/my-tool.js) {
2023-07-14 00:07:10   code: 'ECONNREFUSED',
2023-07-14 00:07:10   errno: -111,
2023-07-14 00:07:10   sqlState: undefined
2023-07-14 00:07:10 }
2023-07-14 00:07:10 
2023-07-14 00:07:10 Node.js v18.5.0

The tool itself is working great, I already tested with standard MySQL server in my machine and using container as well. The problem just happen when I'm connecting two containers.

I expect to find what seems to be the problem to connect the two containers and fix it

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 1
    So, is the MySQL server in a different container from the connecting process? Are you sure the network is bridged to allow the two to communicate? – Tim Roberts Jul 14 '23 at 03:31
  • Is it possible your application is starting before the database is fully initialized? If you run `docker-compose up -d`, and you get this error, and then you try it a second time, does it work the second time? – David Maze Jul 14 '23 at 10:15
  • @TimRoberts I tried to put the network flag on both containers as well, no results on this. I also initialize other ubuntu container to run some tests and used ping to the container `ping host:port`, it worked fine – Juliana Aragão Jul 14 '23 at 17:19
  • @DavidMaze man you were totally right! Just add the command sleep for a couple of minutes before running the script and it worked! I thought the flag depends on were taking care of it but It was my mistake – Juliana Aragão Jul 14 '23 at 18:21
  • This is discussed at further length in [Docker Compose wait for container X before starting Y](https://stackoverflow.com/questions/31746182/docker-compose-wait-for-container-x-before-starting-y). Running `docker-compose up -d` twice is a workaround, but more confirms that "not waiting long enough" is the problem. – David Maze Jul 14 '23 at 20:27
  • We don't do [Solved] here. – user207421 Jul 15 '23 at 02:19
  • srry, first time making questions – Juliana Aragão Jul 15 '23 at 02:20

0 Answers0