Captain_Lee
Captain_Lee

Reputation: 1

I can't open http://127.0.0.1:5000 when I run my Flask project with Docker

I implemented a wordle game based on Flask and MySQL: https://github.com/Goer17/wordle-flask

When I run my program with Docker, the console shows that:

web-1  |  * Serving Flask app 'app.py'
web-1  |  * Debug mode: off
web-1  | WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
web-1  |  * Running on http://127.0.0.1:5000

But I find it that I can't open the link as my browser trigger a 403 error.

The following are my docker-compose.yaml and Dockerfile:

version: '3.8'
services:
  web:
    build: .
    ports:
      - "5001:5000"
    depends_on:
      - db
    environment:
      FLASK_APP: app.py
      FLASK_ENV: development
  db:
    image: mysql:innovation-oracle
    environment:
      MYSQL_DATABASE: wordle
      # MYSQL_USER: user
      # MYSQL_PASSWORD: password
      MYSQL_ROOT_PASSWORD: password
    ports:
      - "3306:3306"
FROM python:3.8-slim

WORKDIR /app

COPY . /app

RUN apt-get update && apt-get install -y netcat-traditional && rm -rf /var/lib/apt/lists/*
RUN pip install --no-cache-dir -r requirements.txt
RUN flask db init

COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

ENV FLASK_APP=app.py

EXPOSE 5000

ENTRYPOINT ["/entrypoint.sh"]

You can see more information in my repo.

I attempted to run my program directly without using Docker, and it successfully executed.

Upvotes: 0

Views: 2129

Answers (1)

datawookie
datawookie

Reputation: 6564

Assuming that your project looks something like this:

├── app.py
├── docker-compose.yml
├── Dockerfile
├── entrypoint.sh
└── requirements.txt

The requirements.txt must specify Flask.

🗎 Dockerfile (I have commented out flask db init because I don't know what you have implemented there.)

FROM python:3.8-slim

WORKDIR /app

COPY requirements.txt /app

RUN apt-get update && apt-get install -y netcat-traditional && rm -rf /var/lib/apt/lists/*
RUN pip install --no-cache-dir -r requirements.txt

COPY . /app

# RUN flask db init

COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

ENV FLASK_APP=app.py

EXPOSE 5000

ENTRYPOINT ["/entrypoint.sh"]

🗎 docker-compose.yml

version: '3.8'
services:
  web:
    build: .
    ports:
      - "5001:5000"
    depends_on:
      - db
    environment:
      FLASK_APP: app.py
      FLASK_ENV: development
  db:
    image: mysql:innovation-oracle
    environment:
      MYSQL_DATABASE: wordle
      MYSQL_ROOT_PASSWORD: password
    ports:
      - "3306:3306"

🗎 entrypoint.sh

#!/bin/bash

flask run --host=0.0.0.0 --port=5000

🗎 app.y (A test application.)

from flask import Flask

app = Flask(__name__)

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

In your Docker Compose configuration you are mapping port 5000 on the container to port 5001 on the host. So to connect to the app you need to browse to http://127.0.0.1:5001.

enter image description here

Upvotes: 0

Related Questions