简体   繁体   中英

How to access python flask application in windows when running it in a linux container?

I am working on a microservice written in python. My microservice works fine on my windows machine and I can easily test it. However, on my Linux container it may work fine too but I cannot test it. Even when optimizing my code for network access as explained here .

So here is my microservice:

from flask import Flask

app = Flask(__name__)


@app.route('/')
def hello_world():
    return 'Hello World!'


if __name__ == '__main__':
    app.run(host='0.0.0.0')

- I tested this by running on windows by typing in the command prompt:

python.exe app.py

- I got a message ending with

Running on http://0.0.0.0:5000/ (Press CTRL C to quit).

To test my microservice, I executed my browser to access: http://localhost:5000/

I can see the "Hello World" message. So my application seems to work perfectly.

After that, I started working with docker. Here is my Dockerfile (named "Dockerfile").

FROM python
RUN apt-get update -y
RUN apt-get install -y python-pip python-dev build-essential
RUN pip install Flask
COPY . /app
WORKDIR /app
ENTRYPOINT ["python3"]
CMD ["app.py"]

Here is my build command:

docker build -t docktoflask .

Which shows some output considering the build process.

Then I started running the container with this command:

docker run -p 5002:5002 docktoflask

I got the same ending message as I got on windows (which is remarkable because I specified port 5002).

After that I tried testing with this link: http://localhost:5002/

(of course I also tried http://localhost:5000/ )

No success.... (which means a browser error instead the hello world message). The error is in Dutch (my browser language) but means the same as this .

This is annoying because it seems to work and the port is really active. There are two applications, I should be able to access from my browser (portainer and my own application). I can access portainer without any problems. How can I access my microservice, when running it in a docker container?

我正在运行的容器

The command

docker run -p 5002:5002 docktoflask

means that the internal container port 5002 will be exposed as host port 5002 (localhost:5002), even if the internal container port 5002 is not opened yet.

You have to change this to

docker run -p 5000:5000 docktoflask

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM