问题描述
这是我的Dockerfile:
This is my Dockerfile:
FROM node:7
RUN apt-get update && apt-get install -y --no-install-recommends \
rubygems build-essential ruby-dev \
&& rm -rf /var/lib/apt/lists/*
RUN npm install -gq gulp bower
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . /usr/src/app
RUN npm install
CMD ["gulp", "start:dev"]
构建映像时,npm install命令执行时输出很少,而且速度非常快。我实际上是通过docker-compose构建的,它确实安装了卷-我看不到在软管上创建的node_modules文件夹。当我在该映像上启动容器时,可以看到没有node_modules文件夹。然后我执行npm install,一切开始工作-安装所有软件包需要2-3分钟,并且确实创建了node_modules文件夹。
When I build the image, the npm install command executes with little output and really quickly. I actually build it through docker-compose which does have a volume mounted - and I cannot see the node_modules folder being created on my hose. When I launch a container on this image, I can see there is no node_modules folder. I then execute npm install and things start working - it takes 2-3 minutes to install all the packages and the node_modules folder is indeed created.
这是怎么回事?我究竟做错了什么?为什么npm install在构建时不起作用,但随后在运行时起作用?
What is happening here? What am I doing wrong? Why doesn't npm install work at build time, but then it works at run time?
推荐答案
npm install
应该已经基于您的 Dockerfile
工作了。如果您在未安装卷的情况下运行映像,则可以看到创建的文件( DIRNAME
:其中的 docker-compose.yml
位于):
The npm install
should have worked based on your Dockerfile
. You can see the created files if you run the image without a mounted volume (DIRNAME
: where your docker-compose.yml
is located):
docker run --rm -it DIRNAME_node ls -ahl / usr / src / app
使用 docker build
,所有数据都存储在映像中。因此,这是希望您不会在主机上看到任何文件。
With docker build
, all data is stored in the image. So, it's intended that you don't see any files created on your host.
如果您装载卷(通常在Linux中,也在Docker容器中), ,它更易于部署。
I suggest you do your tests based on the Docker image itself and don't mount the volume. Then you have an immutable Docker image which is better for deployment.
这篇关于npm install在Docker中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!