Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete files sent to Docker daemon build context

I ran this command in my home directory:

docker build .

and it sent 20 GB files to the Docker daemon before I knew what was happening. I have no space left on my laptop. How do I delete the files that were replicated? I can't locate them.

like image 412
Joe Nyugoh Avatar asked Jan 31 '23 00:01

Joe Nyugoh


2 Answers

What happens when you run docker build . command:

  1. Docker client looks for a file named Dockerfile at the same directory where your command runs. If that file doesn't exists, an error is thrown.
  2. Docker client looks a file named .dockerignore. If that file exists, Docker client uses that in next step. If not exists nothing happens.
  3. Docker client makes a tar package called build context. Default, it includes everything in the same directory with Dockerfile. If there are some ignore rules in .dockerignore file, Docker client excludes those files specified in the ignore rules.
  4. Docker client sends the build context to Docker engine which named as Docker daemon or Docker server.
  5. Docker engine gets the build context on the fly and starts building the image, step by step defined in the Dockerfile.
  6. After the image building is done, the build context is released.

So, your build context is not replicated anywhere but in the image you just created if only it needs all the build context. You can check image sizes by running this: docker images. If you see some unused or unnecessary images, use docker rmi unusedImageName.

If your image does'nt need everything in the build context, I suggest you to use .dockerignore rules, to reduce build context size. Exclude everything which are not necessary for the image. This way, the building process will be shorter and you will see if there is any misconfigured COPY or ADD steps in the Dockerfile.

For example, I use something like this:

# .dockerignore
* # exclude everything
!build/libs/*.jar # include just what I need in the image
  • https://docs.docker.com/engine/reference/builder/#dockerignore-file
  • https://docs.docker.com/engine/docker-overview/
like image 81
er-han Avatar answered Feb 01 '23 13:02

er-han


Likely the space is being used by the resulting image. Locate and delete it:

docker images

Search there by size column.

Then delete it:

docker rmi <image-id>

Also you can delete everything docker-related:

docker system prune -a
like image 39
Robert Avatar answered Feb 01 '23 14:02

Robert