Reputation: 161
I have a web service in docker container running and accessible at localhost:5000 (host machine). I would like to call this service in Dockerfile. I am using a multi-stage Dockerfile for .net core app. I would like to collect build stats and send to service running at localhost:5000. So far, suggested approach was to use --network="host". But this doesn't work during build time.
Upvotes: 2
Views: 5015
Reputation: 18065
How strange! --network=host
should achieve the desired effect, in line with the suggested approach that you mention. I wonder why this doesn't work for you out of the box.
I suggest you test this simplified version of the problem to eliminate potential sources of error outside the Docker networking stack:
docker run -d -p 5000:80 nginx
will do the trick
FROM busybox
RUN echo localhost:5000
RUN wget localhost:5000
docker build --no-cache .
. As expected, the network call will fail and the output should include:
wget: can't connect to remote host (127.0.0.1): Connection refused
docker build --network=host --no-cache .
. For me, the output includes:
Connecting to localhost:5000 (127.0.0.1:5000)
index.html 100% |********************************| 612 0:00:00 ETA
docker build --network=bridge --no-cache .
. Again, the network call fails with:
wget: can't connect to remote host (127.0.0.1): Connection refused
What do you get when you try this?
Important note in the example above: I include the --no-cache
parameter in the docker build
commands to ensure the entire Dockerfile
is rebuilt. Otherwise docker will just pull the built image layers from cache and ignore any changes you made to the --network
parameter since you last built this Dockerfile.
More importantly, though the network
option is exposed at image build time (as of a recent API version), I'm not sure this use would be consistent with best practices. You may have better luck with a solution that transmits build stats outside the actual Dockerfile. For example, a CI script that runs docker build ...
could collect the output from that command, grep
/sed
it for specific stats, and transmit those to your web service.
Edit: how to achieve this with docker-compose, per your comment. Sadly docker-compose docs don't advertise this, but --network
is actually supported. It was added in Compose YAML file format version 3.4, specifically in this PR.
Here's an example docker-compose.yml
:
version: '3.4'
services:
web:
build:
context: .
network: host
Put that in the same directory as the example Dockerfile
from above, and make sure nginx in a separate container as demonstrated above.
Here's the result:
> docker-compose build --no-cache
Building web
Step 1/3 : FROM busybox
---> e1ddd7948a1c
Step 2/3 : RUN echo localhost:5000
---> Running in d5f0d712c188
localhost:5000
Removing intermediate container d5f0d712c188
---> 8aa9d974447f
Step 3/3 : RUN wget localhost:5000
---> Running in a10cee732e48
Connecting to localhost:5000 (127.0.0.1:5000)
index.html 100% |********************************| 612 0:00:00 ETA
Removing intermediate container a10cee732e48
---> 8a287d116b4b
Successfully built 8a287d116b4b
Successfully tagged dockerquestion_web:latest
Upvotes: 3