更有效地debuggingdocker脚本

我开始在我的第一个docker脚本,我试图debugging的最后一步,这是非常慢的debuggingB / C的步骤之前,需要几分钟的运行,所以如果我有一个错字,我不得不重新运行整个脚本。

有没有更有效的方法来debuggingdocker脚本? 或者我每次都要重build整个事物?

#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 cache无效,所以之后的任何命令都将从头开始运行。 从文档:

注意:如果内容发生了变化,第一次遇到的ADD指令将使Dockerfile中的所有后续指令的caching无效。 这包括使RUN指令的caching无效。

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或者不存在,或者其他的东西是错误的,一旦你find了它,你可以把这个命令添加到你的Dockerfile