Ravi
Ravi

Reputation: 3303

Exposing Flask App within Docker to Internet

I have a Python Flask App running inside a Docker container in the default port 500. This is on a Ubuntu server.

I am able to access the Flask App through local host as follows:

URL = "http://127.0.0.1:5000/get_image"

Would like to know how to expose this server to outside world. I would like to use the server IP to expose the service so that others can access it as well.

Can some one guide on how to expose this docket to internet? Thank you

Upvotes: 2

Views: 3194

Answers (1)

bigbounty
bigbounty

Reputation: 17358

You should first put "0.0.0.0" as host for flask. Next you need to expose your docker port.

Flask file:

from flask import Flask
from flask_restful import Resource, Api

app = Flask(__name__)
api = Api(app)

class HelloWorld(Resource):
    def get(self):
        return {'hello': 'world'}

api.add_resource(HelloWorld, '/get_image')

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

Dockerfile

FROM python:3.7
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
ENTRYPOINT ["python"]
CMD ["app.py"]

Build the docker image

docker build -t docker_flask:latest .

Run the image

docker run -d -p 5000:5000 docker_flask:latest

After checking everything is running fine in local, push the image to docker registry using docker push command. Then deploy the image on kubernetes or VM

Upvotes: 7

Related Questions