This section contains the quick-start note for dockerizing Plotly and Dash for your solution. We would go over the tips on writing the Dockerfile and the dependent Python scripts.
You may read the instructions in this Medium Post: Dockerizing Plotly/Dash
Beside copying the scripts to the docker container, you would need to install the right version of dependencies in order to have Dash run properly. Here are the important note:
- Beside Plotly and Dash, You must include flask v2.1.3
- werkzeug must be v2.0.3 which is downloaded from this repository. It is also included in the Dockerfile
When initiate the Dash app object, the host must be set at 0.0.0.0 in order for the local environment to access the container environment.
if __name__ == '__main__':
app.run_server(debug=True, host='0.0.0.0', port=9000)
Since we have set the port to 9000 here, the dashboard will be ran in port 9000 in the Container environment.
Once you have Dockerfile ready, then:
- Build the docker image (Be sure to include "." for the Dockerfile path)
- Run and create a docker container
- Access the dashboard on your browser with the right port number
See the code below, given we have declared port at 9000 in the container environment.
# Build the docker image
docker build -t [image_name] .
# Run and create a docker container
docker run -h localhost -p 9002:9000 -d --name [container_name] [image_name]
In the code above, we have expose the dashboard at port 9000 in the local environment. It means once we access port 9000 in the local environment, it would redirect us to the port 9002 in the container environment.
Note: This example we have set local machine port at 9002 and container port 9000 (According to the port declared in the Python script). You may choose other port number.
Troubleshoot Host Error Running dash app in docker container in the Plotly Community
Troubleshoot Werkzeug Error Dash ImportError in Stack Overflow