I have an application created, a Node.js server and a MySQL database.
Initially, for this application, Connection with the database is installed via port 3306, the server starts on 3000.
I created 2 containers for Docker, one for the database, the other for the server.
I assigned ports 3307 and 3001 to listen to both. But I get an error connecting to port 3307. For the experiment I set it to 3306, but it’s clear that there is an error here as the port is already occupied by the initial connection.
I looked at different answers to stop port 3306 in Connection, will this affect the operation of the application, how will the Docker container replace it? and how should I do this?
I work with the database via MySQL Workbench
Dockerfile:
ARG NODE_VERSION=18.14.0
ARG DB_HOST=mysql-container
ARG DB_PORT=3307
ARG DB_USER=root
ARG DB_PASSWORD=...
ARG DB_NAME=...
FROM node:${NODE_VERSION}-alpine
ENV DB_HOST=$DB_HOST
ENV DB_PORT=$DB_PORT
ENV DB_USER=$DB_USER
ENV DB_PASSWORD=$DB_PASSWORD
ENV DB_NAME=$DB_NAME
ENV NODE_ENV production
WORKDIR /...
RUN --mount=type=bind,source=package.json,target=package.json
--mount=type=bind,source=package-lock.json,target=package-lock.json
--mount=type=cache,target=/root/.npm
npm ci --omit=dev
USER node
COPY ...
EXPOSE 3000
CMD npm start
compose.yaml:
version: '3'
services:
server-container:
build:
context: .
dockerfile: Dockerfile
ports:
- "3001:3001"
depends_on:
- mysql-container
mysql-container:
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: pass
MYSQL_DATABASE: DBName
MYSQL_ALLOW_EMPTY_PASSWORD: "no"
ports:
- "3307:3307"
error
backend-server-container-1 |
backend-server-container-1 | > [email protected] start
backend-server-container-1 | > node server.js
backend-server-container-1 |
backend-server-container-1 | Server started 3000
backend-server-container-1 | node:events:491
backend-server-container-1 | throw er; // Unhandled 'error' event
backend-server-container-1 | ^
backend-server-container-1 |
backend-server-container-1 | Error: connect ECONNREFUSED 127.0.0.1:3307
backend-server-container-1 | at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16)
backend-server-container-1 | Emitted 'error' event on Connection instance at:
backend-server-container-1 | at Connection._notifyError (/backend/node_modules/mysql2/lib/connection.js:252:12)
backend-server-container-1 | at Connection._handleFatalError (/backend/node_modules/mysql2/lib/connection.js:183:10)
backend-server-container-1 | at Connection._handleNetworkError (/backend/node_modules/mysql2/lib/connection.js:196:10)
backend-server-container-1 | at Socket.emit (node:events:513:28)
backend-server-container-1 | at emitErrorNT (node:internal/streams/destroy:151:8)
backend-server-container-1 | at emitErrorCloseNT (node:internal/streams/destroy:116:3)
backend-server-container-1 | at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
backend-server-container-1 | errno: -111,
backend-server-container-1 | code: 'ECONNREFUSED',
backend-server-container-1 | syscall: 'connect',
backend-server-container-1 | address: '127.0.0.1',
backend-server-container-1 | port: 3307,
backend-server-container-1 | fatal: true
backend-server-container-1 | }
backend-server-container-1 |
backend-server-container-1 | Node.js v18.14.0
backend-server-container-1 | npm notice
backend-server-container-1 | npm notice New major version of npm available! 9.3.1 -> 10.2.3
backend-server-container-1 | npm notice Changelog: <https://github.com/npm/cli/releases/tag/v10.2.3>
backend-server-container-1 | npm notice Run `npm install -g [email protected]` to update!
backend-server-container-1 | npm notice
backend-server-container-1 exited with code 1
2
Answers
MySQL listens on port 3306, so no matter what host port you map it to, the container side of the mapping should always be 3306.
- "3307:3307"
should be- "3307:3306"
Also note that you only need to map the port on the database container to a host port if you need to access the database from outside the docker network. If the only thing that needs to talk to the database is
server-container
then you don’t need to map the port.server-container
should always talk to the database on port 3306 as that connection is between containers on the docker network. Those use the container ports.That means that
should be
Change mysql-container internal port to 3306.