![](/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.