Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Which sources does --cache-to and --cache-from cache for? #399

Closed
morlay opened this issue Sep 24, 2020 · 6 comments
Closed

[Question] Which sources does --cache-to and --cache-from cache for? #399

morlay opened this issue Sep 24, 2020 · 6 comments

Comments

@morlay
Copy link
Collaborator

morlay commented Sep 24, 2020

dockerx version: 0.4.2

I expect to cache /go/pkg/mod in github workflow cross multi builds.

some dockerfile like above:

# syntax = docker/dockerfile:experimental

FROM golang:1.15 as builder

WORKDIR /go/src

# for trigger rebuild
ENV COMMIT_SHA=xxx 

RUN --mount=type=cache,sharing=locked,id=gomod,target=/go/pkg/mod go build

FROM busybox

COPY --from=builder /go/bin/app /go/bin/app

Then run script to build

# create builder every time like in ci.
docker buildx create --use

docker buildx build \
    --load \
    --planform linux/arm64,linux/amd64 \
    --cache-to type=local,dest=/tmp/.buildx-cache \
    --cache-from type=local,src=/tmp/.buildx-cache \
    --tag gobuild \
    --file Dockerfile .

The --mount=cache not work. /go/pkg/mod always empty (go mod will re download).

image

is --cache-from and --cache-to only cache the build layers?

--mount=type=cache works well if builder not re-create until gc cleanup.

if i want to cache the /go/pkg/mod, could i have to copy /var/lib/buildkit from buildkit container?

real-world demo https://github.com/querycap/istio/runs/1159405706

@morlay morlay changed the title [Questing] Which sources does --cache-to and --cache-from cache for? [Question] Which sources does --cache-to and --cache-from cache for? Sep 24, 2020
@FernandoMiguel
Copy link
Contributor

--cache-from and --cache-to push all build layers to registry image/label of you choice, local or remote

@morlay
Copy link
Collaborator Author

morlay commented Sep 24, 2020

so --mount=type=cache not be included?

@FernandoMiguel
Copy link
Contributor

so --mount=type=cache not be included?

Only you know your setup, so you use the best cache mechanism for you.
You can use either or both

@morlay
Copy link
Collaborator Author

morlay commented Sep 24, 2020

@FernandoMiguel

any suggestion in this case?.
i need cache /go/pkg/mod for multi workflows in build stage.

but the --cache-to couldn't expose the cached files (/var/lib/buildkit) to host.
and buildkit recreated when each workflow starts, all cached files in /var/lib/buildkit losts

buildx not provide way to mount host path for /var/lib/buildkit when create buildkit

@morlay morlay closed this as completed Sep 24, 2020
@morlay
Copy link
Collaborator Author

morlay commented Sep 24, 2020

mv to moby/buildkit#1673 for further discussion

@2063113624
Copy link

2063113624 commented Oct 31, 2022

@morlay,我有几个js写的前端项目,想要用buildKit构建以减少npm install的时间。但是通过jenkins CI构建的时候总是报/app/node_moudles/目录下面找不到安装的依赖,你能帮忙给点建议吗?Dockerfile如下(文件中的一些变量可以忽略):

# syntax = docker/dockerfile:experimental
FROM $COMPILE_IMAGE as builder
WORKDIR /app
COPY package.json package-lock.json /app
RUN --mount=type=cache,target=/app/node_modules,id=$image_name,sharing=private \
    $install
COPY . /app
RUN --mount=type=cache,target=/app/node_modules,id=$image_name,sharing=private \
    $compile
FROM nginx:1.20.2
COPY --from=builder /app/dist /usr/share/nginx/html
WORKDIR /usr/share/nginx/html
EXPOSE 80
CMD nginx -g "daemon off;"

镜像构建命令:

export "DOCKER_BUILDKIT=1" && docker build  -t $SERVICE_IMAGE:${image_tag}  .

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

No branches or pull requests

3 participants