使用knex迁移时,Docker 1以代码0退出

时间:2019-05-27 04:55:10

标签: postgresql docker knex.js

我遇到此错误

  

app_1
  使用环境:开发app_1运行1个种子

     

express_app_1文件退出,代码为0

在docker-compose.yml上执行此命令时。

command: bash -c "npm run migrate && npm run seed"

执行此命令的另一种方法是什么,这样我就可以避免该错误。

docker-compose.yml

# docker-compose.yml
version: "3"
services:
  app:
    build: .
    depends_on:
      - database
    ports:
      - 3000:3000
    environment:
      - HOST=database
      # name of the container for ex .*****
      # docker exec -it ***** psql -U postgres -c "create database es6knex"
      # ^ this creates the es6knex database
      - DBNAME=es6knex
    env_file:
      - .env
    command: bash -c "npm run migrate && npm run seed"

  database:
    image: postgres:9.6.8-alpine
    volumes:
      - db-data:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: ${DBPASS:-password}
      POSTGRES_USER: ${DBUSER:-knexuser}
      POSTGRES_DB: ${DBNAME:-es6knex}
    ports:
      - 8002:5432
    env_file:
      - .env
volumes:
  db-data:

package.json

{
  "name": "elies6express",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "./node_modules/.bin/mocha --watch --require @babel/register",
    "start": "nodemon --exec babel-node main.js",
    "migrate": "babel-node node_modules/.bin/knex migrate:latest",
    "seed": "babel-node node_modules/.bin/knex seed:run"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "bookshelf": "^0.14.2",
    "chai-http": "^4.3.0",
    "cookie-parser": "^1.4.4",
    "cors": "^2.8.5",
    "dotenv": "^8.0.0",
    "express": "^4.17.0",
    "knex": "^0.16.5",
    "morgan": "^1.9.1",
    "path": "^0.12.7",
    "pg": "^7.11.0"
  },
  "devDependencies": {
    "@babel/cli": "^7.4.4",
    "@babel/core": "^7.4.5",
    "@babel/node": "^7.4.5",
    "@babel/preset-env": "^7.4.5",
    "@babel/register": "^7.4.4",
    "chai": "^4.2.0",
    "mocha": "^6.1.4",
    "nodemon": "^1.19.0",
    "reify": "^0.19.1",
    "request": "^2.88.0"
  }
}

Dockerfile

FROM node:8

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

EXPOSE 3000
CMD [ "npm", "start" ]

1 个答案:

答案 0 :(得分:1)

您得到的并不是一个错误。您的容器正常存在(代码0),因为不再有正在运行的进程。

在Dockerfile中,您正确定义了一个正在运行的进程,该进程将使容器保持活动状态(npm start)。但是随后您将其覆盖在docker-compose.yml中。您应该在docker-compose.yml中更正命令行:

OLD

command: bash -c "npm run migrate && npm run seed"

command: bash -c "npm run migrate && npm run seed && npm start"

您在yml文件中提到的“ docker exec”命令的旁注。您还可以运行“ docker-compose exec数据库psql -U ....”。这样,您可以直接使用服务名称而不是容器名称。