搜索

首页  >  问答  >  正文

无法在 docker 容器上启动 mysql 服务器

我有一个 NestJS 应用程序,我想要对其进行 dockerize。我是docker的新手,我必须这样做。在我的 NestJS 应用程序中,我使用带有 Typeorm 的 mysql 数据库。这个 mysql 本身来自 docker 镜像,即我的计算机上没有安装 mysql。

我有这个 docker-compose.yml 文件:

# docker-compose.yml

version: '3.3'
services:
  mysql:
    image: mysql:latest
    restart: always
    environment:
      - MYSQL_USER=myuser
      - MYSQL_PASSWORD=mypassword
      - MYSQL_DATABASE=db
      - MYSQL_ROOT_USER=myuser
      - MYSQL_ROOT_PASSWORD=root

    volumes:
      - mysql:/var/lib/mysql
    ports:
      - '3306:3306'

volumes:
  mysql:

我可以运行 docker-compose up -d 并使用我计算机上的数据库。现在,我还有一个正在尝试编写的 Dockerfle。

在过去 5 个小时左右的时间里我改变了很多事情,但总的来说,它是这样的:

FROM ubuntu:18.04

# Set the working directory to  /usr/src/app
WORKDIR  /usr/src/app

# Copy the package.json and package-lock.json files into the container
COPY package*.json ./

RUN apt-get update
RUN apt-get -y install curl
RUN curl -sL https://deb.nodesource.com/setup_16.x | bash
RUN apt install -y nodejs

# Install the app dependencies
RUN npm install

# Copy the rest of the app files into the container
COPY . .

# Set environment variables for the MySQL connection
ENV MYSQL_HOST=mysql
ENV MYSQL_USER=myuser
ENV MYSQL_PASSWORD=mypassword
ENV MYSQL_DATABASE=db

# Install the MySQL server
RUN apt-get update -qq && apt-get install -y mysql-server

# Expose the app's port
EXPOSE 3000

# Start the app in development mode
CMD ["npm", "run", "start:dev"]

我在上面的Docekerfile中尝试做的是在主机上创建一个ubuntu映像并分别安装curl、node和mysql,以便该应用程序可以与mysql一起使用。 我可以通过 docker build -t my-app 构建此映像。

然后,我运行 image docker run my-app 并收到以下错误:

[Nest] 29  - 04/02/2023, 7:37:26 PM   ERROR [TypeOrmModule] Unable to connect to the database. Retrying (1)...
Error: connect ECONNREFUSED 127.0.0.1:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16)

我不知道如何处理这个问题。

作为参考,这是我的 app.module.ts 文件

import { Module, OnModuleInit } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UsersModule } from './users/users.module';
import { ArtworksModule } from './artworks/artworks.module';
import { User } from './users/entities/user.entity';
import { Artwork } from './artworks/entities/artwork.entity';
import { AuthModule } from './auth/auth.module';
import { ConfigModule } from '@nestjs/config';
import { SeedService } from './users/seedService/seedService';

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'mysql',
      host: 'localhost',
      port: 3306,
      username: 'myuser',
      password: 'mypassword',
      database: 'db',
      entities: [User, Artwork],
      synchronize: false,
      autoLoadEntities: true,
      logging: true,
    }),
    ConfigModule.forRoot({
      isGlobal: true,
    }),
    UsersModule,
    ArtworksModule,
    AuthModule,
  ],
  controllers: [AppController],
  providers: [AppService, SeedService],
})
export class AppModule implements OnModuleInit {
  constructor(private seedService: SeedService) {}

  async onModuleInit() {
    await this.seedService.seedUsers();
  }
}

这个错误是怎么回事?我怎么解决这个问题?感谢所有帮助。

干杯

编辑:现在我想我已经对这个问题有了更多的了解,所以我正在尝试编写一个 docker-compose-yml 文件,该文件将同时启动我的 NestJS 应用程序和 mysql 映像。首先,我将 dockerfile 更改为 =>

# Use the official Node.js 14 image as the base image
FROM node:14

# Create a working directory for the app
WORKDIR /app

# Copy the package.json and package-lock.json files into the container
COPY package*.json ./

# Install app dependencies
RUN npm install

# Copy the rest of the app code into the container
COPY . .

# Expose port 3000 (the port that the app listens on)
EXPOSE 3000

# Specify the command to run when the container starts
CMD [ "npm", "run", "start:prod" ]

然后写了这个docker-compose.yml文件=>

services:
  app:
    build: .
    command: npm run start:prod
    restart: always
    ports:
      - '3000:3000'
    environment:
      - DB_HOST=mysql
      - DB_PORT=3306
      - DB_USER=myuser
      - DB_PASSWORD=mypassword
      - DB_NAME=db
    depends_on:
      - mysql

  mysql:
    image: mysql:latest
    restart: always
    environment:
      - MYSQL_USER=myuser
      - MYSQL_PASSWORD=mypassword
      - MYSQL_DATABASE=db
      - MYSQL_ROOT_USER=myuser
      - MYSQL_ROOT_PASSWORD=root
    volumes:
      - mysql:/var/lib/mysql

volumes:
  mysql:

networks:
  default:
    driver: bridge

现在,当我在 bash 上运行 sudo docker-compose up 时(如果没有 sudo,它会给出权限被拒绝的错误),我收到以下错误

my-app-1    | [Nest] 19  - 04/03/2023, 8:28:03 AM   ERROR [TypeOrmModule] Unable to connect to the database. Retrying (1)...
my-app-1    | Error: connect ECONNREFUSED 127.0.0.1:3306
my-app-1    |     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1159:16)
my-app-1    | [Nest] 19  - 04/03/2023, 8:28:06 AM   ERROR [TypeOrmModule] Unable to connect to the database. Retrying (2)...
my-app-1    | Error: connect ECONNREFUSED 127.0.0.1:3306
my-app-1    |     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1159:16)

据我所知,它无法连接到数据库。我究竟做错了什么?我该如何解决这个问题?

编辑2:根据评论,我做了更多修改。这些是我的发现:

DB_HOST 值似乎并不重要。

# docker-compose.yml

version: '3.3'
services:
  mysql:
    image: mysql:latest
    restart: always
    environment:
      - DB_HOST=localhost
      - MYSQL_ROOT_USER=myuser
      - MYSQL_ROOT_PASSWORD=root

    volumes:
      - mysql:/var/lib/mysql
    ports:
      - '3306:3306'

volumes:
  mysql:

我可以将此值更改为我想要的任何值,并且该脚本将在我的本地计算机上运行。

这又是我的 app.module.ts 文件 =>

import { Module, OnModuleInit } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UsersModule } from './users/users.module';
import { ArtworksModule } from './artworks/artworks.module';
import { User } from './users/entities/user.entity';
import { Artwork } from './artworks/entities/artwork.entity';
import { AuthModule } from './auth/auth.module';
import { ConfigModule } from '@nestjs/config';
import { SeedService } from './users/seedService/seedService';

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'mysql',
      host: process.env.DB_HOST,
      port: 3306,
      username: 'myuser',
      password: 'mypassword',
      database: 'db',
      entities: [User, Artwork],
      synchronize: false,
      autoLoadEntities: true,
      logging: true,
    }),
    ConfigModule.forRoot({
      isGlobal: true,
    }),
    UsersModule,
    ArtworksModule,
    AuthModule,
  ],
  controllers: [AppController],
  providers: [AppService, SeedService],
})
export class AppModule implements OnModuleInit {
  constructor(private seedService: SeedService) {}

  async onModuleInit() {
    await this.seedService.seedUsers();
  }
}

当我尝试对这个应用程序进行 dockerize 以便它可以在仅安装了 Docker 的计算机上运行时,我尝试更改 docker-compose.yml 文件,例如 =>

version: '3.8'

services:
  app:
    build: .
    command: npm run start:prod
    restart: always
    ports:
      - '3000:3000'
    environment:
      - DB_HOST=localhost
      - DB_PORT=3306

    depends_on:
      - mysql

  mysql:
    image: mysql:latest
    restart: always
    environment:
      - MYSQL_ROOT_USER=myuser
      - MYSQL_ROOT_PASSWORD=root

    volumes:
      - mysql:/var/lib/mysql
    ports:
      - '3306:3306'

volumes:
  mysql:

networks:
  default:
    driver: bridge

这会导致两种不同的行为:我无法再运行 docker-compose up,它给出以下错误 =>

=> [internal] load build definition from Dockerfile                                                           0.0s
 => => transferring dockerfile: 32B                                                                            0.0s
 => [internal] load .dockerignore                                                                              0.0s
 => => transferring context: 34B                                                                               0.0s
 => [internal] load metadata for docker.io/library/node:14                                                     1.4s
 => [1/5] FROM docker.io/library/node:14@sha256:a97048059988c65f974b37dfe25a44327069a0f4f81133624871de0063b98  0.0s
 => ERROR [internal] load build context                                                                        0.1s
 => => transferring context: 402.84kB                                                                          0.0s
------
 > [internal] load build context:
------
failed to solve: error from sender: open /home/sirius/coding/my-app/mysql/#innodb_redo: permission denied

我使用的是 Fish 终端,因此出于某种原因,我需要更改为 bash 来运行 sudo docker-compose up,这又会给出以下错误,表明连接到数据库时出现问题=>

e dependencies initialized +0ms
budapest-icf-recruitment-backend-app-1    | [Nest] 19  - 04/03/2023, 9:26:34 AM   ERROR [TypeOrmModule] Unable to connect to the database. Retrying (1)...
budapest-icf-recruitment-backend-app-1    | Error: connect ECONNREFUSED 127.0.0.1:3306
budapest-icf-recruitment-backend-app-1    |     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1159:16)
budapest-icf-recruitment-backend-app-1    | [Nest] 19  - 04/03/2023, 9:26:37 AM   ERROR [TypeOrmModule] Unable to connect to the database. Retrying (2)...
budapest-icf-recruitment-backend-app-1    | Error: connect ECONN

我真的被困住了。

P粉021854777P粉021854777232 天前2596

全部回复(1)我来回复

  • P粉541796322

    P粉5417963222024-04-01 12:22:14

    通过更改 docker-compose.yml 文件解决了所有问题,如下所示 =>

    version: '3.8'
    
    services:
      app:
        image: my-app
        build: .
        volumes:
          - .:/app
        command: npm run start:dev
        restart: always
        ports:
          - '3000:3000'
        environment:
          - DB_HOST=mysql
          - DB_PORT=3306
    
        depends_on:
          - mysql
    
      mysql:
        image: mysql:latest
        restart: always
        environment:
          - MYSQL_ROOT_USER=myuser
          - MYSQL_ROOT_PASSWORD=root
    
        volumes:
          - mysql:/var/lib/mysql
        ports:
          - '3306:3306'
    
    volumes:
      mysql:
    
    networks:
      default:
        driver: bridge

    这是我的 Dockerfile =>

    # Use the official Node.js 14 image as the base image
    FROM node:14
    
    # Create a working directory for the app
    WORKDIR /app
    
    # Copy the package.json and package-lock.json files into the container
    COPY package*.json ./
    
    # Install app dependencies
    RUN npm install
    
    # Copy the rest of the app code into the container
    COPY . .
    
    # Expose port 3000 (the port that the app listens on)
    EXPOSE 3000
    
    # Specify the command to run when the container starts
    CMD [ "npm", "run", "start:dev" ]

    回复
    0
  • 取消回复