-1

I have made a basic temperature converter web app using python Flask and when i am running in using the command `python3 main.py Then it is runnning fine. But after building dockerfile and running in container it is not running dockerfile is build completely with no error and also the port mapping is correctly done

My flask app is running properly in the local host but when i try running in docker container it is not able to run in the mapped port

Here is the python code in main.py file

from flask import Flask

from flask import request


app = Flask(__name__)


@app.route("/")

def index():

    celsius = request.args.get("celsius", "")

    if celsius:

        fahrenheit = fahrenheit_from(celsius)

    else:

        fahrenheit = ""

    return (

        """<form action="" method="get">

                Celsius temperature: <input type="text" name="celsius">

                <input type="submit" value="Convert to Fahrenheit">

            </form>"""

        + "Fahrenheit: "

        + fahrenheit

    )


def fahrenheit_from(celsius):

    """Convert Celsius to Fahrenheit degrees."""

    try:

        fahrenheit = float(celsius) * 9 / 5 + 32

        fahrenheit = round(fahrenheit, 3)  # Round to three decimal places

        return str(fahrenheit)

    except ValueError:

        return "invalid input"

if __name__ == "__main__":
    app.run(host="127.0.0.1", port=3000, debug=True)
`

Here is the DockerFile code

enter image description here

Goku
  • 59
  • 1
  • 8
  • It's unclear from your question but the `Dockerfile` `EXPOSE` command is for documentation only. It does not change ports exposed by the container. If you're using the default Flask port (`5000`) and you're confident Flask is binding to `0.0.0.0`, then you will want to `docker run .... --publish=5000:5000/tcp ....` to ensure Docker exposes the container's port `5000` on your host's port `5000`. If you're running on Window or a Mac this may not be true but, on Linux, you can then access the app on the host as `localhost:5000` – DazWilkin Jan 06 '23 at 05:21
  • Please don't use images in questions when it's straightforward to copy-and-paste the text. Images may not live as long as the question and they inhibit others' ability to copy-and-paste text to help answer your questions. – DazWilkin Jan 06 '23 at 05:23
  • Do you see the port open with command 'ss -ntl'? – Juan Botero Jan 06 '23 at 05:26

1 Answers1

0

It's because you use 127.0.0.1 instead of 0.0.0.0. So by changing this line in your Python code it will work. See "Externally Visible Server" in the Flask documentation

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=3000, debug=True)

See also this answer for detail.

Note on EXPOSE

You need to expose the port when you run the container, using EXPOSE in the Dockerfile does not do that.

The EXPOSE instruction does not actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published. source

You need to publish / expose it when you run the container. See reference here.

docker run -p 3000:3000 ...
Romain
  • 19,910
  • 6
  • 56
  • 65