Deployment on Docker

Hi,

I would like to deploy my Dash app on Docker. My folder structure:

  • application
    • Dockerfile
    • requirement.txt
    • index.py

My index.py

if __name__ == '__main__':
    app.run_server(host='0.0.0.0', port=8050, debug=True)

My Dockerfile

FROM python:3.7

RUN mkdir /application
WORKDIR /application

COPY requirements.txt /
RUN pip install -r /requirements.txt

COPY ./ ./
EXPOSE 8050

CMD ["python", "./index.py"]

The commands for docker:

docker build -t test_app .
docker run test_app  0.0.0.0:8050 -d

Error: docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused “exec: “0.0.0.0:8050”: executable file not found in $PATH”: unknown.

What am I doing wrong?

Kind regards

In deployment, i would recommend using a production grade web server such as gunicorn. Assuming that gunicorn is installed (just add it to the requirement.txt file), you could start it in your docker container like this (here i assume that there is a application file app.py in which the Flask server is called app),

CMD [ "gunicorn", "--workers=1", "--threads=1", "-b 0.0.0.0:8000", "app:app"]

Thanks for the answer but I’m still getting the same error…

My Dockerfile:

FROM python:3.7

RUN mkdir /app
WORKDIR /app

COPY requirements.txt /
RUN pip install -r /requirements.txt

COPY ./ /app
EXPOSE 8050

CMD [ "gunicorn", "--workers=1", "--threads=1", "-b 0.0.0.0:8050", "index:app.server"]

I have added gunicorn into the requirements.txt

I guess there is a mismatch between how you copy the files and the run command. Can you post the content of index.py?

This is the content of my image:

I followed this link for multi-paging:
My index.py

import uuid
import dash_core_components as dcc
import dash_html_components as html
import dash_resumable_upload
from dash.dependencies import Input, Output, State
import dash_flexbox_grid as dfx
import dash
import os
from app import app
from Tabs import home, data_overview, header, evaluation
from dash.exceptions import PreventUpdate


def serve_layout():
    session_id = str(uuid.uuid4())
    return html.Div([
    header.layout,
    dcc.Store(id='options_store', storage_type='session'),
    dcc.Store(id='session_id', storage_type='session', data=dict(id=session_id)),
    dfx.Grid(id='page-content', fluid=True, children=[
    ])
])

app.layout = serve_layout

@app.callback(Output('page-content', 'children'),
              [Input('tabs_navbar', 'value')],
              [State('session_id', 'data'), State('options_store', 'data')]
)
def display_page(value, session_id, data):
    # Load layout based on selected tab
    session_id = session_id.get('id')

    if value == 'home':
        return home.layout
    elif value == 'data-overview':
        return data_overview.layout
    elif value == 'evaluation':
        return evaluation.layout
    else:
        return '404'

if __name__ == '__main__':
    app.run_server(debug=True)

My app.py:

import dash
import uuid
import dash_html_components as html
import dash_core_components as dcc
import flask
import dash_resumable_upload

external_stylesheets = ['https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css', 'https://use.fontawesome.com/releases/v5.7.2/css/all.css']
external_scripts = ['http://code.jquery.com/jquery-1.7.1.min.js']
server = flask.Flask(__name__)
app = dash.Dash(__name__, external_stylesheets=external_stylesheets, external_scripts=external_scripts, server=server)


def serve_layout():
    session_id = str(uuid.uuid4())
    return html.Div([
    dcc.Store(id='session_id', storage_type='session', data=dict(id=session_id)),
    ])

dash_resumable_upload.decorate_server(app.server, 'Storage/Temporary')
app.layout = serve_layout


app.scripts.config.serve_locally = True
app.config.suppress_callback_exceptions = True

I have run: docker image inspect test_dashboard
And I saw this:

Could it be that this causes the error “executable file not found in $PATH”? (I’m new in Docker)

I am not sure what is going wrong in your example. It might be easier to start from a working example to figure it out. As a reference, i have created a MWE where the app is simply,

import dash
import dash_html_components as html

from flask import Flask

server = Flask(__name__)
app = dash.Dash(server=server)
app.layout = html.Div("Hello world.")

if __name__ == '__main__':
    app.run_server()

with a corresponding requirements.txt

dash==1.12.0
gunicorn
flask

and a simple Dockerfile,

FROM python:3.8-slim-buster

# Create a working directory.
RUN mkdir wd
WORKDIR wd

# Install Python dependencies.
COPY requirements.txt .
RUN pip3 install -r requirements.txt

# Copy the rest of the codebase into the image
COPY . ./

# Finally, run gunicorn.
CMD [ "gunicorn", "--workers=5", "--threads=1", "-b 0.0.0.0:8000", "app:server"]

You can download the files in the link below along with a docker-compose file for easy deployment.

5 Likes

Thanks for the example!