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

Docker 1 exited with code 0 when using knex migrate

我遇到了这个错误

app_1
Using environment: development app_1 Ran 1 seed

files express_app_1 exited with code 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" ]

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

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

中更正命令行

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 database psql -U ...."。这样就可以直接使用服务名称而不是容器名称了。