Docker Compose for Node Server失败

时间:2019-02-04 22:28:40

标签: node.js docker npm docker-compose

我写了一个通过npm脚本运行的节点服务器(npm run dev)。 我为此创建了以下Dockerfile:

FROM node:10.12.0

ARG COMMIT_REF
ARG BUILD_DATE

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

ENV APP_COMMIT_REF=${COMMIT_REF} \
    APP_BUILD_DATE=${BUILD_DATE}

ENV PORT 8009

EXPOSE 8009

CMD [ "npm", "run", "server" ]

使用以下命令构建和运行服务器:
docker build . -t serverdocker run -it -p 8009:8009 --env-file .env server运作良好,服务器在端口8009上运行并公开。

否,我尝试为其创建docker-compose.yml。这是我写的:

version: '3'

services:
    server:
        image: server
        build: .
        env_file:
            - .env
        volumes:
            - .:/usr/src/app
            - /usr/src/app/node_modules
        working_dir: /.
        ports:
            - 8009:8009

使用docker-compose up运行它时,我得到:

Starting bed_server_1 ... done
Attaching to bed_server_1
server_1  | npm ERR! path /package.json
server_1  | npm ERR! code ENOENT
server_1  | npm ERR! errno -2
server_1  | npm ERR! syscall open
server_1  | npm ERR! enoent ENOENT: no such file or directory, open '/package.json'
server_1  | npm ERR! enoent This is related to npm not being able to find a file.
server_1  | npm ERR! enoent
server_1  |
server_1  | npm ERR! A complete log of this run can be found in:
server_1  | npm ERR!     /root/.npm/_logs/2019-02-04T22_27_50_916Z-debug.log
bed_server_1 exited with code 254

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

映像的CMD在其启动所在的任何目录中运行npm run serverdocker-compose.yml working_dir: /告诉容器在根目录中启动。那不是你想要的。您希望容器在您将其源代码复制到的目录中启动。

如果删除working_dir:行,您可能会解决当前的问题。