gpt4 book ai didi

docker - 更有效地调试Docker脚本

转载 作者:行者123 更新时间:2023-12-02 21:27:42 26 4
gpt4 key购买 nike

我正在开始执行第一个docker脚本,并且尝试调试最后一步,调试B / c的步骤非常缓慢,因此需要几分钟才能运行,因此如果我有错字,我必须重新运行整个脚本。

有没有更有效的方法来调试docker脚本?还是我每次都要重建整个东西?

#FROM ubuntu:14.04
FROM node:0.10.40
#FROM mongo:2.6.11

# The port we're running the server on
EXPOSE 10645

# Set this as the working directory
WORKDIR /myproject/hapi

# Move the myproject files to /myproject in the docker container
ADD . /myproject/hapi

# Install the server dependencies
RUN pwd && ls -al && npm install

# Start everything up
CMD npm start

日志输出:
^CR5033505:myproject m089269$ docker build -t myproject-hapi .
Sending build context to Docker daemon 932.2 MB
Step 0 : FROM node:0.10.40
---> a7d8016a6fdb
Step 1 : EXPOSE 10645
---> Running in ebc4f8ebbf7b
---> 701320586e6a
Removing intermediate container ebc4f8ebbf7b
Step 2 : WORKDIR /myproject/hapi
---> Running in 1998f97b252a
---> 1414baf38920
Removing intermediate container 1998f97b252a
Step 3 : ADD . /myproject/hapi
---> c80e665da20b
Removing intermediate container f6904fab79ce
Step 4 : RUN pwd && ls -al && npm install
---> Running in a3ef28ed70ae
/myproject/hapi
total 68
drwxr-xr-x 9 root root 4096 Oct 30 18:35 .
drwxr-xr-x 3 root root 4096 Oct 30 18:35 ..
-rw-r--r-- 1 root root 509 Apr 10 2015 .editorconfig
drwxr-xr-x 8 root root 4096 Oct 30 18:33 .git
-rw-r--r-- 1 root root 491 Oct 20 15:09 .gitignore
drwxr-xr-x 8 root root 4096 Aug 19 14:51 .idea
-rw-r--r-- 1 root root 1781 Apr 10 2015 .jscsrc
-rw-r--r-- 1 root root 6164 Apr 10 2015 .tfignore
-rw-r--r-- 1 root root 430 Oct 30 18:33 Dockerfile
-rw-r--r-- 1 root root 371 Oct 30 18:16 Dockerfile-client
-rwxr-xr-x 1 root root 1374 Oct 30 15:15 README.md
drwxr-xr-x 5 root root 4096 Oct 21 21:18 ab-testing-deploy
drwxr-xr-x 3 root root 4096 Oct 30 15:15 build
drwxr-xr-x 14 root root 4096 Oct 30 15:15 client
drwxr-xr-x 2 root root 4096 Apr 10 2015 githooks
drwxr-xr-x 10 root root 4096 Oct 30 15:15 hapi
npm ERR! install Couldn't read dependencies
npm ERR! Linux 4.1.10-boot2docker
npm ERR! argv "node" "/usr/local/bin/npm" "install"
npm ERR! node v0.10.40
npm ERR! npm v2.14.1
npm ERR! path /myproject/hapi/package.json
npm ERR! code ENOPACKAGEJSON
npm ERR! errno 34

npm ERR! package.json ENOENT, open '/myproject/hapi/package.json'
npm ERR! package.json This is most likely not a problem with npm itself.
npm ERR! package.json npm can't find a package.json file in your current directory.

npm ERR! Please include the following file with any support request:
npm ERR! /myproject/hapi/npm-debug.log
The command '/bin/sh -c pwd && ls -al && npm install' returned a non-zero code: 34

最佳答案

不幸的是,当您执行ADD命令时,您使docker build缓存无效,因此之后的所有命令都将从头开始运行。从文档:

Note: The first encountered ADD instruction will invalidate the cache for all following instructions from the Dockerfile if the contents of have changed. This includes invalidating the cache for RUN instructions.

https://docs.docker.com/reference/builder/#add


我认为在这种情况下,最简单的方法是运行一个交互式容器,并从 Dockerfile逐个运行每个命令,这样您就可以查看哪个失败,并可以在尝试修复错误后立即再次运行它。
例如:
docker run -ti -v /path/to/your/code:/myproject/hapi node:0.10.40 bash

$ cd /myproject/hapi
$ npm install
然后,当您执行 ls -la时,您将能够看到 package.json不存在或其他错误,一旦找到答案,就可以将该命令添加到 Dockerfile中。

关于docker - 更有效地调试Docker脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33502494/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com