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