gpt4 book ai didi

javascript - 在 docker compose 中运行后续迁移

转载 作者:行者123 更新时间:2023-12-02 22:07:47 24 4
gpt4 key购买 nike

我正在尝试使用 Node 和 postgres 启动两个容器(我需要保存数据库的内容)。请按照以下代码操作:

Dockerfile:

FROM node:lts-alpine

RUN mkdir -p /home/node/api/node_modules && chown -R node:node /home

WORKDIR /home/node/api

COPY package.json /

USER node

RUN npm i

COPY --chown=node:node . .

EXPOSE 3000

COPY ./docker-entrypoint.sh /docker-entrypoint.sh

RUN chmod +x /docker-entrypoint.sh

ENTRYPOINT ["/docker-entrypoint.sh"]

docker-entrypoint.sh:

#!/bin/sh

npx sequelize-cli db:migrate

npm run dev

好吧,在这里我不知道如何运行迁移,因为在我的计算机中我只运行“sequelize db:migrate”,我在package.json中也有一个脚本 "migrate:dev": "NODE_ENV=开发 Sequelize db:migrate"

例如。

还有 docker-compose.yml:

version: '3'

services:
app-app:
container_name: app-app
build: '.'
volumes:
- .:/home/node/api
- /home/node/api/node_modules
depends_on:
- postgres-app
networks:
- app-connect
ports:
- '3000:3000'

postgres-app:
container_name: postgres-app
image: postgres:11
restart: unless-stopped
volumes:
- postgres-app-data:/data
environment:
POSTGRES_DB: ${DATABASE}
POSTGRES_USER: ${DB_USER}
POSTGRES_PASSWORD: ${DB_PASS}
networks:
- app-connect

volumes:
postgres-app-data:

networks:
app-connect:
driver: bridge

但是迁移尚未运行。当我尝试运行 docker-entrypoint.sh 时出现错误:

chmod: /docker-entrypoint.sh: Operation not permitted
ERROR: Service 'app-app' failed to build: The command '/bin/sh -c chmod +x /docker-entrypoint.sh' returned a non-zero code: 1

如果我删除所有这些项目并仅直接运行 npm start ,则所有工作都有效,但是当我尝试访问应用程序容器来运行迁移时,会显示错误:

docker exec -it 351 npm run migrate:dev                                                                 

> app@1.0.0 migrate:dev /home/node/api
> NODE_ENV=development sequelize db:migrate


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

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

ERROR: getaddrinfo ENOTFOUND postgres

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! app@1.0.0 migrate:dev: `NODE_ENV=development sequelize db:migrate`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the app@1.0.0 migrate:dev 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! /home/node/.npm/_logs/2020-01-09T12_43_32_377Z-debug.log

我怎样才能做到这一点?谢谢!

最佳答案

因此,根据上面的评论,问题是 DB_HOST 设置了错误的值。

在这种情况下,DB_HOST 应具有服务名称的值:postgres-app。

关于javascript - 在 docker compose 中运行后续迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59664802/

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