Docker - max depth exceeded

Delete all local docker images related to your dockerfile using the following and try again.

$ docker rmi -f $(docker images -a -q)

If this error pops for specific images, then you might want to take a look at the number of layers that exist in your said docker image. When doing changes to a Docker image, each change is getting added as a layer regardless the size increase in the image. I have come across scenarios where my docker images having multiple layers and failing to push in Jenkins jobs. The workaround is to use the same context in the docker image but with reduced amount of layers. For that you can simply take out the content of the image and port them into a new image with a new image name, which will result the same content in a new image and with less amount of layers. Use this tip across scenarios that is helpful. Thanks


The image parameter for a service in a docker-compose.yml definition has dual meanings depending on the existence of a build parameter.

  • If there is no build stanza, The image will just be pulled and run.

  • If you have a build stanza, image will be the name your built image is tagged as, and run.

By naming the built image microsoft/mssql-server-linux, which is the same as the FROM microsoft/mssql-server-linux image. Docker was layering the build on top of itself each time.

The original build started on the "official" microsoft/mssql-server-linux but then each subsequent build would start from your local microsoft/mssql-server-linux image which had been appended to, until eventually you hit the maximum number of layers for your storage driver.

Use your own namespace for all images you build:

version: "3"
services:
  mssql:
      build: .
      image: 'user3437721/mssql-server-linux'

I was already using a custom image name like in the accepted answer, and I still had this issue.

In order to get past this error, I found that I needed to remove all unused (not associated with any container) and dangling (old image after a new one is made) docker images with the following command:

docker image prune -a

See https://linuxhandbook.com/remove-docker-images/