![](/img/trans.png)
[英]Can run npm / node in docker container but not in dockerfile
[英]How can I run an npm command in a docker container?
我正在尝试在 docker 容器内以开发模式运行 angular 应用程序,但是当我使用 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"]
Nginx服务器站点
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 撰写文件(我遇到问题的重要部分)
sypgod: # The name of the service
container_name: sypgod # Container name
build:
context: ../angular
dockerfile: Dockerfile # Location of our Dockerfile
最终运行的图像是这样的:
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 镜像中,并删除所有CMD
行( nginx
有一个合适的默认CMD
)。 @VikramJakhar 的回答有一个更完整的 Dockerfile 显示了这一点。
看起来您可能正在尝试在 Docker 中同时运行 Nginx 和 Angular 开发服务器。 如果这是您的目标,则需要在两个单独的容器中运行它们。 去做这个:
CMD ["npm", "serve"]
行放在第一个(仅限 Angular)Dockerfile 的末尾。docker-compose.yml
文件中添加第二个块以运行第二个容器。 后端npm serve
容器不需要发布ports:
.localhost
更改为另一个容器的 Docker Compose 名称。您可以执行以下操作
### 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;"]
似乎无法从容器访问 npm。 尝试定义它尝试从哪里执行它:
docker run -v "$PWD":/usr/src/app -w /usr/src/app node:10.9 npm serve --port 4000
来源: https : //gist.github.com/ArtemGordinsky/b79ea473e8bc6f67943b
还要确保在运行 docker 容器的计算机上安装了 npm。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.