docker / buildx

Docker CLI plugin for extended build capabilities with BuildKit
Apache License 2.0
3.54k stars 480 forks source link

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

Open trajano opened 1 year ago

trajano commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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

saeidscorp commented 6 months ago

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

erez-rabih commented 1 month ago

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