


Why Can't I Access My Flask App After Deploying it in Docker?
Deploying Flask App in Docker: Resolving Server Connection Issues
When deploying Flask applications in Docker, it's possible to encounter server connectivity issues despite the container appearing to be running. This article will investigate a common problem and provide a solution to ensure the application is accessible from outside the container.
Problem Description
Consider an app named "perfektimprezy" that runs on Flask, with the following source:
from flask import Flask app = Flask(__name__) app.debug = True @app.route('/') def main(): return 'hi' if __name__ == '__main__': app.run()
When deployed in a Docker container, the server seems to be running, but the application remains inaccessible from outside the container.
Docker Configuration
The Dockerfile used for the deployment is:
# Dockerfile FROM dreen/flask MAINTAINER dreen WORKDIR /srv # Get source RUN mkdir -p /srv COPY perfektimprezy.tar.gz /srv/perfektimprezy.tar.gz RUN tar x -f perfektimprezy.tar.gz RUN rm perfektimprezy.tar.gz # Run server EXPOSE 5000 CMD ["python", "index.py"]
The deployment steps involve building the image and running the container with port 5000 exposed:
>$ sudo docker build -t perfektimprezy . >$ sudo docker run -i -p 5000:5000 -d perfektimprezy
Investigation
The container appears to be running as expected, with the Flask server listening on port 5000 within the container:
>$ sudo docker logs 1c50b67d45b1 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) * Restarting with stat
However, requests to the application from outside the container result in empty replies:
>$ curl 127.0.0.1:5000 -v * Empty reply from server
Solution
The issue lies in the binding of the Flask app to the localhost interface. To make the application accessible from outside the container, it should bind to the 0.0.0.0 address instead.
In the Flask app initialization, change:
if __name__ == '__main__': app.run()
to:
if __name__ == '__main__': app.run(host='0.0.0.0')
This modification will bind the app to all interfaces on the host, making it accessible from outside the container.
The above is the detailed content of Why Can't I Access My Flask App After Deploying it in Docker?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

Regular expressions are powerful tools for pattern matching and text manipulation in programming, enhancing efficiency in text processing across various applications.

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...
