How to handle networking between containers at build time

How do you handle the situation where you have two containers A and B. Container B needs to make a request to A when image B is being built.

I understand that by adding both A and B to the same network, when both are running I can use the container name of A to make a request to it from B. But there doesn’t seem to be utilize the network A is on when image B is building

I have found that not adding either A or B to a network, then using the IP address of A to make a request to it from B, it will work fine at both build time and run time of B. However, there is another container C that needs to be networked with B, but adding B to a network causes issues with the request to A using A’s IP address.

Two questions I have are:

Can anyone explain why adding B to a network causes issues with making a request to A using it’s IP address? And is it possible for B to make requests to A while being on different networks?

Or is it possible to use the a network that A is on, when the image of B is being built so that I can use the container name of A to make the request?

AFAIK a Docker build process is not attaching to any Docker network.

So if you want to access data from another running service/container during build, you would need to expose a port or use a reverse proxy in front of the target service.

The other option would be to run the build inside a container, that is attached to the Docker network.

Actually the containers used during build are connected to the default bridge, unless --network is specified to use a different network.

The compose specs describe how it’s configured in a compose file: https://docs.docker.com/compose/compose-file/build/#network

1 Like

The --network parameter of docker build image actually does not refer to a network, but to the networking mode for the RUN instructions during build.
The Docker semantics is not consistent in this case (and it sucks), since in docker run , the --network connects a container to a network (it should be called --networking-mode in docker build image ).

Related to: Is "docker build --network=host" working as expected? - #3 by nickyayoub

1 Like

Good catch!

One of the examples in the compose specification shows that it allows to configure the network freely. Either it’s a docu-bug, or another inconsistency.

1 Like

Could you please provide the link? Since this parameter is a footgun, I think it should be properly documented and I’d like to report it. I found compose docs regarding networks, but these are container networks (which accept any network name), not the build ones.

It was already in my last post from December 2023:

Update:
After stumbling across this github issue: DOCKER_BUILDKIT=1 prevents custom networks using docker build --network · Issue #978 · moby/buildkit · GitHub, It appears that it’s a docu-bug and needs updating to reflect the changed behavior of BuildKit.

1 Like

Sorry! And thanks. I created this issue: docker image build --network refers to netwoking type · Issue #21982 · docker/docs · GitHub

1 Like