当两个镜像都在docker-compose上运行时,将Node服务器连接到MySQL的问题

2w3rbyxf  于 2023-03-28  发布在  Mysql
关注(0)|答案(2)|浏览(129)

我有一个docker-compose.yml文件:
版本:“3”

services:
  db:
    image: mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_ROOT_USER: root
      MYSQL_DATABASE: bl
    ports:
      - "3306:3306"
    volumes:
      - ./data:/var/lib/mysql

  server:
    env_file: "./server/.env"
    build:
      context: ./server
      dockerfile: ./Dockerfile
    image: "server"
    ports:
      - "5000:5000"
    depends_on:
      - db

Dockerfile:

FROM node:lts-alpine

WORKDIR /server

COPY . .
COPY package*.json ./
COPY .env ./ 

RUN yarn install

EXPOSE 5000
EXPOSE 3306

ENV NODE_ENV=docker

RUN yarn typeorm:migrate
RUN yarn typeorm:run-migrations

CMD ["yarn", "start"]

当尝试构建这两个时,我在这一行得到一个错误:RUN yarn typeorm:migrate .
这是我得到的错误:

> [7/8] RUN yarn typeorm:migrate:                                                                                             
#10 0.857 yarn run v1.22.19                                                                                                    
#10 0.943 $ yarn create:migrate-repo && yarn run typeorm -- -d ./src/systems/typeOrm.config.ts migration:generate ./src/migrations/migration
#10 1.344 $ mkdir -p ./src/migrations
#10 1.757 warning From Yarn 1.0 onwards, scripts don't require "--" for options to be forwarded. In a future version, any explicit "--" will be forwarded as-is to the scripts.
#10 1.769 $ ts-node ./node_modules/typeorm/cli -d ./src/systems/typeOrm.config.ts migration:generate ./src/migrations/migration
#10 7.524 Error during migration generation:
#10 7.527 Error: connect ECONNREFUSED 127.0.0.1:3306
#10 7.527     at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16) {
#10 7.527   errno: -111,
#10 7.527   code: 'ECONNREFUSED',
#10 7.527   syscall: 'connect',
#10 7.527   address: '127.0.0.1',
#10 7.527   port: 3306,
#10 7.527   fatal: true
#10 7.527 }
#10 7.565 error Command failed with exit code 1.
#10 7.565 info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
#10 7.594 error Command failed with exit code 1.
#10 7.594 info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
------

发生错误的原因是服务器无法与db映像建立连接。
这是我的DataSource

export const appDataSource = new DataSource({
  type: "mysql",
  host,
  port,
  username: rootUser,
  password,
  database,
  entities: [Company, Order],
  migrationsTableName: "migrations",
  migrations: [`${__dirname}/../migrations/*.ts`],
});

下面是我特灵做的事情的列表。为了解决这个问题:
1.在两个映像之间创建网桥。它不起作用。

version: "3"

services:
  db:
    image: mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_ROOT_USER: root
      MYSQL_DATABASE: bl
    ports:
      - "3306:3306"
    volumes:
      - ./data:/var/lib/mysql
    networks:
      - my-network

  server:
    env_file: "./server/.env"
    build:
      context: ./server
      dockerfile: ./Dockerfile
    image: "server"
    ports:
      - "5000:5000"
    depends_on:
      - db
    networks:
      - my-network

networks:
  my-network:
    driver: bridge

1.将host的名称更改为我的db映像的名称(有网桥和没有网桥)。
1.我甚至试图获得db图像的直接IP,但没有成功。

docker inspect db | grep IPAddress
            "SecondaryIPAddresses": null,
            "IPAddress": "",
                    "IPAddress": "172.23.0.2",
u5rb5r59

u5rb5r591#

问题是db地址在构建时不可用。

RUN yarn typeorm:migrate 
RUN yarn typeorm:run-migrations

从Dockerfile中删除这些行,并添加到server服务中的命令指令中:

server:
    env_file: "./server/.env"
    build:
      context: ./server
      dockerfile: ./Dockerfile
    image: "server"
    ports:
      - "5000:5000"
    depends_on:
      - db
    networks:
      - my-network
    command:
      - yarn typeorm:migrate && yarn typeorm:run-migrations && yarn start
y1aodyip

y1aodyip2#

好吧,我是这么解决的:
我按照Andromeda的建议在docker-compose.yml文件中添加了一行command
我的docker-compose文件看起来像这样:

version: "3"

services:
  db:
    image: mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_ROOT_USER: root
      MYSQL_DATABASE: bl
    ports:
      - "3306:3306"
    volumes:
      - ./data:/var/lib/mysql

  server:
    env_file: "./server/.env"
    build:
      context: ./server
      dockerfile: ./Dockerfile
    image: "server"
    ports:
      - "5000:5000"
    depends_on:
      - db
    command: sh -c "yarn typeorm:migrate && yarn typeorm:run-migrations && yarn start"

我的Dockerfile看起来像这样:

FROM node:lts-alpine

WORKDIR /server

COPY . .
COPY package*.json ./
COPY .env ./ 

RUN yarn install

EXPOSE 5000
EXPOSE 3306

ENV NODE_ENV=docker

CMD ["yarn", "start"]

我从package.json运行的scripts是:

"typeorm": "ts-node ./node_modules/typeorm/cli",
    "create:migrate-repo": "mkdir -p ./src/migrations",
    "typeorm:migrate": "yarn create:migrate-repo && yarn run typeorm -- -d ./src/systems/typeOrm.config.ts migration:generate ./src/migrations/migration",
    "typeorm:run-migrations": "yarn typeorm migration:run -- -d ./src/systems/typeOrm.config.ts",

为了在我的服务中使用db镜像,我需要使用“db”作为host

export const appDataSource = new DataSource({
  type: "mysql",
  host: "db", // <--- use "db" instead of "localhost".
  port,
  username: rootUser,
  password,
  database,
  entities: [Company, Order],
  migrationsTableName: "migrations",
  migrations: [`${__dirname}/../migrations/*.ts`],
});

相关问题