vercingortix
vercingortix

Reputation: 249

Flask and Frontend with Docker Compose

I'm trying to get a basic Flask backend, and frontend framework in separate containers communicating with each other via docker-compose.

Caveat here is that I'm using Windows 10 Home so I need to be using Docker Toolbox so I've had to add a few networking rules for port forwarding. However, I can't seem to access http://localhost:5000 for my backend. I get ECONNREFUSED. I'm just trying to get basic communication between the frontend and backend communications to simulate frontend/api communication.

Given my port forwarding rules, I can access http://localhost:8080 and I can view the static portions of the app. However, I can't access the backend or can I tell if it they are communicating. New to both Flask and Docker so please forgive my ignorance. Coming from a .NET background, Windows seems to really make this a pain. Thank you for your help.

Here is my project structure:

Project Structure

Here is my application.py:

# Start with a basic flask app webpage.
from flask_socketio import SocketIO, emit
from flask import Flask, render_template, url_for, copy_current_request_context

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
app.config['DEBUG'] = True

#turn the flask app into a socketio app
socketio = SocketIO(app)

@app.route('/')
def index():
    #only by sending this page first will the client be connected to the socketio instance
    return render_template('index.html')

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

Dockerfile for the backend:

FROM python:2.7

ADD ./requirements.txt /backend/requirements.txt
WORKDIR /backend

RUN pip install -r requirements.txt

ADD . /backend

ENTRYPOINT ["python"]

CMD ["/backend/application.py"]

EXPOSE 5000

Dockerfile for frontend:

FROM node:latest

COPY . /src
WORKDIR /src

RUN npm install --loglevel warn
RUN npm run production

EXPOSE 8080

CMD [ "node", "server.js" ]

And my docker-compose.yml:

version: '2'
services:
  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    restart: always
    ports:
     - "5000:5000"
    env_file:
      - .env

  frontend:
    build: ./frontend
    ports:
      - "8080:8080"

Upvotes: 1

Views: 2097

Answers (1)

Mostafa Hussein
Mostafa Hussein

Reputation: 11950

Your issue with Flask configuration, as long as you get this error ECONNREFUSED while trying to connect it means that there is no service running on port 5000 with the ip you are trying to use and that's because this function socketio.run(app) defaults to 127.0.0.1 which will be the localhost inside the container itself. In order to make your application accessible from outside the container or through the container ip in general you have to pass another parameter to that function called host with value 0.0.0.0 in order to be listen on any interface inside the container.

socketio.run(app, host='0.0.0.0')

Quoted from the documentation:

run(app, host=None, port=None, **kwargs)
Run the SocketIO web server.

Parameters: 
app – The Flask application instance.
host – The hostname or IP address for the server to listen on. Defaults to 127.0.0.1.
port – The port number for the server to listen on. Defaults to 5000.

Upvotes: 3

Related Questions