2025, Nov 05 03:00
Dash ignoring HOST in Conda environments: why it defaults to 127.0.0.1 and ways to bind 0.0.0.0
Why Dash ignores the HOST environment variable in Conda and binds to 127.0.0.1. Fix: listen on 0.0.0.0 by passing host from env or unsetting CONDA_PREFIX.
Dash apps often rely on environment variables for configuration. One common expectation is that setting HOST=0.0.0.0 will make the server listen on all interfaces by default. Yet, in some setups the app stubbornly binds to 127.0.0.1, even though PORT is respected. Here’s what’s going on and how to handle it cleanly.
Minimal example that reproduces the issue
import os
import dash
from dash import html
svc = dash.Dash(__name__)
svc.layout = html.Div("Hello Dash!")
print(f'{os.environ["HOST"]=}')
svc.run()
The Dash documentation suggests the server should pick up HOST if you don’t pass host explicitly:
host
Host IP used to serve the application, default to "127.0.0.1" env: HOST
But in environments where HOST is set to 0.0.0.0, the app may still run on 127.0.0.1 unless you override it in code.
What actually happens and why
Dash deliberately ignores HOST whenever it detects that it is running inside a Conda-managed environment (CONDA_PREFIX is in os.environ). This behavior was introduced while addressing #3069 and landed via PR #3130. The reason is that some Conda activators export an invalid host name (for example, x86_64-conda-linux-gnu), which breaks Flask’s socket binding. To avoid that failure mode, Dash ignores HOST if CONDA_PREFIX is present.
Practical fixes
If you want the app to listen on 0.0.0.0, the most straightforward approach is to pass the host explicitly from the environment. This keeps configuration external while being explicit in code.
import os
import dash
from dash import html
svc = dash.Dash(__name__)
svc.layout = html.Div("Hello Dash!")
env_host = os.environ.get("HOST", "127.0.0.1")
svc.run(host=env_host)
Another option, when you do want Dash to automatically honor HOST, is to unset CONDA_PREFIX before importing dash so that the guard does not trigger.
import os
if "CONDA_PREFIX" in os.environ:
del os.environ["CONDA_PREFIX"]
import dash
from dash import html
svc = dash.Dash(__name__)
svc.layout = html.Div("Hello Dash!")
print(f'{os.environ["HOST"]=}')
svc.run()
This approach ensures the environment variable is considered just as the docs describe.
Why this matters
When deploying Dash under containers or orchestrators, binding to 0.0.0.0 is often required so the service is reachable from outside the container. Seeing PORT respected while HOST is ignored can be confusing and lead to time-consuming debugging. Understanding the CONDA_PREFIX guard explains the discrepancy and helps keep configuration predictable.
Takeaways
If you need a specific bind address, pass it explicitly to run; it’s unambiguous and resilient across environments. If you depend on environment-driven defaults and are in a Conda context, unset CONDA_PREFIX before importing dash so that HOST is honored. For background and rationale behind the guard, see the Dash issue #3069 and PR #3130.