0

I am building and running this docker container. It is running a simple flask server. But when I run, it exited right after.

This is my Dockerfile

FROM python:3

WORKDIR /usr/src/app

COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

EXPOSE 8080

# CMD ["python3", "-m",  "http.server", "8080"]
CMD ["python3", "./py_server.py"]

and this is py_server.py

from flask import Flask
app = Flask(__name__)

PORT = 8080

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

if __name__ == '__main__':
   app.run(PORT)

this is how I build and run the container respectively.

build:

docker build -t banuka/python-venv .

run:

docker run -dit -p 8080:8080 --name server1 banuka/python-venv:latest

Can someone tell me what I do wrong?

deceze
  • 510,633
  • 85
  • 743
  • 889
Jananath Banuka
  • 2,951
  • 8
  • 57
  • 105

3 Answers3

2

There are several issues:

  1. You want the -it parameter, not -dit:
docker run -it -p 8080:8080 --name server1 banuka/python-venv:latest
  1. You are passing PORT as a variable to the app.run() function, so that it is interpreted as the first host parameter, rather than what you want, which is for it to be the port parameter. What you want is this: app.run(port=8080)

  2. As @Alexandre pointed out, if you're accessing the host remotely, then you need to explicitly bind it to host='0.0.0.0', so we need app.run(host='0.0.0.0',port=8080)

Peter Prescott
  • 768
  • 8
  • 16
1

You have a bug in your Flask Code. You're trying to configure Flask Server PORT in a wrong way. This will throw the error you're experiencing:

AttributeError: 'int' object has no attribute 'startswith'

You should configure your Flask Server Port with the following way

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',port=8080)

The documentation: https://flask.palletsprojects.com/en/1.1.x/api/#flask.Flask.run

EDIT: Added host='0.0.0.0' so you can access your Flask Server remotely.

Alexandre Juma
  • 3,128
  • 1
  • 20
  • 46
  • I fixed this, and now the problem is I can't access it. now the container is running. But now it doesn't print the hello world. When I see the logs it says `Running on http://127.0.0.1:8080/` – Jananath Banuka Apr 13 '20 at 09:55
  • Are you running your docker locally in your machine? If so , just access from your browser http://127.0.0.1:8080. If you're hosting it remotely, you'll not be able to access it because you're binding it to localhost – Alexandre Juma Apr 13 '20 at 09:58
  • Remotely. But I have a public IP assigned to the host I am running the container and have allowed all the network traffic in the security group – Jananath Banuka Apr 13 '20 at 10:00
  • Look at my updated code to bind Flask to public IP. You need to add host='0.0.0.0' to app.run(), otherwise it will only bind to localhost. – Alexandre Juma Apr 13 '20 at 10:00
  • app.run(host='0.0.0.0',port=8080) – Alexandre Juma Apr 13 '20 at 10:02
  • Thank you. now this is working. One little question. How many times do I have to expose this port? I have exposed this in `Dockerfile` and even in the docker run command. Where only necessary? – Jananath Banuka Apr 13 '20 at 10:04
  • You need to have Flask process, which is running inside a container, listening to a specific port (this is defined in the app.run() call). The Dockerfile EXPOSE is a good practice even if you're exposing the ports with -p because it acts as documentation of what ports your container expose. Then you need to tell docker run to map an internal container port to an external host port (-p 8080:8080). If you're confused with so much port definition, take a look at this [article](https://nickjanetakis.com/blog/docker-tip-59-difference-between-exposing-and-publishing-ports) – Alexandre Juma Apr 13 '20 at 10:12
0

you are maybe running procces that finishes and then exit ? if you run you py script and it finishes so your container will be closed to ... try using while(true) //and then your code

OrDushi
  • 86
  • 6