fju*_*urr 6 javascript node.js docker docker-compose
yarn sequelize db:migrate我正在使用 docker-compose,当我尝试在控制台中运行时出现以下错误:
“已加载配置文件“src/config/database.js”。错误:getaddrinfo ENOTFOUND db”
一切正常,但我无法运行我的迁移。
Dockerfile
FROM node:alpine
WORKDIR /usr/app
COPY package.json yarn.lock ./
RUN yarn
COPY . .
EXPOSE 3000
CMD ["yarn", "dev"]
Run Code Online (Sandbox Code Playgroud)
Docker-compose.yml
version: '3'
services:
api:
container_name: api
build: .
ports:
- '3000:3000'
command: yarn dev
volumes:
- .:/usr/app
links:
- db
db:
container_name: db
image: mysql:5.7
volumes:
- ./data:/mysql/db
ports:
- '3306:3306'
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: be_db
MYSQL_USER: root
MYSQL_PASSWORD: password
Run Code Online (Sandbox Code Playgroud)
.env
# Database Variables
DB_HOST=db
DB_USER=root
DB_PASS=password
DB_NAME=be_db
DB_PORT:3306
Run Code Online (Sandbox Code Playgroud)
/src/config/database.js
require('dotenv').config();
module.exports = {
dialect: 'mysql',
host: process.env.DB_HOST,
username: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME,
define: {
timestamps: false,
underscored: true,
underscoredAll: true,
},
};
Run Code Online (Sandbox Code Playgroud)
OBS:如果我切换DB_HOST=db到DB_HOST=localhost我可以运行我的迁移,但是我的容器会弹出此错误:
code: 'ECONNREFUSED',
api | syscall: 'connect',
api | address: '127.0.0.1',
api | port: 3306,
api | fatal: true
Run Code Online (Sandbox Code Playgroud)
好的,我只需将端口和主机分成单独的字段:
\n"development": {\n "username": "postgres",\n "password": "postgres",\n "database": "postgres",\n "host": "localhost",\n "port": "5434",\n "dialect": "postgres"\n },\nRun Code Online (Sandbox Code Playgroud)\ndocker-compose我也有同样的问题sequelize-cli
node:\n build:\n context: ./node\n target: dev\n volumes:\n - ./node/src:/app/src\n - ./node/private:/app/private\n ports:\n - 3111:3000\n command: npm run dev\n environment:\n - NODE_ENV=development\n env_file:\n - ./node/.env\n depends_on:\n - db\n\n db:\n image: postgres:12-alpine\n ports:\n - 5434:5432\n environment:\n - POSTGRES_HOST_AUTH_METHOD=trust\n - POSTGRES_USER=postgres\n - POSTGRES_PASSWORD=postgres\n - POSTGRES_DB=postgres\nRun Code Online (Sandbox Code Playgroud)\n "development": {\n "username": "postgres",\n "password": "postgres",\n "database": "postgres",\n "host": "localhost:5434",\n "dialect": "postgres"\n }\nRun Code Online (Sandbox Code Playgroud)\n当我跑步时\xe2\x9d\xaf npx sequelize-cli db:migrate
我收到以下错误\n:
\nSequelize CLI [Node: 15.2.1, CLI: 6.2.0, ORM: 6.3.5]\n\nLoaded configuration file "config/config.json".\nUsing environment "development".\n\nERROR: getaddrinfo ENOTFOUND localhost:5434\nRun Code Online (Sandbox Code Playgroud)\n它适用于以下命令,这更多的是一种解决方法
\n\xe2\x9d\xaf npx sequelize-cli db:migrate --url \'postgres://postgres:postgres@localhost:5434/postgres\'\n\nSequelize CLI [Node: 15.2.1, CLI: 6.2.0, ORM: 6.3.5]\n\nParsed url postgres://postgres:*****@localhost:5434/postgres\nNo migrations were executed, database schema was already up to date.\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
8904 次 |
| 最近记录: |