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

Make it possible to use build docker image using BuildKit #2857

Open
pawelebe opened this issue Jun 10, 2020 · 11 comments
Open

Make it possible to use build docker image using BuildKit #2857

pawelebe opened this issue Jun 10, 2020 · 11 comments

Comments

@pawelebe
Copy link

As per Docker release 18.09 it is possible to mount cache (using BuildKit) explained

Example dockerfile

# syntax = docker/dockerfile:1.0-experimental

#RUN --mount=type=cache,target=/home/gradle/.gradle gradle build

However when build such image, it is gives an error

Dockerfile parse error line 12: Unknown flag: mount

Code to build:

new GenericContainer<>(
                new ImageFromDockerfile()
                                .withDockerfile(Path.of("Dockerfile").toAbsolutePath())
        )

I order to build such docker image from CLI, you need to set DOCKER_BUILDKIT=1 in order to use buildkit.

While analyzing docker cli source code it seems like it is appending such query parameter to REST API call version=2

Unfortunately, I do not see any documented version parameters in the official API documentation.
https://docs.docker.com/engine/api/v1.40/#operation/ImageBuild

@bsideup
Copy link
Member

bsideup commented Jun 10, 2020

AFAIK BuildKit isn't just a Docker API call, but a whole new API.

Given that even Docker's official Python library (that is used in Docker Compose) have decided that it is easier to delegate it to an external binary, I am not sure there is much we can do here, unless we do the same (call docker build which will require Docker CLI).

/cc @rnorth

@pawelebe
Copy link
Author

pawelebe commented Jun 10, 2020

@bsideup thanks for super fast reply... so given that it seems like not so easy to implement, do you have any workaround to recommend?

My first idea is to get a dockerfile as string, do something like sed /--mount=type=cache.*// (remove all buildkit related stuff) and then pass it to testcontainers

@stale
Copy link

stale bot commented Sep 11, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you believe this is a mistake, please reply to this comment to keep it open. If there isn't one already, a PR to fix or at least reproduce the problem in a test case will always help us get back on track to tackle this.

@stale stale bot added the stale label Sep 11, 2020
@stale
Copy link

stale bot commented Sep 27, 2020

This issue has been automatically closed due to inactivity. We apologise if this is still an active problem for you, and would ask you to re-open the issue if this is the case.

@stale stale bot closed this as completed Sep 27, 2020
@cesdperez
Copy link

I'm interested in this, @pawelebe did you find a workaround?

@rnorth
Copy link
Member

rnorth commented Jun 8, 2021

Let's reopen - we shouldn't have let this get stale, so I apologise.

I think that buildkit is something we'll have to keep in mind, particularly if we want Testcontainers to retain the ability to build Dockerfiles while more and more buildkit features become relied upon.

That said, I can't see any immediate way to do it without the docker binary. This may be something we deem to be OK enough.

For now as a workaround I'd suggest executing docker build -t SOMENAME . (ideally from Java code, but perhaps from outside) and then pass the tagged image name to Testcontainers to run.

@trajano
Copy link

trajano commented Sep 29, 2022

It's a bit janky but I used the workaround specified by @morth https://github.com/trajano/spring-cloud-demo/blob/78ad12e7db4198fbedd94d2b2a4e87f5ae5ff187/gateway/src/test/java/net/trajano/swarm/gateway/ContainerTests.java#L55-L61 perhaps people can use it as an example.

UPDATE: corrected the line number as @sharongur pointed out

The lines were supposed to show how I started a docker compose build inside the container similar to @morth idea but can do multiple containers at once

@sharongur
Copy link

is there any update regarding this issue?

i've upgraded my projects dockerfiles to use buildkit buildx and now all my testcontainer arent working anymore...

i didnt quite understand how the demo you linked is of any help @trajano, would you mind elaborating ?

@sharongur
Copy link

@trajano so you are using the local machines docker-compose to build the image instead of the testcontainers dockerfile infrastructure and then just use the created image with testcontainers?

@trajano
Copy link

trajano commented Mar 30, 2023

That's correct, that way you take advantage of everything buildx has to offer including cache mounts and secrets.

@pekuz
Copy link

pekuz commented Feb 20, 2024

With testcontainers facing:

13:34:21.960 DEBUG [docker-java-stream--505742923] t.localhost/testcontainers/36kg4mfjztahmewr  ---> 2e32511ef14e
13:34:21.960 DEBUG [docker-java-stream--505742923] t.localhost/testcontainers/36kg4mfjztahmewr Step 8/17 : COPY --chmod=ugo+x entrypoint.sh /entrypoint.sh
13:34:21.960 DEBUG [docker-java-stream--505742923] t.localhost/testcontainers/36kg4mfjztahmewr 
13:34:21.960 ERROR [docker-java-stream--505742923] t.localhost/testcontainers/36kg4mfjztahmewr the --chmod option requires BuildKit. Refer to https://docs.docker.com/go/buildkit/ to learn how to build images with BuildKit enabled

while using ImageFromDockerfile.

NOTE: BuildKit is default since Docker 23.

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

No branches or pull requests

7 participants