如何在Docker容器中运行npm命令?

时间:2020-02-05 10:52:15

标签: node.js angular docker npm docker-compose

我正在尝试在docker容器中以开发模式运行一个有角度的应用程序,但是当我使用docker-compose build运行它时,它可以正常工作,但是当我尝试放置该容器时,出现以下错误:



ERROR: for sypgod  Cannot start service sypgod: OCI runtime create failed: container_linux.go:346: starting container process caused "exec: \"npm\": executable file not found in $PATH

真正的问题是它无法识别npm服务,但是为什么?

设置如下:

Docker容器(Nginx反向代理-> Angular在端口4000中运行)

我知道有更好的部署方法,但是由于某些个人原因,目前我需要此设置

Dockerfile:


FROM node:10.9 


COPY package.json package-lock.json ./

RUN npm ci && mkdir /angular && mv ./node_modules ./angular

WORKDIR /angular

RUN npm install -g @angular/cli 

COPY . . 


FROM nginx:alpine
COPY toborFront.conf /etc/nginx/conf.d/
EXPOSE 8080
CMD ["nginx", "-g", "daemon off;"]
CMD ["npm", "serve", "--port 4000"]

NginxServerSite

server{
    listen 80;
    server_name sypgod;


    location / {
            proxy_read_timeout 5m;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_pass http://localhost:4000/;


    }

}


Docker Compose文件(我遇到问题的重要部分)

 sypgod: # The name of the service
        container_name: sypgod  # Container name
        build: 
            context: ../angular
            dockerfile: Dockerfile # Location of our Dockerfile


3 个答案:

答案 0 :(得分:1)

您可以执行以下操作

### STAGE 1: Build ###

# We label our stage as ‘builder’
FROM node:alpine as builder

RUN apk --no-cache --virtual build-dependencies add \
    git \
    python \
    make \
    g++

    RUN mkdir -p /ng-app/dist

    WORKDIR /ng-app

    COPY package.json package-lock.json ./

    ## Storing node modules on a separate layer will prevent unnecessary npm installs at each build
    RUN npm install

    COPY . .

    ## Build the angular app in production mode and store the artifacts in dist folder

    RUN npm run ng build -- --prod --output-path=dist


    ### STAGE 2: Setup ###

    FROM nginx:1.14.1-alpine

    ## Copy our default nginx config
    COPY toborFront.conf /etc/nginx/conf.d/

    ## Remove default nginx website
    RUN rm -rf "/usr/share/nginx/html/*"

    ## From ‘builder’ stage copy over the artifacts in dist folder to default nginx public folder
    COPY --from=builder /ng-app/dist /usr/share/nginx/html

    CMD ["nginx", "-g", "daemon off;"]

答案 1 :(得分:1)

最终运行的图像是这样的:

FROM nginx:alpine
COPY toborFront.conf /etc/nginx/conf.d/
EXPOSE 8080
CMD ["npm", "serve", "--port 4000"]

第一阶段没有任何效果(您可以COPY --from=...退出文件),并且如果有多个CMD,则只有最后一个有效果。由于您是在普通的nginx图像中运行此命令,因此没有npm命令,会导致您看到错误。

我建议将主机上的Node用于实时开发环境。在构建并测试了应用程序并打算对其进行部署后,请在适当的情况下使用Docker。在您的Dockerfile中,在第一阶段运行ng build以将应用程序编译为静态文件,在第二阶段添加COPY --from=...以将已构建的应用程序放入Nginx映像,并删除所有{{1 }}行(CMD具有适当的默认值nginx)。 @VikramJakhar的answer有一个更完整的Dockerfile显示了这一点。

看起来您可能正在尝试在Docker中同时运行Nginx和Angular开发服务器。如果这是您的目标,则需要在两个单独的容器中运行它们。为此:

  • 将此Dockerfile拆分为两个。将CMD行放在第一个(仅角度)Dockerfile的末尾。
  • CMD ["npm", "serve"]文件中添加第二个块以运行第二个容器。后端docker-compose.yml容器不需要发布npm serve
  • 将Nginx配置中的后端服务器的主机名从ports:更改为另一个容器的Docker Compose名称。

答案 2 :(得分:0)

似乎无法从容器访问npm。 尝试定义从哪里尝试执行它:

<mapping class="java.sql.Date"/>

来源:https://gist.github.com/ArtemGordinsky/b79ea473e8bc6f67943b

还要确保在运行docker容器的计算机上安装了npm。