简体   繁体   中英

Unable access Dockerized Flask app at http://localhost:5000

I'm trying to build and deploy a flask app using docker. When I navigate to localhost:5000 I get a page stuck on loading.

I'm building and running the containers with

docker-compose up

Containers seem to be running fine

Attaching to db_1, flaskapp_1
db_1        |
db_1        | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1        |
db_1        | 2021-05-18 16:51:31.624 UTC [1] LOG:  starting PostgreSQL 13.3 (Debian 13.3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1        | 2021-05-18 16:51:31.624 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1        | 2021-05-18 16:51:31.624 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1        | 2021-05-18 16:51:31.757 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1        | 2021-05-18 16:51:31.927 UTC [26] LOG:  database system was shut down at 2021-05-18 16:50:46 UTC
db_1        | 2021-05-18 16:51:31.976 UTC [1] LOG:  database system is ready to accept connections
flaskapp_1  |  * Serving Flask app '/usr/src/app/flaskapp/__init__.py' (lazy loading)
flaskapp_1  |  * Environment: development
flaskapp_1  |  * Debug mode: on
flaskapp_1  | /usr/local/lib/python3.9/site-packages/flask_sqlalchemy/__init__.py:872: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppress this warning.
flaskapp_1  |   warnings.warn(FSADeprecationWarning(
flaskapp_1  |  * Running on all addresses.
flaskapp_1  |    WARNING: This is a development server. Do not use it in a production deployment.
flaskapp_1  |  * Running on http://192.168.16.3:5000/ (Press CTRL+C to quit)
flaskapp_1  |  * Restarting with stat
flaskapp_1  |  * Debugger is active!
flaskapp_1  |  * Debugger PIN: 132-792-693
flaskapp_1  | /usr/local/lib/python3.9/site-packages/flask_sqlalchemy/__init__.py:872: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppress this warning.
flaskapp_1  |   warnings.warn(FSADeprecationWarning(
64051148fde3   52151bbc0161   "flask run --host=0.…"   8 minutes ago   Up 8 minutes   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   flaskapp_flaskapp_1
b368ac60098a   293e4ed402ba   "docker-entrypoint.s…"   8 minutes ago   Up 8 minutes   0.0.0.0:5432->5432/tcp, :::5432->5432/tcp   flaskapp_db_1

I've tried these solutions but they didn't work.

app.run(debug=False, host='0.0.0.0')

CMD ["flask", "run", "--host=0.0.0.0"]

Below is my Dockerfile

# For more information, please refer to https://aka.ms/vscode-docker-python
FROM python

EXPOSE 5000

# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1
# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1

WORKDIR /usr/src/app/
COPY . /usr/src/app/
ENV FLASK_APP=/usr/src/app/__init__.py

# Install pip requirements
COPY requirements.txt /usr/src/app/requirements.txt
RUN pip install --upgrade pip
RUN pip install -r requirements.txt

# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /usr/src/app/
USER appuser

# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
# CMD ["gunicorn", "--bind", "0.0.0.0:5000", "run:app"]

# ENTRYPOINT ["/usr/src/app/entrypoint.sh"]
# CMD ["flask", "run", "--host", "0.0.0.0", "-p", "5000"]
CMD ["flask", "run", "--host=0.0.0.0"]
# ENTRYPOINT ["python", "run.py"]

run.py

from flaskapp import app, db
from flask.cli import FlaskGroup


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

docker-compose.yml

version: '3.4'

services:
  flaskapp:
    image: flaskapp
    build: .
    env_file: .env.dev
    depends_on:
      - db
    ports:
      - 5000:5000
    volumes:
      - ./flaskapp:/usr/src/app/app
  
  db:
    image: postgres:latest
    networks:
      - default
    ports:
      - 5432:5432
    restart: always
    volumes:
      - ./postgres-data:/var/lib/postgresql/data
    env_file: .env.dev

volumes:
  postgres_data:

config.py

import os

basedir = os.path.abspath(os.path.dirname(__file__))


class Config(object):
    user = os.environ['POSTGRES_USER']
    password = os.environ['POSTGRES_PASSWORD']
    host = os.environ['POSTGRES_HOST']
    database = os.environ['POSTGRES_DB']
    port = os.environ['POSTGRES_PORT']

    SQLALCHEMY_DATABASE_URI = f'postgresql+psycopg2://{user}:{password}@{host}:{port}/{database}'
    SECRET_KEY = '2a0f51a5c1992df84ee9bbd9817492d5'

.env.dev

FLASK_APP=/usr/src/app/flaskapp/__init__.py
FLASK_ENV=development

POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_HOST=db
POSTGRES_PORT=5432
POSTGRES_DB=postgres

APP_FOLDER=/usr/src/app

full source code HERE

What am I doing Wrong?

Turns out I was just starting the existing images of the containers by running

docker-compose up

So all I had to do was to build new images and then run those images.

docker-compose up --build

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM