Docker: failed to build: max depth exceeded

Escrito na categoria "DevOps" por Alex Benfica.

Docker is a tool every developer should use. It makes a lot easier to create a development environment. Actually, it makes easier to create many different environments in the same machine at the same time.

There is no turning back to the old style programming when you start using docker (or similar tools) in your project.

Well… I use docker for every project! Python, PHP, small tests, WordPress and many sites and services I have in production. It just works!

You probably know that already, right?

I was using docker to developer some WordPress plugins and make some theme customizations and when I tried to build an image with “docker-compose build” I was presented with the error message:

failed to build: max depth exceeded

Docker makes setups easier

This was my previous docker-compose.yml file that was causing the error.

What is causing this error?

Well… the problem is that I was using “build” and “image” at the same time and with this Dockerfile (below).

The Dockerfile is starting from image: wordpress:4.9.6-php7.2-apache … and writing the results to the same image, as specified in line 26 of the above docker-compose.yml.

Fixed: failed to build: max depth exceeded

In order to fix this issue, make sure that you are using an image name different from the one your Dockerfile starts from!

Otherwise, each build will add layers to your “final image” and you’ll end up with this max depth exceeded.

This error means that your image has too many layers.

This is the final docker-file.yml for my devel environment

Fixed: Docker build not using cache

Another side effect of this issue is that docker builds will always start from scratch as if you were asking to not use cache!

That is because you’re updating the initial image on every build. Simply does not update the image you are starting your build from!

Fix your docker-compose.yml file and your image builds will back to use cache and be fast again!

Sobre o autor

Autor Alex Benfica

IT professional with over 20 years of experience in industry. BSc in Computational Mathematics, always learning and enthusiastic about software development and automation. Full stack developer proficient in Javascript, Node, React, Python, Docker, API design and many more!

Leave a Reply