dockerrusttargetarch

docker build eating up over 30 gigs of space in /var/lib/docker/tmp and /var/lib/docker/overlay2 with rust project


I am on Archlinux and am executing a simple docker build for a rust project that has a target directory of 12GB.

Due to this problem, I have moved the /var/lib/docker directory to /data/arch/var/lib/docker with: rsync -avg /var/lib/docker /data/arch/var/lib/ and rm -rf /var/lib/docker and ln -s /data/arch/var/lib/docker /var/lib/docker

The problem still persisted as the build reaches over 30GB which is all the free space I have on the data partition.

Inside the /data/arch/var/lib/docker directory, the tmp directory will grow to 12GB. Then the overlay2 will have at least 2 sub-directories grow with each having a diff directory and a merged directory both containing an app/target that will contain 12GB.

/data/arch/var/lib/docker
  /tmp -> 12GB
  /overlay2
    /dirXXX1
      /merged/app/target -> 12GB
      /diff/app/target -> 12GB
    /dirXXX2
      /merged/app/target -> 12GB
      /diff/app/target -> 12GB

However, the dirXXX2 directory doesn't actually get to 24GB as the build will run out of space and kill the whole process.

This seems overly excessive to me and wondering if something can be reconfigured here.

UPDATE:

I have also added to /etc/docker/daemon.json:

{ 
  "storage-driver": "devicemapper",
  "log-driver": "local",
  "log-opts": {
      "max-size": "100m",
      "max-file": "3"
}

Dockerfile:

FROM rust:1.72.0

WORKDIR /app

RUN apt update && apt install lld clang -y

COPY . .

RUN cargo build --release

ENTRYPOINT ["./target/release/zero2prod"]

Solution

  • Add the target directory to a .dockerignore file in your project, otherwise:

    1. the whole target directory from the host (if present) needs to be sent to the Docker daemon, which takes a while when it's several gigabytes big; and
    2. the COPY . . command in your Dockerfile copies the target directory from the host into the layer for that command.

    (In general, you probably want to keep your .dockerignore in sync with your .gitignore/.hgignore/etc., but for Rust projects, the target directory is especially important.)