How can I inspect the file system of a failed docker build

0 votes

I'm trying to build a new Docker image for our development process, using cpanm to install a bunch of Perl modules as a base image for various projects.

While developing the Dockerfile, cpanm returns a failure code because some of the modules did not install cleanly.

I'm fairly sure I need to get apt to install some more things.

My question is, where can I find the /.cpanm/work directory quoted in the output, in order to inspect the logs? In the general case, how can I inspect the file system of a failed docker build command?

After biting the bullet and running a find I discovered

/var/lib/docker/aufs/diff/3afa404e[...]/.cpanm

Is this reliable, or am I better off building a "bare" container and running stuff manually until I have all the things I need?

Jul 30, 2018 in Docker by Hannah
• 18,520 points
3,135 views

2 answers to this question.

0 votes

Everytime docker successfully executes a RUN command from a Dockerfile, a new layer in the image filesystem is committed. Conveniently you can use those layers ids as images to start a new container.

Take the following Dockerfile:

FROM busybox

RUN echo 'foo' > /tmp/foo.txt

RUN echo 'bar' >> /tmp/foo.txt

and build it:

$ docker build -t so-2622957 .

Sending build context to Docker daemon 47.62 kB

Step 1/3 : FROM busybox

 ---> 00f017a8c2a6

Step 2/3 : RUN echo 'foo' > /tmp/foo.txt

 ---> Running in 4dbd01ebf27f

 ---> 044e1532c690

Removing intermediate container 4dbd01ebf27f

Step 3/3 : RUN echo 'bar' >> /tmp/foo.txt

 ---> Running in 74d81cb9d2b1

 ---> 5bd8172529c1

Removing intermediate container 74d81cb9d2b1

Successfully built 5bd8172529c1

You can now start a new container from 00f017a8c2a6, 044e1532c690and 5bd8172529c1:

$ docker run --rm 00f017a8c2a6 cat /tmp/foo.txt

cat: /tmp/foo.txt: No such file or directory

$ docker run --rm 044e1532c690 cat /tmp/foo.txt

foo

$ docker run --rm 5bd8172529c1 cat /tmp/foo.txt

foo

bar

of course you might want to start a shell to explore the filesystem and try out commands:

$ docker run --rm -it 044e1532c690 sh     

/ # ls -l /tmp

total 4

-rw-r--r--    1 root     root             4 Mar  9 19:09 foo.txt

/ # cat /tmp/foo.txt

foo


When one of the Dockerfile command fails, what you need to do is to look for the id of the preceding layer and run a shell in a container created from that id:

docker run --rm -it <id_last_working_layer> bash -il

Once in the container:

  • try the command that failed, and reproduce the issue
  • then fix the command and test it
  • finally update your Dockerfile with the fixed command
answered Jul 30, 2018 by Kalgi
• 52,350 points
0 votes

Failed command is a build that has taken place several hours back, so rewinding the build to that stage is a waste of time.

Follow these steps instead

1) Find the container that failer

docker ps -a

2) Commit it to an image

docker commit 6934ada98de6

3) Run the image

 docker run -it 7015687976a4 [bash -il]

answered Aug 6, 2018 by Nilesh
• 7,060 points

Related Questions In Docker

0 votes
2 answers

How do I force Docker for a clean build of an image ?

You could try this inorder to clean ...READ MORE

answered Aug 6, 2019 in Docker by Sirajul
• 59,230 points
10,699 views
0 votes
1 answer

How do I delete the build cache for a docker image?

You could try this inorder to clean ...READ MORE

answered Jul 3, 2019 in Docker by Sirajul
• 59,230 points
12,635 views
+1 vote
0 answers

How can I map a docker location to my Jenkins workspace so that I can use the files in my Jenkins

Hey guys, I was trying to execute few ...READ MORE

Feb 25, 2020 in Docker by dineshkumar
• 130 points
4,936 views
+1 vote
2 answers

How do I copy a file from docker container to host?

Here is a way which you can ...READ MORE

answered Aug 28, 2018 in Docker by Damon Salvatore
• 5,980 points
28,582 views
+2 votes
1 answer
+2 votes
1 answer

Deploy Docker Containers from Docker Cloud

To solve this problem, I followed advice ...READ MORE

answered Sep 3, 2018 in AWS by Priyaj
• 58,020 points
2,719 views
+15 votes
19 answers

How can I run a ‘docker exec’ command inside a docker container?

you can run any command in a ...READ MORE

answered Dec 10, 2018 in Docker by Pramiti
1,229,602 views
0 votes
2 answers

How to edit file after I shell to a docker container?

You can even install it using a ...READ MORE

answered Apr 23, 2019 in Docker by Ashish
17,848 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP