Search code examples
postgresqldockerknex.js

Docker 1 exited with code 0 when using knex migrate


I'm getting this error

app_1
Using environment: development app_1 Ran 1 seed

files express_app_1 exited with code 0

when executing this command on docker-compose.yml.

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

What would be an alternative way to execute this command so i can avoid the error.

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" ]

Solution

  • What you get is not really an error. Your container exists normally (code 0) because there is no running process anymore.

    In your Dockerfile you correctly define a running process that would keep the container alive (npm start). But then you overwrite that in your docker-compose.yml. You should correct your command line in docker-compose.yml:

    OLD

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

    NEW

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

    A side note for the "docker exec" command that you mention in your yml file. You can also run "docker-compose exec database psql -U ....". This way you use the service name directly instead of the container name.