I have set up a fast-api docker container. I need to communicate with fast-api present in my docker container running locally through http requests, however I am not able to determine the IP address in which my fast-api docker container is running. My dockerfile is:
FROM jhonatans01/python-dlib-opencv
COPY . .
RUN pip3 install -r requirements.txt
CMD ["uvicorn", "main:app", "--reload"]
When I run fast-api locally by,
uvicorn main:app --reload
the terminal tells me where the instance is running. However docker does not provide any output. I have looked at http://192.168.99.100 and http://127.0.0.1 with no success.
3
Answers
While running the application with
docker run
, you can pass an option called--network host
. This will run your application on 127.0.0.1You will not need the container IP to access your application
But, if you want to know the container IP in any case
you can do
This will give you a json including all information (and of course the IP)
Another solution :
As Kiaus’s comment suggests ..
You can do port binding, means you have to pass option
-p <port1>:<port2>
in thedocker run
commandsay, your python app is running on
8000
you can pass
-p 7000:8000
and now your app will open onlocalhost:7000
Here is what worked for me to find IP address of the container:
docker inspect -f ‘{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}’ your_container_id