更高效地调试docker脚本

时间:2015-11-03 15:09:24

标签: docker

我开始使用我的第一个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

1 个答案:

答案 0 :(得分:2)

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

  

注意:如果内容已更改,则第一个遇到的ADD指令将使来自Dockerfile的所有后续指令的高速缓存无效。这包括使RUN指令的高速缓存无效。

     

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