node.js - docker-compose npm install && npm start

我想使用 docker-compose 启动 2 个容器。一个用于数据库,一个用于 Node 服务器,使用 DockerHub 上的默认 node 图像。在 Node 容器中,我想挂载一个作为卷进行源代码控制的本地文件夹。我见过许多创建 Dockerfile 的示例,该 Dockerfile 将源文件复制到镜像中,然后在 Dockerfile 中使用 RUN npm install 命令。但这会导致创建一个包含源文件的新图像。就我而言,我不想在 Node 镜像中保存源代码文件。所以我希望我的 docker-compose 文件中的 Node 容器的 entrypoint 运行 npm install 以及 npm start,但是我似乎无法使该组合起作用。这是我的 docker-compose.yml:

version: "3.8"

services:

  server:
    container_name: my_server
    image: node:12.16.1
    ports:
      - "8000:8000"
    volumes:
      - ../my-server-files-source-controlled:/var/www
    working_dir: /var/www
    entrypoint: ["npm", "start"]
    networks:
      - my-network

  db:
    container_name: my_database
    image: postgres
    environment:
      {ommitted}
    ports:
      - "5432:5432"
    networks:
      - my-network

networks:
  my-network:
    driver: bridge

上面的命令工作正常,但是当我更改 entrypoint 以包含 npm install 时,它失败了。我尝试了以下示例以及许多其他示例,但都失败了:

entrypoint: ["npm", "install", "&&", "npm", "start"]
entrypoint: ["npm install", "npm start"]
entrypoint: "npm install && npm start"

似乎我可以在 entrypoint 中安装 npm installnpm start,但不能同时安装。如何让这两个命令在我的 docker-compose 文件中工作?

最佳答案

如果你想使用多个命令,你可以使用bash -c "commands":

services:
    myservice:
        command: bash -c "npm install && npm start"

顺便说一下,在生产部署中,我建议使用 npm ci 而不是 npm install。还可以考虑使用 --only=prod--no-audit 标志(取决于您的设置)。

关于node.js - docker-compose npm install && npm start in 入口点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65149385/

相关文章:

scala - scala中以下两种模式匹配案例有什么区别

asp.net-core - 415 ASP.NET Core Web API 中不支持的媒体类型

wordpress - Disqus iframe 透明度不适用于 Chrome 87

python - For循环遍历Python中的列表

kubernetes - 如何使用 HELM 强制重新部署

r - 使用向量和定义的标量在 R 中生成金字塔

python - 删除txt中的特定字符

c - 打印函数,C语言的hello world

java - Config Client 在 Spring boot 中不工作

flutter - 查看 .pub-cache 文件给出 Request textDocument/