我正试图弄清楚如何使用我的端口组合设置运行后缀数据库迁移,到目前为止,两种不同的方法都失败了。在安装cli之后,我第一次尝试在我的Dockerfile中运行npx sequelize db:migrate,但由于数据库容器还没有运行,所以失败了,第二次尝试创建一个依赖于数据库容器的容器,并使用wait-for-it.sh运行命令,但这导致了wait-for-it.sh容器引用的失败。两次尝试都失败了,但我想我理解需要发生的逻辑。
使用Docker文件contents
这听起来正确吗?有没有人知道一个好的方法?
文档:
FROM node:alpine
# Setting working directory. All the paths will be relative to WORKDIR
WORKDIR /usr/src/app
# Add bash to docker
RUN apk update && apk add bash
# Install dependencies
COPY package*.json ./
RUN npm install
# Install sequelize-cli for migrations
RUN npm install sequelize-cli --save
# Copying source files
COPY . .
# Copying shell script to ensure DB starts prior to server
COPY wait-for-it.sh .
# Run sequelize database migrations
# RUN npx sequelize db:migrate # FAILED ATTEMPT == ERROR: connect ECONNREFUSED 127.0.0.1:5432
# Build app
RUN npm run build
# Port
EXPOSE 3000
# Run the app
CMD [ "npm", "start" ]docker-compose.yml:
version: "3.9"
services:
redis:
image: redis:alpine
database:
image: postgres
ports:
- "5432:5432"
environment:
POSTGRES_USER: ${DB_USERNAME}
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: ${DB_DATABASE}
volumes:
- nextjs_auth_template:/var/lib/postgresql/data/ # persist data even if container shuts down
app:
image: nextjs-auth-boilerplate
build: .
depends_on:
- redis
- database
# command: ["./wait-for-it.sh", "database:5432", "--", "node", "server.js"]
ports:
- "3000:3000"
environment:
- REDIS_HOST=redis
- DB_HOSTNAME=database
# migration:
# image: app:latest # FAILED ATTEMPT == pull access denied for app
# command: ["./wait-for-it.sh", "database:5432", "--", "npm", "run", "db:migrate"]
# depends_on:
# - database
volumes:
nextjs_auth_template:.env:
DB_USERNAME=postgres
DB_PASSWORD=''
DB_DATABASE=nextjs_auth_template
DB_HOSTNAME=127.0.0.1
DB_URL=postgresql://postgres@127.0.0.1/nextjs_auth_templatepackage.json (npm运行db:迁移):
"db:migrate": "node_modules/.bin/sequelize db:migrate",发布于 2021-11-09 22:12:07
这是相当古老的,但首先,您必须更改您的续集配置。应该是类似的东西。
development: {
...
host: process.env.MYSQL_HOST || '127.0.0.1',
dialect: 'mysql',
},此外,您还应该从.env文件中更改它。据我所见,您正在使用redis,所以您的主机名应该是redis。除此之外,您还可以像下面这样在docker-compose.yml中运行迁移。
在您的服务中,添加以下内容。
command: npx sequelize db:migrate && npm starthttps://stackoverflow.com/questions/67401442
复制相似问题