将完成的项目迁移到 Docker

Migrate a finished project to Docker

提问人:AVT 提问时间:11/9/2023 最后编辑:AVT 更新时间:11/10/2023 访问量:49

问:

我创建了一个应用程序,一个Node.js服务器和一个MySQL数据库。

最初,对于此应用程序,通过端口 3306 安装与数据库的连接,服务器从 3000 启动。

我为 Docker 创建了 2 个容器,一个用于数据库,另一个用于服务器。

我分配了端口 3307 和 3001 来侦听两者。但是我在连接到端口 3307 时出错。对于实验,我将其设置为 3306,但很明显这里存在错误,因为端口已被初始连接占用。

我在 Connection 中查看了停止端口 3306 的不同答案,这会影响应用程序的运行,Docker 容器将如何替换它?我应该怎么做?

我通过MySQL Workbench使用数据库

Docker文件:

ARG NODE_VERSION=18.14.0
ARG DB_HOST=mysql-container
ARG DB_PORT=3306
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:3306"

错误

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

.env

DB_HOST=localhost
DB_USER=root
DB_PASSWORD=...
DB_NAME=...

DBConnection.js

const mysql = require("mysql2");

const db = mysql.createConnection({
  host: process.env.DB_HOST || "localhost",
  user: process.env.DB_USER || "root",
  password: process.env.DB_PASSWORD || pass, //same value like in .env
  database: process.env.DB_NAME || name, //same value like in .env
});

module.exports = db;

关于 3307 的任何提及 enter image description here

mysql 节点 .js docker

评论


答:

0赞 Hans Kilian 11/9/2023 #1

MySQL 侦听端口 3306,因此无论您将其映射到哪个主机端口,映射的容器端都应始终为 3306。

- "3307:3307"应该是- "3307:3306"

另请注意,如果需要从 docker 网络外部访问数据库,则只需将数据库容器上的端口映射到主机端口。如果唯一需要与数据库通信的是,则无需映射端口。server-container

server-container应始终在端口 3306 上与数据库通信,因为该连接位于 Docker 网络上的容器之间。这些使用容器端口。

这意味着

ARG DB_PORT=3307

应该是

ARG DB_PORT=3306

评论

0赞 AVT 11/9/2023
我也用 3307:3306 尝试过,但没有用
0赞 Hans Kilian 11/10/2023
你改变了声明吗?ARG DB_PORT
0赞 AVT 11/10/2023
改变,但它仍然是相同的错误。添加发布错误消息ARG DB_PORT=3306
0赞 Hans Kilian 11/10/2023
错误消息说它正在尝试连接到,这表明它既没有使用也没有。127.0.0.1:3307DB_HOSTDB_PORT
0赞 AVT 11/10/2023
好的,你能告诉我该怎么做才能让它工作吗?((
0赞 Jeong Yo Han 11/10/2023 #2

将 mysql-container 内部端口更改为 3306。

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:3306"