gpt4 book ai didi

node.js - Docker撰写-无法连接到mysql-在mysql之前构建应用程序?

转载 作者:行者123 更新时间:2023-12-02 21:00:04 25 4
gpt4 key购买 nike

我有一个docker compose文件,其中包含3个应用程序。 mysql,phpmyadmin和nodejs应用。您将在下面找到撰写文件。

  • nodejs应用程序具有sequilizeJS,它需要在初始化时运行migration&seed命令。
  • 当我运行docker-compose up --build时,构建失败,因为mysql返回错误getaddrinfo ENOTFOUND mysql
  • 我看不出我在组合文件中做错了什么,因为对我来说看起来还可以。
  • phpmyadminauth应用程序都需要 mysql,因此我已将 mysql添加到 depends_on部分。从日志文件看来, Composer 尝试在创建 auth之前先构建 mysql

    日志

    Creating network "updials-auth_default" with the default driver
    Building auth
    Step 1/8 : FROM node:12.14.0
    ---> 6b5991bf650f
    Step 2/8 : WORKDIR /var/www
    ---> Using cache
    ---> 21c89e8b8059
    Step 3/8 : COPY . .
    ---> 73072a4bddb5
    Step 4/8 : COPY package.json /usr/share/app
    ---> 886992b71802
    Step 5/8 : EXPOSE 3001
    ---> Running in cd7c14183427
    Removing intermediate container cd7c14183427
    ---> b93bcdf8c653
    Step 6/8 : RUN npm install
    ---> Running in 4b6d75b77bab
    npm WARN deprecated mkdirp@0.5.1: Legacy versions of mkdirp are no longer supported. Please update to mkdirp 1.x. (Note that the API surface has changed to use Promises in 1.x.)
    npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
    npm WARN rm not removing /var/www/node_modules/.bin/rimraf as it wasn't installed by /var/www/node_modules/rimraf

    > bcrypt@3.0.8 install /var/www/node_modules/bcrypt
    > node-pre-gyp install --fallback-to-build

    node-pre-gyp WARN Using request for node-pre-gyp https download
    [bcrypt] Success: "/var/www/node_modules/bcrypt/lib/binding/bcrypt_lib.node" is installed via remote

    > ejs@2.7.4 postinstall /var/www/node_modules/ejs
    > node ./postinstall.js

    Thank you for installing EJS: built with the Jake JavaScript build tool (https://jakejs.com/)

    npm notice created a lockfile as package-lock.json. You should commit this file.
    added 18 packages from 3 contributors, removed 9 packages, updated 467 packages and audited 1563 packages in 51.173s

    22 packages are looking for funding
    run `npm fund` for details

    found 5 low severity vulnerabilities
    run `npm audit fix` to fix them, or `npm audit` for details
    Removing intermediate container 4b6d75b77bab
    ---> f3c15392ccc2
    Step 7/8 : RUN npm run migrate && npm run seed
    ---> Running in cd58f889c907

    > updials-auth@0.0.2 migrate /var/www
    > npx sequelize-cli db:migrate

    npx: installed 81 in 8.76s

    Sequelize CLI [Node: 12.14.0, CLI: 5.5.1, ORM: 5.21.5]

    Loaded configuration file "config/config.js".
    Using environment "development".

    ERROR: getaddrinfo ENOTFOUND mysql

    npm ERR! code ELIFECYCLE
    npm ERR! errno 1
    npm ERR! updials-auth@0.0.2 migrate: `npx sequelize-cli db:migrate`
    npm ERR! Exit status 1
    npm ERR!
    npm ERR! Failed at the updials-auth@0.0.2 migrate script.
    npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

    npm ERR! A complete log of this run can be found in:
    npm ERR! /root/.npm/_logs/2020-03-29T15_36_20_888Z-debug.log
    ERROR: Service 'auth' failed to build: The command '/bin/sh -c npm run migrate && npm run seed' returned a non-zero code: 1


    Docker文件
    FROM node:12.14.0
    #USER node
    WORKDIR /var/www

    COPY . .
    COPY package.json /usr/share/app
    #COPY package.lock.json /usr/share/app

    EXPOSE 3001
    RUN npm install
    RUN npm run migrate && npm run seed

    CMD ["npm", "start"]


    docker-compose.yml
    version: '3.7'

    services:
    mysql:
    container_name: updials-auth-mysql
    image: mysql:5.7
    environment:
    MYSQL_ROOT_PASSWORD: 'password'
    MYSQL_DATABASE: 'updials'
    MYSQL_USER: 'updials'
    MYSQL_PASSWORD: 'password'
    volumes:
    - database:/var/lib/mysql

    auth:
    container_name: updials-auth
    restart: always
    depends_on:
    - mysql
    build: .
    ports:
    - '3001:5002'
    environment:
    DB_HOST: 'mysql'
    DB_USER: 'updials'
    DB_PASS: 'password'
    DB_NAME: 'updials'
    phpmyadmin:
    container_name: phpmyadmin-updials-auth
    restart: always
    image: phpmyadmin/phpmyadmin:5.0.2
    depends_on:
    - mysql
    environment:
    MYSQL_USER: updials
    MYSQL_PASSWORD: password
    ports:
    - '4000:8080'
    volumes:
    database:
    driver: local
    driver_opts:
    type: 'none'
    o: 'bind'
    device: '/home/sisir/docker-databases/updials-auth'

    最佳答案

    Dockerfile永远无法访问docker-compose.yml中声明的数据库,卷或其他资源(该服务的即时build:块之外)。构建作为一个单独的阶段运行;它没有连接到Compose网络。

    (想象一下,在一个系统上运行docker build; docker push,然后在第二个系统上指定匹配的image:。在这种设置下,构建时系统无法访问运行时数据库,这是您应该牢记的基本模型。更直接地说,可以删除并重新创建mysql容器,而无需重建auth图像。)

    完成这项工作的典型模式是编写一个入口点脚本。这成为您的容器运行的主要命令;它通过Dockerfile CMD(或Docker Compose command:)作为命令行参数传递。由于此操作是在容器启动时运行的,因此它确实有权访问数据库,网络,环境变量等。

    #!/bin/sh
    set -e # Stop on any error
    npm run migrate # Run migrations
    npm run seed # Preload initial data
    exec "$@" # Run the command as the main container process

    在您的 Dockerfile中,将此脚本作为 ENTRYPOINT。您必须在此处使用 ENTRYPOINT的JSON数组形式。

    FROM node:12.14.0
    WORKDIR /var/www

    # Install dependencies first to save time on rebuild
    COPY package.json .
    RUN npm install

    COPY . .
    EXPOSE 3001

    RUN chmod +x entrypoint.sh # if required
    ENTRYPOINT ["./entrypoint.sh"]
    CMD ["npm", "start"]

    关于node.js - Docker撰写-无法连接到mysql-在mysql之前构建应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60916919/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com