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.
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 ).
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.
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.