简体   繁体   English

如何在 docker CMD 命令中运行多个 npm 脚本

[英]how to run multiple npm scripts in docker CMD command

I created a Dockerfile for a nodejs project.我为 nodejs 项目创建了一个 Dockerfile。 It contains a package.json with many scripts.它包含一个带有许多脚本的 package.json。

"scripts": {
        "start": "npm run",
        "server": "cd server && npm start",
        "generator": "cd generator && npm start",
...
    },

I need to run server and genberator in my docker image.我需要在我的genberator映像中运行server和发电机。 How to achieve this?如何做到这一点?

I tried:我试过了:

CMD ls;npm run server; npm run generator CMD ls;npm run server; npm run generator this won't find the package json because shellform seems to run within /bin/sh -c . CMD ls;npm run server; npm run generator这不会找到 package json 因为 shellform 似乎在/bin/sh -c内运行。

CMD ["npm","run","server"] is also not working and is missing the 2nd command CMD ["npm","run","server"]也无法正常工作,并且缺少第二条命令

The ls in first try showed me that all files are in place (incl. the package.json).第一次尝试的 ls 向我展示了所有文件都已就位(包括 package.json)。

For sake of completeness the project in question is https://github.com/seekwhencer/node-bilder-brause (not mine).为了完整起见,有问题的项目是https://github.com/seekwhencer/node-bilder-brause (不是我的)。 the current Dockerfile:当前 Dockerfile:

FROM node:14

# Create app directory
WORKDIR /usr/src/app

# Bundle app source
COPY ./* ./

RUN npm install

EXPOSE 3050
EXPOSE 3055
CMD ls ; npm run server ; npm run generator

The typical way to run multiple commands in a CMD or ENTRYPOINT is to write all of the commands to a file and then run that file.CMDENTRYPOINT中运行多个命令的典型方法是将所有命令写入文件,然后运行该文件。 This is demonstrated in the Dockerfile below.这在下面的 Dockerfile 中得到了证明。

This Dockerfile also install imagemagick, which is a dependency of the package OP is trying to use.这个 Dockerfile 还安装了 imagemagick,这是 package OP 正在尝试使用的依赖项。 I also changed the base image to node:14-alpine because it is much smaller than node:14 but works just as well for this purpose.我还将基础映像更改为node:14-alpine ,因为它比node:14小得多,但同样适用于此目的。

FROM node:14-alpine

# Install system dependencies for this package.
RUN apk add --no-cache imagemagick

# Create app directory
WORKDIR /usr/src/app

# Bundle app source
COPY . .

RUN npm install \
    # Install server and generator.
    && npm run postinstall \
    # Write entrypoint.
    && printf "ls\nnpm run server\nnpm run generator\n" > entrypoint.sh

EXPOSE 3050
EXPOSE 3055

CMD ["/bin/sh", "entrypoint.sh"]
docker build --tag nodebilderbrause .
docker run --rm -it nodebilderbrause

The contents of entrypoint.sh are written in the Dockerfile. entrypoint.sh的内容写在Dockerfile中。 Here is what the file would look like.这是文件的样子。

ls
npm run server
npm run generator

I found another way, adding for sake of completeness and reference for me: https://www.npmjs.com/package/concurrently我找到了另一种方法,为了完整起见并为我添加参考: https://www.npmjs.com/package/concurrently

adding添加

RUN npm install -g concurrently

enables:启用:

CMD ["concurrently","npm:server", "npm:generator"]

If you need to run two separate processes, the typical approach is to run two separate containers.如果需要运行两个独立的进程,典型的做法是运行两个独立的容器。 You can run both containers off the same image;你可以在同一个镜像上运行两个容器; it's very straightforward to override the command part of a container when you start it.启动容器时覆盖容器的命令部分非常简单。

You need to pick something to be the default CMD .您需要选择一些作为默认的CMD Given the package.json you show, for example, you can specify例如,鉴于您显示的package.json ,您可以指定

CMD npm start

When you actually go to run the container, you can specify an alternate command: anything after the image name is taken as the command.当您实际 go 运行容器时,您可以指定备用命令:将图像名称后的任何内容作为命令。 (If you're using Docker Compose, specify command: .) A typical docker run setup might look like: (如果您使用 Docker Compose,请指定command: .)典型的docker run设置可能如下所示:

docker build -t bilder-brause .

docker network create photos

docker run \
  --name server \
  --net photos \
  -d \
  -p 3050:3050 \
  bilder-brause \
  npm run server

docker run \
  --name generator \
  --net photos \
  -d \
  -p 3055:3055 \
  bilder-brause \
  npm run generator

You could build separate images for the different components with separate EXPOSE and CMD directives if you really wanted如果您真的想要,您可以使用单独的EXPOSECMD指令为不同的组件构建单独的图像

FROM bilder-brause
EXPOSE 3050
CMD npm run server

Building these is a minor hassle;构建这些是一个小麻烦; there is no way to specify in Compose that one local image is built FROM another so the ordering might not turn out correctly, for example.例如,无法在 Compose 中指定一个本地图像是FROM另一个图像构建的,因此排序可能不会正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM