如何在Docker容器中运行npm命令? [英] How can I run an npm command in a docker container?
问题描述
我正在尝试在docker容器中以开发模式运行一个有角度的应用程序,但是当我使用docker-compose build运行它时,它可以正常工作,但是当我尝试放置该容器时,出现以下错误:
I am trying to run an angular application in development mode inside a docker container, but when i run it with docker-compose build it works correctly but when i try to put up the container i obtain the below error:
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服务,但是为什么?
The real problem is that it doesn't recognize the command npm serve, but why??
设置如下:
Docker容器(Nginx反向代理-> Angular在端口4000中运行)
Docker container (Nginx Reverse proxy -> Angular running in port 4000)
我知道有更好的部署方法,但是由于某些个人原因,目前我需要此设置
I know that there are better ways of deploying this but at this moment I need this setup for some personals reasons
Dockerfile:
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
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文件(我遇到问题的重要部分)
Docker Compose file(the important part where I have the problem)
sypgod: # The name of the service
container_name: sypgod # Container name
build:
context: ../angular
dockerfile: Dockerfile # Location of our Dockerfile
推荐答案
最终运行的图像是这样的:
The image that's finally getting run is this:
FROM nginx:alpine
COPY toborFront.conf /etc/nginx/conf.d/
EXPOSE 8080
CMD ["npm", "serve", "--port 4000"]
第一阶段没有任何效果(您可以 COPY --from = ...
个文件退出),并且如果有多个 CMD
,只有最后一个才有效果.由于您是在普通的 nginx
图片中运行此命令,因此没有 npm
命令,从而导致您看到错误.
The first stage doesn't have any effect (you could COPY --from=...
files out of it), and if there are multiple CMD
s, only the last one has an effect. Since you're running this in a plain nginx
image, there's no npm
command, leading to the error you see.
我建议在主机上使用Node进行实时开发环境.在构建并测试了应用程序并打算对其进行部署后,请在适当的情况下使用Docker.在您的Dockerfile中,在第一阶段运行 ng build
将应用程序编译为静态文件,在第二阶段添加 COPY --from = ...
以获取将应用程序内置到Nginx图像中,并删除所有 CMD
行( nginx
具有适当的默认 CMD
).@VikramJakhar的 answer 有一个更完整的Dockerfile显示了这一点.
I'd recommend using Node on the host for a live development environment. When you've built and tested your application and are looking to deploy it, then use Docker if that's appropriate. In your Dockerfile, run ng build
in the first stage to compile the application to static files, add a COPY --from=...
in the second stage to get the built application into the Nginx image, and delete all the CMD
lines (nginx
has an appropriate default CMD
). @VikramJakhar's answer has a more complete Dockerfile showing this.
看起来您可能正在尝试在Docker中同时运行Nginx和Angular开发服务器.如果这是您的目标,则需要在两个单独的容器中运行它们.为此:
It looks like you might be trying to run both Nginx and the Angular development server in Docker. If that's your goal, you need to run these in two separate containers. To do this:
- 将此Dockerfile拆分为两个.将
CMD ["npm","serve"]
行放在第一个(仅角度)Dockerfile的末尾. - 在
docker-compose.yml
文件中添加第二个块以运行第二个容器.后端npm serve
容器不需要发布ports:
. - 将Nginx配置中的后端服务器的主机名从
localhost
更改为另一个容器的Docker Compose名称.
- Split this Dockerfile into two. Put the
CMD ["npm", "serve"]
line at the end of the first (Angular-only) Dockerfile. - Add a second block in the
docker-compose.yml
file to run the second container. The backendnpm serve
container doesn't need to publishports:
. - Change the host name of the backend server in the Nginx config from
localhost
to the Docker Compose name of the other container.
这篇关于如何在Docker容器中运行npm命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!