0

HelloWorld-1.py

app = Flask(__name__)
@app.route('/')
def printHelloWorld():
    print("+++++++++++++++++++++")
    print("+ HELLO WORLD-1 +")
    print("+++++++++++++++++++++")
    return '<h1>Bishwajit</h1>'
    # return '<h1>Hello %s!<h1>' %name

if name == '__main__':
    app.run(debug='true')

Dockerfile

FROM python:3
ADD HelloWorld-1.py /HelloWorld-1.py
RUN pip install flask
EXPOSE 80
CMD [ "python", "/HelloWorld-1.py"]

Building docker using the below command

docker build -t helloworld .

Running docker image using below command

docker run -d --name helloworld -p 80:80 helloworld

when i run the below command

docker ps -a

i get the below output

CONTAINER ID  IMAGE      COMMAND               CREATED             STATUS             PORTS               NAMES

cebfe8a22493 helloworld "python /home/HelloW…" 2 minutes ago Up 2 minutes (unhealthy) 0.0.0.0:80->80/tcp helloworld

If I hit in the browser(127.0.0.1:5000), it does not give response, But when i run the python file individually, it runs properly in the browser.

4
  • you are trying to access by using 127.0.0.1:5000 so shouldn't the -p argument be as -p 5000:80? Commented May 30, 2021 at 6:16
  • i tried changing the port also, it did not work. Is my docker file correct? Commented May 30, 2021 at 6:32
  • i tried with that too.. In Dockerfile....it is there EXPOSE 80, should it be changed. Commented May 30, 2021 at 7:26
  • @programandoconro Finally it worked. I changed few things like ``` app.run(debug='true', port='5000', host='0.0.0.0') ``` ``` docker run -p 5000:5000 helloworld ``` Commented May 30, 2021 at 17:46

1 Answer 1

1

I reproduced your problem and there were four main problems:

  1. Not importing flask.
  2. Using name instead of __name__
  3. Not assigning the correct port.
  4. Not assigning the host.

This is how your HelloWorld-1.py should look like:

from flask import Flask
app = Flask(__name__)
@app.route('/')
def printHelloWorld():
    print("+++++++++++++++++++++")
    print("+ HELLO WORLD-1 +")
    print("+++++++++++++++++++++")
    return '<h1>Bishwajit</h1>'
    # return '<h1>Hello %s!<h1>' %name

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

This is how you Dockerfile should look like:

FROM python:3
ADD HelloWorld-1.py .
RUN pip install flask
CMD [ "python", "/HelloWorld-1.py"]

Then simply build and run:

docker build . -t helloflask
docker run -dit -p 5000:5000 helloflask

Now go to localhost:5000 and it should work.

Additionally: You could actually assign any other port, for example 4444, and then go to localhost:4444:

docker run -dit -p 4444:5000 helloflask
Sign up to request clarification or add additional context in comments.

2 Comments

In Dockerfile....it is there EXPOSE 80, should it be changed.
Yes. correct .. If at all i want to expose a port, it need to first give the port, and expose it in Dockerfile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.