buildx icon indicating copy to clipboard operation
buildx copied to clipboard

Provide option to allow access to /var/run/docker.sock during build

Open trajano opened this issue 2 years ago • 6 comments

Description

In order for tools like TestContainers to work inside a docker build it would need access to a docker daemon. I tried

RUN  --mount=type=bind,rw,source=/var/run/docker.sock,target=/var/run/docker.sock

to no avail, likely because I am on Windows. What I would propose is

RUN --mount=type=docker

Which will provide the /var/run/docker.sock during the run and will adapt to the current context. However, it can be extended to allow other contexts aside from the current.

RUN --mount=type=docker,context=anothercontext

rel: https://stackoverflow.com/questions/76842452/how-do-i-access-var-run-docker-sock-inside-docker-build

### Tasks
- [ ] Allow creating the bind mount and provide an example that shows TestContainers working for a simple local build
- [ ] Provide capability to handle alternate contexts.

trajano avatar Aug 05 '23 16:08 trajano

My workaround for Docker desktop is https://stackoverflow.com/a/76842771/242042

This is specifically for Docker Desktop for Windows. First you need to expose the Docker daemon

docker run -p 2375:2375 -v //var/run/docker.sock://var/run/docker.sock alpine/socat tcp-listen:2375,reuseaddr,fork unix-connect:/var/run/docker.sock

Then in your Dockerfile you have

ENV DOCKER_HOST=tcp://host.docker.internal:2375 
ENV TESTCONTAINERS_HOST_OVERRIDE=host.docker.internal 
RUN gradle --no-daemon test

trajano avatar Aug 05 '23 17:08 trajano

I have tested on Linux with the bind mount no luck still

ERROR: failed to solve: failed to compute cache key: failed to calculate checksum of ref 0ec2159a-9e97-4835-9cf4-7427f9526e4b::snj9qtt8vlr50m3fsd24nntto: "/var/run/docker.sock": not found

trajano avatar Aug 06 '23 00:08 trajano

I second this. Is there a specific reason the UNIX socket is not bind-mountable for the builder whereas it is for docker run?

saeidscorp avatar Apr 14 '24 19:04 saeidscorp

+1 - running testcontainers during build step could be very helpful

erez-rabih avatar Aug 28 '24 09:08 erez-rabih