运行nodemon的Docker容器“已退出代码0”

时间:2019-08-22 03:10:50

标签: docker docker-compose nodemon

我运行docker-compose -f docker-compose-base.yml -f docker-compose-dev.yml up

哪个输出:

...
api    |
api    | > api@0.0.0 start:development /usr/src/api
api    | > npm run start:dev
api    |
api    |
api    | > api@0.0.0 start:dev /usr/src/api
api    | > nodemon -L
api    |
api exited with code 0

当我进入容器并使用docker-compose -f docker-compose-base.yml -f docker-compose-dev.yml run --rm api /bin/bash运行/ bin / bash时,nodemon运行正常:

# npm run start:dev

> api@0.0.0 start:dev /usr/src/api
> nodemon -L

[nodemon] 1.18.10
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: /usr/src/api/src/**/*
[nodemon] starting `ts-node -r tsconfig-paths/register src/main.ts`

Docker撰写文件:

$ cat docker-compose-base.yml docker-compose-dev.yml

version: "3"

services:
  api:
    build: .
    restart: on-failure
    volumes:
      - ./:/usr/src/api
      - /usr/src/api/node_modules
    env_file:
      - .env
    container_name: api
version: "3"

networks:
  # Use lb_lbnet network created by the load balancer repo (lb)
  # We do this because we need the load balance to resolve container names defined here to forward traffic
  default:
    external:
      name: lb_lbnet

services:
  db:
    image: postgres:11
    container_name: db
    restart: always
    env_file:
    - ./db.env # uses POSTGRES_DB and POSTGRES_PASSWORD to create a fresh db with a password when first run
    volumes:
      - ./postgres-data:/var/lib/postgresql/data
      # only used to upload DB dump:
      # - ./backup:/tmp/backup

  api:
    tty: true
    depends_on:
      - db
    ports:
      - 3000:3000

添加tty: true无济于事。

更新:这是api的Dockerfile:

FROM node:11

WORKDIR /usr/src/api

COPY package*.json ./

RUN npm install

COPY . .

# not using an execution list here so we get shell variable substitution
CMD npm run start:$NODE_ENV

1 个答案:

答案 0 :(得分:1)

您的npm可执行文件“ start:dev”正在作为后台任务运行,立即退出,或者先运行,然后完成并退出。

我建议您将dockerfile更改为末尾带有以下内容,而不是当前的CMD行。

COPY entrypoint.sh /entrypoint.sh
ENTRYPOINT /entrypoint.sh "$NODE_ENV"

现在创建一个名为entrypoint.sh的文件,并将其放置在与dockerfile相同的目录中。在您的entrypoint.sh中,包含以下内容。

#!/bin/bash
npm run start:$1 &
bash