Making requests from a docker container a service running on localhost

Viewed 568

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!

1 Answers

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

Related