无法在docker容器上启动mysql server

m1m5dgzv  于 2023-04-04  发布在  Mysql
关注(0)|答案(1)|浏览(97)

我有一个nestJS应用程序,我想dockerize。我是一个新手在docker,我必须这样做。在我的nestJS应用程序,我使用mysql数据库与Typeorm。这个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构建这个镜像。
然后,我运行镜像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镜像。

# 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时(如果没有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了,它给出了以下错误=〉

=> [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

我真的被困住了。

yrefmtwq

yrefmtwq1#

通过像so =〉这样更改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" ]

相关问题