3

I have a service running on http://localhost:8123 that I'd like to reach from inside my docker container.

This is the Dockerfile I used for creating the image

FROM python:3.9-alpine

WORKDIR /usr/local/bin/code
COPY . /usr/local/bin/code

RUN python -m pip install --upgrade pip
RUN pip install pipenv
RUN pipenv install --system --deploy

CMD ["/bin/sh"]

The service is listening on port 8123 on the host, and the container needs to send HTTP requests to there, how would that be done?

Thanks!

orie
  • 541
  • 6
  • 20
  • Remember that `localhost` is very context specific: each individual Docker container separately believes it is `localhost`, as do individual instances of Stack Overflow's HTTP tier, my local system, _etc._; _every_ service is "running on localhost". I'm pretty sure you're asking about something running outside a container on the same system and have linked to the canonical question about that. – David Maze Jan 30 '22 at 11:42

1 Answers1

1

If you want to send requests from a container to your host, it requires that the container runs in the host network. You can run your container with --net=host option:

docker run -it --net=host <my_image>

From the container, you'll now be able to send requests to your host (try to run something like curl http://localhost:8123).

norbjd
  • 10,166
  • 4
  • 45
  • 80
  • Of the various ways to do this, this is probably the one I'd recommend least. It generally disables Docker networking (and features like port remapping and Docker-internal host names for containers) and it doesn't work on MacOS. – David Maze Jan 30 '22 at 11:40