skip to Main Content

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


  1. While running the application with docker run, you can pass an option called --network host. This will run your application on 127.0.0.1

    You 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

    docker ps 
    // to list all the docker containers
    
    docker inspect <container_id> 
    // you will get container_id from the output of 1st command
    

    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 the docker run command

    say, your python app is running on 8000
    you can pass -p 7000:8000 and now your app will open on localhost:7000

    Login or Signup to reply.
  2. Here is what worked for me to find IP address of the container:

    docker inspect -f ‘{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}’ your_container_id

    Login or Signup to reply.
  3. docker exec <container_id> sh -c "hostname --ip-address"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search