buildx: network mode "custom_network" not supported by buildkit
Background: Running a simple integration test fails with network option:
docker network create custom_network
docker run -d --network custom_network --name mongo mongo:3.6
docker buildx build --network custom_network --target=test .
Output:
network mode "custom_network" not supported by buildkit
Still not supported? Code related: https://github.com/docker/buildx/blob/master/build/build.go#L462-L463
About this issue
- Original URL
- State: open
- Created 7 years ago
- Reactions: 26
- Comments: 83 (13 by maintainers)
Indeed @Hronom, it feels like gaslighting when they act like
Please keep such comments at the door. No need for this.
I’ll skip over some of you other wordings, because I don’t think there’s anything constructive in those
There’s many things associated with this, and this feature is not trivial to implement (if possible at all). Let me try to do a short summary from the top of my head. Note that I’m not a BuildKit maintainer; I’m familiar with the overal aspects, but not deeply familiar with all parts of BuildKit.
First of all, buildx (the repository this ticket is posted in) is a client for BuildKit; the feature requested here would have to be implemented by the builder (BuildKit); Ideally this ticket would be moved to a more appropriate issue tracker, but unfortunately GitHub does not allow transferring tickets between orgs; in addition, multiple projects may need to be involved. There’s no harm in having a ticket in this repository (nor to have discussions elsewhere), but ultimately this feature would have to be both accepted by upstream BuildKit and/or Moby maintainers, and implemented respectivee projects.
Now the more technical aspect of this feature request (or: this is where the fun starts).
BuildKit can be run in different settings / ways;
docker buildx create) which can create new containerized buildersDepending on the above, semantics, and available features will differ.
Standalone BuildKit builders
Standalone builders are designed to be stateless; builders may have cache from prior builds, but don’t persist images (only build-cache), nor do they have a concept of “networks” (other than “host” networking or “no” networking). The general concept here is that standalone builders can be ephemeral (auto-scaling cluster), and that builds don’t depend on prior state. They may advertise certain capabilities (BuildKit API version, native platform/architecture), but otherwise builders are interchangeable (perform a build, and export the result (e.g. push to a registry, or export as an OCI bundle).
Given that standalone builders don’t have access to “docker custom networks” (there’s no docker daemon involved), it won’t be possible to provide a per build option to use a specific network. The workaround mentioned in this thread is to;
In this configuration, the “host” (container) can resolve containers running in that custom network, but this only works in very specific scenarios;
All of the above combined make this a workaround that would work in only very specific scenarios. Implementing this as a native feature for standalone builders would quickly go down the rabbit-hole; “custom network attached to this builder” as well as “state of dependencies” would have to be exposed as capabilities, so that buildx can query which builder to select for a specific build, or an external orchestrator would need to be involved to construct builders (and dependencies) on-demand. Both would be moving away significantly from the current design of standalone builders (stateless, ephemeral).
As part of the Moby daemon (“Docker Engine”)
When using the "default’ builder on Docker Engine, BuildKit is not running as a standalone daemon, but compiled into the Docker Engine. In this scenario, BuildKit has some (but limited) access to features provided by the Docker Engine. For example, it’s possible to use images that are available in the local image cache as part of the build (e.g. an image you built earlier, but that’s not pushed to a registry). There’s also limitations; when using “graphdrivers”, the Docker Engine does not provide a multi-arch image store, so it’s not possible to build multple architectures in a single build. (This will be possible in the near future and being worked on as part of the containerd image store integration).
Containers created during build are optimized for performance; containers used for build-steps tend to be very short-lived. “regular” containers as created by the Docker Engine have a non-insignificant overhead to provide all features that
docker runcan provide. To reduce this overhead, BuildKit creates optimized containers with a subset of those features; to further improve performance, it also skips some intermediate processes: BuildKit acts as its own runtime, and it can directly use the OCI runtime (runc) without requiring the intermediate “docker engine”, and “containerd” processes.Long story short; build-time-containers are (by design) isolated from “regular” containers; they’re not managed by the docker daemon itself, won’t show up in
docker ps, and networking is not managed by the regular networking stack (no DNS-entries are registered in the internal DNS).So, while the “embedded” BuildKit may have more potential options to integrate with custom networks, integrating with custom networks will require a significant amount of work; both in BuildKit (integration with the network stack) and in Moby / “docker engine” to (somehow) allow BuildKit to create different “flavors” of containers (isolated / non-isolated). This will come with a performance penalty, in addition to complexity involved (a secondary set of (short-lived) containers that can be attached to a network, but are not directly managed by the Moby / Docker Engine itself.
Heya, so, I’m no longer at docker and so only really speak for myself here, but I do feel the need as a maintainer on this project to call this out.
This is an open source project. Contributions and contributors are welcome - any person in this thread is welcome to engage on how to fix this issue, and upstream any contributions. It’s known to be a limitation, but demanding that the maintainers drop everything to work on this is just not great form, sorry. Just like you, the maintainers of this project are busy, having to deal with internal/external constraints on their time, and often other things take priority.
That said, if you’re a paid user of docker and this is affecting you, and you need to see this resolved, then you should raise this through support/your account representative. They can escalate internally and help set engineering priority - this is generally true of any corporate-backed open source project.
Finally, essentially calling for a brigade of a github thread during the break between Christmas and the new year, a time which very often people take off from work and would not be able to respond is… not great. I sincerely hope no good open source citizen would do this, but if they had, it would have the potential to disrupt people’s time with family and friends. Please don’t do that.
Please let’s move this thread back on track - sharing workarounds and discussing ways in which this could be worked on productively.
I’m not surprised that someone with pronouns in their GitHub profile just created an entire victim narrative out of this. However, I am thankful for jedevc for pointing out that I should try pushing on a docker account rep for this issue.
Calling it a “brigade” is your narrative - not a fact. Especially when loads of stuff has recevied positive attention via this method, there is no rule against posting in a more general developer publication (which HN is), and after the attention was gotten via that method no one retrospectively said “I know billion dollar corporation X helped us only after thread Y was posted, but we now realize this was actually a brigade.” I believe it is clear in my comment that I am not for pushing anyone who is working for free to fix it, but rather for the utilization of Docker Inc’s power to fix (restore) the functionality.
I will also add that “we should not post anything that causes workers to scramble unnecessarily during the holidays” - despite the fact that I never said the opposite. I do realize it could be implied by the fact that I made the suggestion on Dec 29th, but I view this as an async rather than sync comm.
But I’d like to spin up a network for each build - and have all the stuff running that would be needed for the integration tests. But again, I have to loop back around and either do weird stuff with iptables, or run postgres on the host and share it with all builds (contention/secrets/writing to the same resources/etc).
You could see how it would be so much more encapsulated and attractive if I could spin up a network per build with a bunch of stub services and tear it down afterwards ?
Sorry, I’m not sure if we will ever start supporting this as it makes the build dependant on the configuration of a specific node and limits the build to a single node.
This is particularly needed in environments such as Google Cloud Build where ambient credentials (via special-IP metadata service) are available only on a particular named network, not on the default network, in order to keep their exposure to build steps opt-in.
Adding another use case where specifying the network would be useful: “hermetic builds”.
I’m defining a docker network with
--internalthat has one other container on the network, a proxy that is providing all the external libraries and files needed for the build. I’d like the docker build to run on this network without access to the external internet, but with access to that proxy.I can do this with the classic docker build today, or I can create an entire VM with the appropriate network settings, perhaps it would also work if I setup a DinD instance, but it would be useful for buildkit to support this natively.
My two cents about use cases.
I was led here after trying to collapse a complex CI process into a Dockerfile. Specifically, a step runs migrations on a DB, another step dumps the DB’s schema to a file, and a third step generates templates using that output. All this work follows the mandate of Docker build recipes—repeatable, portable—but the implementation of spinning up a DB doesn’t fit with the paradigm.
Reason being: while migrations can run on the DB in step one, the DB itself must be running and accessible for step two, which has to run on a different image. By design, you can’t keep a build layer alive once you’ve moved on to another one. Thus, in my use case, it points to a reasonable need for the DB to be attached as an external service, but still scoped and isolated to the build process.
Regrettably, the current tooling ecosystem almost serves this purpose. It provides a
networkparameter, but won’t allow custom networks. There is a workaround using buildx containers, but in my experimentation, that workaround did not work-around (code not available; I could not reach the container that should allegedly have been attached).Seeking support for the issue, one then reaches this labyrinth of support tickets. And the first thing one sees there, confusingly, is the proverbial “slap of the wrist” for wanting or expecting the feature to exist in the first place.
As I see it: Docker builds are supposed to be able to network. We know this. To avoid unpredictable build outputs, the user should use the internet only to download resources that are themselves invariant for a given set of build parameters. If the user breaks this rule, they have earned the consequences.
Why should resources isolated to a custom network be any different? Docker networks are an extension of the general concept of networking, and in this context an irreducible one. One of the tolls you pay for moving off of bare metal is that containers need a mediating layer if they want to be able to talk to each other in an un-ambiguous way. It’s natural to expect that tool to be available wherever it is needed, and build-time is one of the places where it is sometimes needed.
What I’m saying is: this isn’t really about repeatability or portability, is it? It’s about difficulty of implementation. I appreciate that it takes a vast amount of work to satisfy a particular use-case without breaking something else, but that makes it all the more unfortunate to see effort be wasted on framing a difficult use-case as somehow un-“Dockerly”. Especially because it is only difficult as a result of competing priorities taking precedence (BuildKit cache optimizations etc) and especially because of CLI, docs, and GitHub leading developers down a garden path that sometimes ends at that extremely unsatisfying answer.
OK, so once you’ve got it set up, how do you get name resolution to work? If I have a container
foothat’s running on my custom network, and I dodocker run --rm --network custom alpine ping -c 1 foo, it’s able to resolve the namefoo. Likewise, if I create a builder withdocker buildx create --driver docker-container --driver-opt network=custom --name example --bootstrap, and thendocker exec buildx_buildkit_example0 ping -c 1 foo, that works. But if I have a Dockerfile withRUN ping -c 1 fooand then rundocker buildx build --builder example ., I getbad address foo. If I manually specify the IP address, that works, but hard-coding an IP address into the Dockerfile hardly seems reasonable.Hmm… right, but that should not be the case when using
--network=host. In that case, the container should inherit the/etc/resolv.conffrom the host; here’s a docker-in-docker running with a custom network (127.0.0.11is the ambedded DNS resolver); a container with--network=hostinherits the host’s settings;Trying to do the same with a BuildKit container builder that has
hostnetworking allowed, and a build started with--network=hostshows that BuildKit does not do the same; it uses the default DNS servers;Creating a custom network and a “test-server” container attached to it;
Create a custom builder attached to the network, and allow “host-mode” networking;
Running a build with
--network=host, which should run in the host’s networking namespace and inherit the host’s DNS configuration (127.0.0.11- the embedded DNS);However, it looks like BuildKit sets the default DNS resolvers, not inheriting from the host;
So I think there’s something funky going on there, and BuildKit’s executor / runtime does not take
hostnetworking into account for DNS resolvers 🤔Had a quick peek at code that I think is related to this; it looks like it logs a message about host networking; https://github.com/moby/buildkit/blob/8849789cf8abdc7d63ace61f8dc548582d22f3b5/executor/runcexecutor/executor.go#L184-L188
But after that unconditionally uses the standard
/etc/resolv.confthat was generated (and used for all containers used during build); https://github.com/moby/buildkit/blob/8849789cf8abdc7d63ace61f8dc548582d22f3b5/executor/oci/resolvconf.go#L27-L118I found a nice workaround, it’s also relevant to any other frontend framework too, short workaround strategy is this:
hostnetwork mode available, meaning by default the build happens insandboxedisolated mode, so we switch our Next.js app build time network to host mode from the isolationhostnetwork, so follow this rule:From your container's perspective localhost is the container itself. If you want to communicate with the host machine, you will need the IP address of the host itself. Depending on your Operating System and Docker configuration, this IP address varies. host.docker.internal is recommended from Docker version 18.03 onward.host.docker.internalas Strapi app hostname, if we take into account Strapi+Postgres container up and running with:1337port exposed for Strapi we need connection string value like thisNEXT_PUBLIC_STRAPI_API_URL=http://host.docker.internal:1337/graphqlThat way we have build and runtime both working just fine, without external network setup, just two independent docker-compose files started locally on the same machine It works like that:
hostmode from thesandboxedmode -> it builds and asks graphql for data -> connects tohost.docker.internal:1337-> Docker’s internal network redirects it tolocalhost:1337-> Docker routes this to locally exposed Strapi running at:1337port -> connection is fineso, frontend build stage can connect straight to internet to fetch cloud CMS instance or
host.docker.internalconnection to access another local docker-compose setup, but it can’t connect directly tolocalhost:1337or attach to custom network and use DNS there likehttp://strapi:1337/, so the build stage network setup is very limited and needs some tweaking like this, onlyhttps://cms-in-the-cloud.com/graphqlor onlyhttp://host.docker.internal:1337/graphqland work outside the default sandboxed mode set for docker buildhost.docker.internalThis will work for you on the Windows machine with latest Docker version used (Docker v3 and above with Buildkit engine), for Linux/MacOs consider to use 127.0.0.1 address instead if you have older versions of Docker
To make this work follow the external network configuration at Docker like this
Docker-compose A for Next.js Specify .env connection to Strapi like this
NEXT_PUBLIC_STRAPI_API_URL=http://host.docker.internal:1337/graphqlDocker-compose B for Strapi + PostgreSQL for data storage
Run the Docker-compose B to get Strapi up, and then run the Docker-compose A to run the Next.js build+run process that will go through the
buildandrunphases of it nicely through theNEXT_PUBLIC_STRAPI_API_URL=http://host.docker.internal:1337/graphqlconnection, that’s it!More docs:
Read https://github.com/docker/buildx/issues/591#issuecomment-816843505 to see
Buildkit supports none, host, default(sandboxed) as network mode. Supporting custom network names is currently not planned as it conflicts with the portability and security guarantees.and don’t use any other custom networks with Buildkit, that’s the new reality ofdocker buildcommand.In short, custom network cannot be specified for the build step of the docker-compose anymore, meaning that connecting two docker-compose files into a single custom external network to have build running as before is not feasible anymore after the Docker moved to this https://docs.docker.com/build/buildkit/ engine which was enabled by-default in v23.0.0 on 2023-02-01 (https://docs.docker.com/engine/release-notes/23.0/#2300)
With custom network runtime phase is always fine but this Docker v3 update breaks the build phase completely making the build impossible if any
getStaticPropsstatic asset can’t be fetched during the build phase(docker build)and fails the build so it never reaches the runtime phase(docker run)(if you skip build and got to runtime, it will work)The use case for this setup: Your organization has
website/Strapiandwebsite/Nextjsrepositories separately, but you just want to run them both locally in same Docker network runningwebsite/Strapidocker-compose file andwebsite/Nextjsdocker-compose file with build step and getStaticProps, this is stupid simple use case we all use in everyday life and this is where Docker v3 introduced that breaking change.Any other solution I found is to use
docker buildx ...but that way you lose thedocker-composefeatures or merge strapi and next.js in one big monorepo with big single docker-compose fileOk, so for those who is looking for some solution like me, here I created repo https://github.com/Hronom/buildx-add-host-example with workaround that is aggregated from this thread and from this topic.
There I put examples for local usage and GitHub Actions usage with
setup-buildx-actionandbuild-push-action@poconnor-lab49 big respect to you! Thanks for the inspiration in this topic
Shame to buildkit/buildx that you still not able to find normal solution within 4 years for common and popular use case. This is really sad story.
The recommendation is to use
buildx create --driver-opt network=custominstead when you absolutely need this capability. The same applies to the google cloud build use case.Also require this option due to build it on restricted environment, despite what environment it will be “run” on.
To repeat a nearly 2 year old comment here, buildkit does support running with a custom network, it’s even documented: https://docs.docker.com/build/drivers/docker-container/#custom-network
The issue isn’t that it won’t run on a custom network, instead, as so often happens on the internet, it was DNS. When you run a container runtime (which buildkit does) inside of a container on a custom network, it sees the DNS settings of that parent container:
And as docker and other runtimes do, they refuse to use 127.0.0.11 as a DNS server, so the nested container falls back to 8.8.8.8. Here’s a demo for proof:
Running that shows that custom networks are supported, just not DNS:
There’s no immediate plans to actively remove the classic builder, but no active development is happening on it; consider it in “maintenance mode”, and mostly to support building native Windows containers (BuildKit does not yet support Windows Containers, although work on that is in progress). The classic Builder’s architecture does not give a lot of room for further expansion, so it will start to diverge / get behind BuildKit more over time. The classic builder may also not make the transition to the containerd image-store integration; there’s currently a very rudimentary implementation to help the transition, but we’re aware of various limitations in that implementations that may not be addressable with the classic buidler.
Perhaps it’d be useful to start a GitHub discussion; GitHub tickets aren’t “great” for longer conversations and don’t provide threads (not sure which repository would be best; perhaps BuildKit (https://github.com/moby/buildkit/discussions) to collect slightly more in-depth information about use-cases. I know there was a lot of contention around the original implementation, which also brought up concerns about portability and the feature being out of scope for building (see lengthy discussion on https://github.com/moby/moby/issues/10324 and https://github.com/moby/moby/pull/20987 (carried in https://github.com/moby/moby/pull/27702)).
Use-cases that I’m aware of;
docker buildas a CI pipeline; spin up a stack of containers to test/verify things in an intermediate stage before tagging the final stageBut there may be other use-cases. Some of those may make more sense in a “controlled” environment (local / special purpose machine; single user), but get complicated fast in other environments. Having more data about use-cases could potentially help design around those (which may be through a different approach).
TLDR; Which scenario would be better?
EDIT: For me personally - build time is not the primary thing I am after. I wish the solution to work. Currently using the classic builder still works, but my primary concern is that this feature disappears.
Any workaround which involves ‘finding out the IP of’ is not acceptable, since that absolutely does not make it portable 😃. That works maybe for a local setup where you are the only one using the docker files and you know your host. In any kind of network setting or team setting, that is out of the window.
Also, can’t be ‘the way it is supposed to be’
The thing is that they are still recommending (and it’s kind of needed with how it work) to disable the builtin bridge network as step 1 after installing Docker Engine. So you need to create custom networks and supply them to any container or build command that starts and needs internet or some sort of container-to-container network mode. Breaking the
--networkcommand fromdocker build(since it’s passed blindly to buildx which uses a different network parameter all together) means breaking the build command.I know a lot of people here are commenting about how it works with docker-compose, but it just basically breaks the default
docker buildcommand. How…The workaround is to create your own builder, and apply a network to that. But I would much rather just see the ‘buildx’ enforcement being removed, because it seems completely useless to me if you are just building things locally to then push. But that’s also not a good workaround if you often switch networks for your build commands (as you do in a docker-compose file with custom networks).
In theory, this means you use docker-compose to build and then launch? Simply building in a separate step would work I guess. I don’t use docker-compose here. Development is done locally and I’m the one making the images for deployment for the team, so I only work local and push to our registry.
If someone can tell me howto go back to the normal old ‘docker build’ command and just disable and ignore the whole buildx thing, yes please.
Indeed, ideally it would be more simple. These workarounds are ugly as hell.
I will send $100 to anyone who adds custom networks back to docker compose by default (without disabling BuildKit). Sorry it is low, but I’m not valued in the billions of dollars, unlike the Docker corporation.
I can’t cope anymore. Going in circles trying to mitigate functionality issues between compose/docker/buildx/bake/buildkit when all I want is to be in one coherent environment (compose). It just overcomplicates everything in an already complicated environment which was previously working well. Until then I’ll continue to disable BuildKit like the folks above. Considering moving my infra over to podman or just baremetal.
Maybe @bryanhuntesl or others wouldn’t mind chipping in if my offer is to meager on its own.
That horse has bolted - SSH mount makes the build dependent upon the configuration of a single node - where did that dogma even get started?
It was blocked immediately by this:
Where is the evidence for this claim? Also, it seems to me that this person is on Docker’s payroll - and yet I am still not asking them to stop their holiday and work @jedevc - I’m merely replying with a reasonable question in a session of asynchronous communication. They can choose to use the technology they own so they only see work messages during work hours, for example.
The comment also implies that the fix for this issue will not be accepted by Docker, so it definitely dissuades anyone (who isn’t on the Docker payroll) from attempting a fix during their free time. That’s why the argument that “this is an open source project” work for us rather than Docker Inc in this senario.
It seems GCE’s metadata server IP is
169.254.169.254(but I’m not sure if this is always the case), so this worked for me in Google Cloud Build:and inside
Dockerfile(or using Cloud Client Libraries which use Application Default Credentials):Why would someone do that? ssh-agent is a something that needs to be fairly well locked down - why would someone forward it across an insecure connection?
I mean, that’s a tangent anyway. Being able to run integration-tests in a docker build was an incredibly useful feature, one less VM to spin up, and one less iceberg to melt, it’s just useful because it’s efficient.
It’s also great to not have to run nodejs, ruby, etc on the build host but instead just have them as container dependency, if you can do all your tests in a docker build container it’s one less thing to lock down.
Anyhow, I apologise for running off on a tangent. All I’m saying is, it would be awesome if you could bring that functionality into the latest version of docker along with the means to temporary mount secrets. It’s just a really lightweight way to run disposable VMs without touching the host or even giving any rights to run any scripts or anything on the host.
My usecase is a minimal personal container-based dev/test/stage environment where ‘docker—compose’ is the only “orchestration” (component-architecture) tool I need there; my git server is local to that “self server” environment, so, its component network is neatly included in many builds (more info on the setup) – and this issue here is one of the major reasons I’m not using ‘docker compose’ with buildkit this far. Thank you in advance for addressing it properly.
@TBBle If I understand correctly, you’re saying it’s not merely “hard to implement” but impossible to implement because of a decision about separation of concerns made by BuildKit?
I think I follow your explanation broadly but there are some unfortunate choices of phrase… For example “Docker is no longer in a position to make [custom networks in the build environment] work magically for you” is a strange way to phrase “Docker no longer allows one feature, provided by Docker, through the CLI, to leverage another feature, provided by Docker, through the CLI”. If that’s “magic” then we are all wizards!
I would be much happier with the status quo if someone, in the five years of this issue being open, had reported success in binding their custom network to their build environment by leveraging a custom builder. So far everyone who has tried this, including myself, has reported failure. Consider this an open request for a working example.
@MattiasMartens Very well said. I would just add that it was so easy and simple previously with the classic builder. You don’t know what you’d be missing until it is deprecated! I must say I don’t follow @TBBle’s thought process as well as Mattias’s. First, because I don’t consider it “magic” to make a network available to a container. And second because “the structure of the container build system has shifted to be much more strongly isolated” - meaning isolated from what exactly? Clearly not the internet. I’m not claiming I’m owed a more streamlined compose experience, but I do feel it is important to correctly communicate the experience - as Mattias has. This design short circuits the UX of docker compose for users that run containers that need access to other containers or networks at build time. What happens, for example, when docker detects that a newer version of the container should be running from
compose upand attempts to build it? Then the build will fail becausecompose upcannot exercise the requisite code path(s), right?I believe so, yes. AFAIK Bake is just driving these same components underneath, and I think all the relevant configuration options as used in the test-case I extended are exposed in compose.yaml; but note that I am not a Compose user so I haven’t tried this, and some of the compose.yaml fields will not be usable as they will be passing parameters todocker buildx buildthat instead need to go todocker buildx create.Edit: Actually no. I tried to get this working, and realised that I don’t see how you specify a compose file that actually starts services, and then runs builds that rely on those services; the
docker compose up --buildcommand wants to build things before starting any services. (Apparently in 2018depends_onaffected building, but that wasn’t intended behaviour)So I’d need to see a working (and ideally simple) example (i.e. from non-buildx) to understand what I’m trying to recreate.
Also, as noted in https://github.com/docker/buildx/issues/175#issuecomment-1875172770, compose doesn’t currently support creating a buildx builder instance, so the builder instance would need to be created first, which means the network must be created first and then referenced in the compose.yaml as
external: true.I also just remembered you specified
docker buildx bakewith a compose.yaml, notdocker compose upwhich I was testing with. I didn’t thinkdocker buildx bakeran services from the compose.yaml, I understood it just parsed out build targets.So yeah, without an example of what you think ought to work, I can’t really advance any compose/bake discussion further.
If you’re just using
docker compose upto bring up the relevant services on the custom network, thendocker buildx baketo do the build against those, then it should work, but you’d still need to either pre-create the custom network and builder before running the tools, or between compose-up and buildx-bake, create the custom builder attached it to the compose-created network.I recrated the same test-script in this style:
and then created test.txt with the desired contents (“hello from custom network”), and:
Note that in the
docker buildx bakecall,--progress plain --no-cache --set=build-only.output.type=outputis only there so you can see the output in the log, so I can rerun the command and see the output each time, and to simulate the shellscript’s-o "type=local,dest=output"to dump the final image into a directory so you can examine the results, respectively. (Weirdly,--set=build-only.output.type=<any string>also had this effect, which I’m pretty sure is a bug/unintended feature.)So that seems to work, yeah. If compose and/or bake were able to define builders to create (and ideally agree on the real name of either the builder or the network in order to avoid hard-coding the network name as I did here) then it would just be a relatively simple
compose up -d && bake && compose down.If that’s what you want, then I’d suggest opening a new feature request for it: I think it makes more sense to be part of
composeso it can be used for compose’s own builds too but SWTMS (See What The Maintainers Say). (Also,docker buildx bakedoesn’t know about compose name-mangling, so if this was part of bake then you’d still need to give the network an explicit name; either way you end up hard-coding thecontainer_namefor the services accessed from the Dockerfile, so local parallelisability is of the bake call only, not the whole stack)(If you’re feeling really clever, you could include a builder as a normal service in the compose spec, and then use the
remotebuildx driver to connect to it, making thedocker buildx createcall more generic; however, I haven’t used theremotebuildx driver so can’t promise that this ends up simpler… it probably ends up more complex as you have to manage TLS and however you reach the custom-network-based builder from your buildx-hosting instance. Thedocker-containerandkubernetesdrivers avoid all this by usingdocker execandkubectl execequivalents.)BuildKit incorrectly replacing localhost DNS resolvers when using host networking is https://github.com/moby/buildkit/issues/3210. There was a PR in progress just over a year ago, but it wasn’t completed. https://github.com/moby/buildkit/issues/2404 seems to have had more recent activity, but looks much wider in scope than https://github.com/moby/buildkit/issues/3210.
If someone has a Hacker News account with street/interweb cred, then perhaps they can post this thread on there… It is often how such situations get attention from companies with loads of money whose sole purpose is supposedly to remedy such situations - rather than cause them.
I am currently hitting this issue, too, with the following setup on my Jenkins. I want to a) spin up a postgres docker image b) build a python library inside a Dockerfile, while running tests against said postgres database with a fixed name.
The issue is that my company wants me to use the docker plugin for Jenkins (https://plugins.jenkins.io/docker-workflow/, see https://docs.cloudbees.com/docs/cloudbees-ci/latest/pipelines/docker-workflow)
The Jenkinsfile code looks similar to this here:
Now, I could rewrite this code to work with buildx as said above, but then I’d need to use basic shell syntax as opposed to the plugin, which will perform clean-up activities in case of failures automatically.
I’ve been seeing similar. You can run the build in a user specified network. But the buildkit container on that network has DNS set to the docker’s localhost entry which won’t get passed through to nested containers. So the RUN steps within the build don’t have that DNS resolution. I’m not sure of the best way to get that to pass through, perhaps a proxy running in the buildkit container that lets DNS get set to the container IP instead of localhost?
I have the same problem as @philomory. Name resolution doesn’t work. I am using
network=cloudbuildon Google Cloud platform, so I can’t hardcode any IP address.Builder has been created with the following command:
docker buildx create --driver docker-container --driver-opt network=cloudbuild --name test --useWhy would that connection be insecure? Forwarding agent is more secure than build secrets because your nodes never get access to your keys.
We have solutions for build secrets, privileged execution modes (where you needed
docker runbefore for more complicated integration tests) and persistent cache for your apt/npm cache etc. https://github.com/moby/buildkit/issues/1337 is implementing sidecar containers support. None of this breaks the portability of the build. And if you really want it, host networking is available for you.No, it does not. You can forward your ssh agent against any node or a cluster of nodes in buildx. Not really different than just using private images.
Which DNS issues do you mean?
There was a long-standing bug with DNS access to the linked services due to BuildKit’s resolv.conf mangling needing an opt-out for host-networking mode, but that was fixed in February and was shipped in BuildKit 0.13. That’s why I was using
image=moby/buildkit:masterin my examples, to test that fix and show that hostname-based lookups “just worked” now, compared to the demonstration of the problem in January, and removing the need for the--add-host echo-server:$(docker inspect echo-server | jq '.[0].NetworkSettings.Networks["echo-server"].IPAddress' | tr -d '"\n')workaround posted in 2022.Edit: Also, if you are looking at my examples, passing
--buildkitd-flags "--allow-insecure-entitlement=network.host"tobuildx createshould not be needed as of Buildx 0.13, it should be automatic whenbuildx buildis passed--network host. But I haven’t tested that, Buildx 0.13 came out after I last tried this.From a distance, your comment from January of this year (https://github.com/docker/buildx/issues/175#issuecomment-1875486526) could be read as a demonstration of the problem, not of a solution.
On a closer read, it does explain my issue. If my priorities were such that the ability to use custom networks in the build process were worth the cruft of putting
docker container inspect test-server --format "{{ (index .NetworkSettings.Networks \"custom-network\").IPAddress }}"or other such incantation in my script, I would do it.Maybe if this pattern gets wrapped into a Docker Compose plugin, that plugin could also do something like add the container’s name and IP to
/etc/hostsand take most of the pain out of the process.In this case the starting point is a DB image. The only reason I can’t apply migrations within the build script is because the DB image and the migration script image (which expects to be able to connect to a DB over the network) are two separate images, and to coordinate between them in the same dockerfile would require inter-process communication between build stages. Hence instantiating the DB on a custom network as an attempted workaround.
However, the migration script image is really just a wrapper around a binary, so I’m realizing my best bet is to copy that binary into my DB image and run it there which cuts out the need for an external network. Good riddance!
It’s not merely difficulty of implementation; if that were the case, someone would have done it by now. I probably would have done it by now if it was merely “hard to implement”.
The driving change is that with the introduction of BuildKit and buildx, the structure of the container build system has shifted to be much more strongly isolated, both from outside influences and particularly from Docker.
The build system is no longer Docker-specific. This has been part of Docker’s direction for years now, as seen by the break-out of moby/moby, OCI standardisation of container formats, etc.
The upshot of that for our purposes is that Docker’s internal overlay network implementation is not visible to BuildKit (
docker buildx build), unless you specifically create a builder instance on that custom network (docker buildx create --driver docker-container --driver-opt "network=my-custom-network"), in the same way if you want to give your builds access to some Kubernetes environment, you create a builder instance using the Kubernetes driver. Even the buildkit instance compiled into Docker doesn’t have full access to the repertoire of Docker features, as described in the last segment of https://github.com/docker/buildx/issues/175#issuecomment-1874749665.So wanting to have your builds run against a custom network is not being rejected as “Possible but unDockerly”. However, to do that in the current state, you actually need to do it, Docker is no longer in a position to make that work magically for you.
So “using buildx containers” is not a work-around, that’s the way to achieve what you want. As it’s the way to achieve anything you want from buildx that isn’t available via the current built-in, limited, (and default) buildkit integration.
There’s definitely space to improve the UX on “using buildx containers”, but it’s still going to be the same rough flow of “To do something that we can’t do from the built-in buildkit builder, create a custom builder that supports what you need, use it, and tear it down when done” with varying levels of automation around it. Docker and
buildx builddirectly probably can’t do much about this, but orchestration tools like buildx bake or docker compose probably have scope to wrap this sort of thing up in their configuration files. (Although right now, neither actually offers to manage services used during build at all, so that’s a larger discussion to be had).Thank you @thaJeztah for that clarifying summary.
I wonder if it’d be worth a feature request against Docker Compose, which seems like it can do most of what the “containerised builder workaround” needs, since people in this thread have tried to do it that way already.
If Docker Compose was able to create/use/remove temporary private builder instances on its own (or the config-specified) network, along with resolving the apparent issue that a standalone BuildKit instance in a Docker container attached to a custom network doesn’t get the right DNS setup for services on that custom network (see https://github.com/docker/buildx/issues/175#issuecomment-1732566254 from earlier attempts to use the “workaround” manually), then I think it can deliver the “workaround” flow fairly naturally for some of the use-cases described here.
I see a similar idea mentioned at https://github.com/docker/compose/pull/10745#issuecomment-1609671858 but I think that was about selecting an existing builder, rather than instantiating a new one. https://github.com/compose-spec/compose-spec/issues/386 is along these lines but it wants to be super-generic; it is probably too generic for this use-case, particularly if we want to tear-down the builder instance after usage or at compose-down time. In this case, the builder is more like another service in Docker Compose that compose knows how to use when building service images. (That also might be a better way to visualise and implement it, similar to existing
depends_onsemantics, and semantics for defining service deployment etc. in the Compose file.)That is separate from the existing
--builderflag fordocker compose buildintroduced in Docker Compose 2.20.0 in July 2023.That said, I’m not a Docker Compose user, so I may be overestimating what it can cover, or misunderstanding the relevant workflow. Either way, unless this is an obviously-faulty idea to Docker Compose users, it’d be better to discuss in a ticket there, to focus on the relevant use-cases that involve Docker Compose, and also focus this ticket on buildx-involved use-cases.
My pipelines are still working fine for some time, but trying to create a new container locally I now notice that
docker buildis not working anymore. Apparentlydocker buildis an alias forbuildx(Whatever that is) but buildx can’t have a simple ‘network’ flag to build with a network? (The recommendation is still to disable the default network on local installs and create a custom bridge network, so this seems quite essential??).So, to get a build going locally, I now have to ‘create a buildx node’ with the network flag, then tell buildx to use that node, then when use ‘buildx build’ instead of just ‘build’, and the first thing it does is load up some buildx-image.
… why? If the build system is being replaced by buildx, at least make it seamless and feature-parity before doing things like this (or make users opt in to the experimental stuff).
Making the
docker buildcommandline compatible by including an option to set the network to use makes it already a lot better, but I’m sure other people have other issues with it reading this thread.this is crazy,
docker composewas used to spin up dependencies e.g. db, redis etc. Butbuild-push-actionnot able to connect to that docker containers. This is critical…I try use:
in
docker/setup-buildx-action@v3and then run
docker/metadata-action@v5and during build it not able to resolve host name fordbandredis.What should I do?
Any updates on this? I have also checked this issue https://github.com/moby/buildkit/issues/978, but can’t find a straight answer. I’ve disabled
buildKitin the Docker Desktop configuration to be able to build my containers, but I’m guessing that is a workaround. Any progress on this would be appreciated.Good point, I should have mentioned I was doing that too for git dependencies, and… Docker themselves have blogged about using it to augment the docker cache. Now I just burn the network, take lots of coffee breaks, and do my bit to melt the ice caps.
Also, it does work in compose while build secrets does not.
@TBBle I was referring to the hostname-based lookup issue. I see now that is fixed. Thank you.
The command I tried was
docker buildx create --name mybuilder --driver-opt network=my-network-nameso I was missing--driver docker-containerand that’s presumably why it didn’t work, and/or the Buildkit image in use was pre-February.https://github.com/docker/buildx/issues/2255 https://github.com/docker/buildx/issues/2256
Should be solved with https://github.com/moby/buildkit/pull/4524
Thank you for the analysis. Do you think the classic builder will be deprecated in the near future within docker, docker compose or buildx?
Thank you @VladimirAndrianov96 - it is a great summary of how they’re wasting our time by removing features they claim we didn’t want/need.
Does anyone know if podman/podman-compose has custom networks during build time? I’m ready to take the leap, despite the unpaid dev hours it’ll take.
I think it’s a lot of different workarounds , for something that could be simple .
With the buildx plug-in (which is in the install docs these days ) the ‘build’ command doesn’t manage to pass the ‘–network’ parameter along to the buildx builder being automatically created.
And since the ‘–network’ parameter exists and tries to do something with buildx it seems like a bug to me, but that’s probably the view from my limited bubble.
On Wed, Dec 20, 2023, 19:57 Vinícius Gajo @.***> wrote:
I was able to reach the containers using
--network "host"but this is not good enough, since some people that would run this command are not using Linux, and this flag does not work in other major OS’ like Mac and Windows (even with WSL).That’s not nearly enough information to get help, I fear. Is this repository public? Then we could actually see what you’ve tried to do, and possibly work out what’s gone wrong.
Edit: Looking back, e.g. this comment and the preceding few suggests that DNS resolution during building specifically may not be correctly honouring the custom network.
That’s already been specifically reported at https://github.com/docker/buildx/issues/1347, but turns out to be a BuildKit issue, rather than buildx, so the real issue to track (AFAICT) is https://github.com/moby/buildkit/issues/3210 or https://github.com/moby/buildkit/issues/2404. Probably both, at an initial glance. I suspect the former is a duplicate of the latter, since when using a custom network for the docker-container driver, from the perspective of the BuildKit running in that container, that custom network is the host network.
You might be able to use the workaround shown in https://github.com/docker/buildx/issues/175#issuecomment-1099567863 with add-hosts, but possibly you’ll need a separate build-step to extract the IPs from the custom network, as (I hope) you can’t smuggle arbitrary subshell executions into
build-push-action…Right, that’s why I opened this a couple weeks ago: https://github.com/docker/buildx/issues/1761
I haven’t been able to, but I need to put more effort into it. It just feels like too many nested workarounds at this point to set myself up to depend on. What stopped me from
docker buildx buildwas that I couldn’t get it to find the (first layer of) images that were already created, whichbuildx buildextends viaFROMin this service’s Dockerfile. It would just try to download them fromdocker.io. It also wouldn’t read the${project}env var that has always worked in the Dockerfile.But then there’s a bunch of compose-specific build stuff you don’t have access to…
Makes no sense. Also, provably false; it used to work and therefore was already supported. Who exactly is “we”?
Is there some version of docker-compose v1 where DOCKER_BUILDKIT=1 somehow does not result in buildkit getting use? Because docker-compose build on Docker Compose version 2.2.3 produces output that looks like the clssic builder.
You don’t pass the custom network name with build commands. Your builder instance is already part of that network.
@bryanhuntesl The proxy vars are still supported. For this use case, cache mounts might be a better solution now https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/syntax.md#run---mounttypecache
The sidecar also sounds great and very clever and well structured. But again, 3 years ago I could build with secrets and talk to network services to run integration tests.