作者:mobiledu2502882465 | 来源:互联网 | 2023-01-09 17:10
我正在使用keymetrics官方图像docker hub链接构建一个非常基本的容器
但这不是一个node.js或pm2问题,而是一个非常多的Docker问题,尤其是 docker build
Dockerfile(npm install
)中的步骤将节点程序包下拉到node_modules文件夹中-IT WORKS。创建文件夹并下载文件,并在生成输出中显示(如下)。
问题在于,该RUN命令完成后,中间容器将被删除,而我丢失了node_modules文件夹。
我的dockerfile有一些ls -l
命令来阐明文件的状态。
保留在工作目录(/app
)中的唯一文件是我使用COPY命令从主机复制的文件
这是我的Dockerfile:
FROM keymetrics/pm2:latest
# Bundle APP files
COPY src src/
COPY package.json .
COPY pm2.json .
# Install app dependencies
ENV NPM_CONFIG_LOGLEVEL info
RUN pwd && ls -l /app && npm install && ls -l
# Show current folder structure in logs
RUN ls -l
CMD [ "pm2-docker", "start", "pm2.json" ]
生成命令:
docker build -t localapps/pm2_test_app:0.0.4 .
建立输出
Sending build context to Docker daemon 13.31kB
Step 1/8 : FROM keymetrics/pm2:latest
---> 6aa333f957ec
Step 2/8 : COPY src src/
---> Using cache
---> 6b73b4463af5
Step 3/8 : COPY package.json .
---> Using cache
---> d27a2e75fdde
Step 4/8 : COPY pm2.json .
---> Using cache
---> 9864d9dd73a9
Step 5/8 : ENV NPM_CONFIG_LOGLEVEL info
---> Using cache
---> 9f711fe6bada
Step 6/8 : RUN pwd && ls -l /app && npm install && ls -l
---> Running in 668eb2e2c1e8
/app
total 12
-rw-r--r-- 1 root root 323 Nov 8 17:33 package.json
-rw-r--r-- 1 root root 123 Nov 8 16:59 pm2.json
drwxr-xr-x 2 root root 4096 Nov 8 18:10 src
npm info it worked if it ends with ok
npm info using npm@5.5.1
npm info using node@v9.0.0
... ...
added 8 packages in 2.001s
npm info ok
total 20
drwxr-xr-x 10 root root 4096 Nov 8 19:49 node_modules
-rw-r--r-- 1 root root 1833 Nov 8 19:49 package-lock.json
-rw-r--r-- 1 root root 323 Nov 8 17:33 package.json
-rw-r--r-- 1 root root 123 Nov 8 16:59 pm2.json
drwxr-xr-x 2 root root 4096 Nov 8 18:10 src
---> 0d749171a431
Removing intermediate container 668eb2e2c1e8
Step 7/8 : RUN ls -l
---> Running in fc2132121c96
total 12
-rw-r--r-- 1 root root 323 Nov 8 17:33 package.json
-rw-r--r-- 1 root root 123 Nov 8 16:59 pm2.json
drwxr-xr-x 2 root root 4096 Nov 8 18:10 src
---> c689180648c9
Removing intermediate container fc2132121c96
Step 8/8 : CMD pm2-docker start pm2.json
---> Running in e70b2b89b3c8
---> 299c26f883e8
Removing intermediate container e70b2b89b3c8
Successfully built 299c26f883e8
Successfully tagged localapps/pm2_test_app:0.0.4
^^请注意,该node_modules
目录存在,并且在执行完第6步之后是否可以执行该操作?
从构建输出中可以证明已npm install
运行并将软件包拉到相应的目录...。
但是在步骤7中,它消失了吗?此外,在最终图像上,该图像已消失。应用程序无法正常运行,因为node_modules
最终映像中的依赖项已被破坏。
为什么?没有node_modules目录,应用程序将无法运行,它们是依赖项。
我可以执行此工作的唯一方法是npm_install
在构建外部运行,然后使用来复制文件夹,COPY
这绝对不是有效的解决方案...但这是一种行之有效的解决方法。
我知道它使用了有意义的中间容器,但是如何使在RUN
命令中创建的文件保留到最后呢?
编辑:进一步的调查显示,在此处找到的源映像dockerfile VOLUME
在其dockerfile中声明了一个-这是我的问题所在吗?他们的示例表明,npm install
开箱即用即可完成工作,但并不能消除,因为它会清除安装下载的文件。
1> BMitch..:
keymetrics / pm2映像在其Dockerfile中包含以下行:
VOLUME ["/app"]
这破坏了以后的步骤和修改/app
目录的子映像。从码头工人的文档:
从Dockerfile中更改卷:如果在声明了卷后有任何构建步骤更改了卷中的数据,则这些更改将被丢弃。
您将需要在其他目录中运行构建,制作不包含该行的其他基础映像,或者说服上游作者删除该行。
我以前已经在博客上介绍了Dockerfiles中定义的卷的问题,这是其中的问题之一。在他们的Dockerfile中确实没有任何上行空间。