1

Hi I have made my flask app and I have exposed port 5001 in Docker file. I pushed it to dockerhub repo and ran on different machine by

docker container run --name XYZ <username>/<repo_name>:<tag>

The log says that app is running on http://127.0.0.1:5001/

But if I open that localtion in browser its says

Unable to connect

Dockerfile:

FROM ubuntu:18.04

RUN apt-get update && apt-get -y upgrade \
    && apt-get -y install python3.8 \
    && apt -y install python3-pip \
    && pip3 install --upgrade pip

WORKDIR /app

COPY . /app

RUN pip3 --no-cache-dir install -r requirements.txt 

EXPOSE 5001

ENTRYPOINT  ["python3"]

CMD ["app.py"]
Hitesh Somani
  • 620
  • 4
  • 11
  • 16
  • 1
    The details are a little sparse here, but if you want to expose 5001, you'll probably need the -p command. i.e. -p 5001:5001 – Jesse Q Aug 01 '20 at 05:10
  • host port has to be opened – Visakh Vijayan Aug 01 '20 at 05:34
  • 1
    Try running flask with `--host=0.0.0.0` or `app.run(host= '0.0.0.0')`. Also check that the container is active/healthy using `docker ps`. – Daniser Aug 01 '20 at 06:04
  • 2
    If it says `Running on http://127.0.0.1:5000` that sounds very similar to [Deploying a minimal flask app in docker - server connection issues](https://stackoverflow.com/questions/30323224/deploying-a-minimal-flask-app-in-docker-server-connection-issues); you both need the `host=0.0.0.0` option @Daniser suggests and also the `-p` option from @JesseQ. – David Maze Aug 01 '20 at 11:03
  • @Daniser, What does `0.0.0.0`. Does Host ip: `0.0.0.0` means anyone on internet can use my flask app ? – Hitesh Somani Aug 06 '20 at 04:39
  • @DavidMaze, same question, What does 0.0.0.0. Does Host ip: 0.0.0.0 means anyone on internet can use my flask app ? – Hitesh Somani Aug 06 '20 at 06:06
  • 1
    In order for anyone on the internet to see your app you will have to configure your home router to support this using port forwarding (the app will have your **public IP**). But setting the host IP to `0.0.0.0` will allow anyone in your LAN (e.g: connected to your router at home) to see your app by connecting to the **private IP** of your computer (private IP inside the LAN). – Daniser Aug 06 '20 at 07:03
  • Thanks @DavidMaze and @Daniser `host=0.0.0.0` worked for me. – Hitesh Somani Aug 07 '20 at 16:46

2 Answers2

0

You have to expose the port. Try this:

docker run container -p 5001:5001 --network host --name XYZ <username>/<repo_name>:<tag>
0

You need to do the port mapping for your container then only port will exposed outisde

docker container run --name XYZ -p 5001:5001
<username>/<repo_name>:<tag>

And then if it's Docker hub windows then you can directly hit with above local host url else you need to docker machine ip address and hit with that ip like below command docker-machine ip

Pandit Biradar
  • 1,777
  • 3
  • 20
  • 35