Skip to content

Conversation

@bennesp
Copy link
Contributor

@bennesp bennesp commented Apr 22, 2025

Closes #48 (thank you @omus for sharing a very useful code snippet)

This PR allows users to avoid specifying cache-map: the cache-map will be automatically created by inspecting the AST of the Dockerfile. Of course, users can continue specifying cache-map, in situations where users need more control.

Users can specify a Dockerfile of their choice with the dockerfile option.

For each cache mount, the respective generated cache-map entry is the following:

  • cache-map source: The id of the cache mount if specified, otherwise the target of the cache mount
  • cache-map target: The absolute path of the target (prefixed by the WORKDIR, if any)

Example:

FROM alpine:latest AS builder

# Target absolute path, no id
RUN --mount=type=cache,target=/tmp/cache \
    echo "Hello, World!" > /tmp/cache/hello.txt

# Target relative path with workdir, no id
WORKDIR /app
RUN --mount=type=cache,target=cache1 \
    echo "Hello, World!" > cache/hello.txt

# Multi-stage build
FROM alpine:latest

# Target absolute path with id
RUN --mount=type=cache,id=cache2,target=/tmp/cache \
    echo "Hello, World!" > /tmp/cache/hello.txt

# Target relative path with workdir and id
WORKDIR /app2
RUN --mount=type=cache,id=cache3,target=cache \
    echo "Hello, World!" > cache/hello.txt

This dockerfile will generate the following cache-map:

{
  "/tmp/cache": {
      "id": "/tmp/cache",
      "target": "/var/cache-target"
  },
  "cache1": {
      "id": "cache1",
      "target": "/var/cache-target"
  },
  "cache2": {
      "id": "cache2",
      "target": "/var/cache-target"
  },
  "cache3": {
      "id": "cache3",
      "target": "/var/cache-target"
  }
}

@bennesp bennesp force-pushed the automatic-cache-map-discovery branch from 8a21fcd to ff982a4 Compare April 22, 2025 13:03
Copy link
Contributor

@omus omus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

bennesp added 2 commits April 22, 2025 22:09
Signed-off-by: bennesp <[email protected]>
@bennesp bennesp force-pushed the automatic-cache-map-discovery branch from 395734c to 9b9fed1 Compare April 22, 2025 20:12
@bennesp bennesp force-pushed the automatic-cache-map-discovery branch from dba22cf to 429cead Compare April 22, 2025 20:23
@bennesp
Copy link
Contributor Author

bennesp commented May 2, 2025

@AkihiroSuda do you think you could kindly take a look at this PR? 🙏

Copy link
Member

@AkihiroSuda AkihiroSuda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@AkihiroSuda AkihiroSuda merged commit 653a570 into reproducible-containers:main May 3, 2025
3 checks passed
@AkihiroSuda
Copy link
Member

Next time please consider squashing commits

@bennesp
Copy link
Contributor Author

bennesp commented May 3, 2025

Thank you for the quick response, I will squash next time!
Best

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Determine default cache map from Dockerfile

3 participants