Inside a dev container, Vite's dev server reports port 5174 and the host's browser cannot reach it; which server options explain that?
answer
- where the server listens, and how
- default port, then the next free one
- exit instead of drifting
- loopback-only default host
- listen on all addresses, then allow hostnames
basics
~10 sVite's dev server defaults to port 5173, quietly takes the next free port when that one is busy, and listens on localhost only. In a container, set server.host to true and server.strictPort to true.
solid answer
~40 sTwo defaults combine here. `server.port` is `5173`, and when that port is taken Vite automatically tries the next available one, so a forward set up for 5173 now reaches whatever else holds 5173 (often a dev server left running in another terminal), not the new one; `server.strictPort: true` makes Vite exit instead, so the clash is visible. `server.host` defaults to `'localhost'`, which inside a container is the container's own loopback, and traffic arriving through a published port never reaches it; `server.host: true` (or `'0.0.0.0'`, or `--host` on the CLI) listens on every address. If the app is then opened under a hostname other than `localhost`, a `.localhost` name or an IP address, add that name to `server.allowedHosts` rather than setting it to `true`.
code
typescript · 11 lines// vite.config.ts
import { defineConfig } from 'vite'
export default defineConfig({
server: {
host: true, // listen on all addresses, not only the container's loopback
port: 5173,
strictPort: true, // exit if 5173 is taken instead of moving to 5174
allowedHosts: ['dispatch.devbox.test'],
},
})go deeper
Recall the two defaults: port 5173 with silent fallback to the next free port, and a server that listens on localhost only. Know that host true and strictPort true are the usual container fixes.
Explain why a loopback-bound server cannot see traffic from a published container port, and how strictPort turns a drifting port into a visible failure for every forward that expects a fixed number.
Separate the three gates when diagnosing an unreachable dev server: the port actually bound, the address it listens on, and the hostname check, and resist allowedHosts true because of DNS rebinding.
Weigh a shared dev-container setup that pins ports and hostnames for every developer against letting each person override them, given the exposure that listening on all addresses creates on shared networks.
## What starts when you run `vite` `vite` (with `vite dev` and `vite serve` as aliases) starts Vite's **development server**. It does not bundle the application up front; it transforms each module when the browser asks for it, which is why it starts in moments. This question is about what comes before any of that: **where the server listens** — which port, and on which network addresses — and why a container makes both defaults bite. ## The port: `5173`, then the next free one - `server.port` defaults to **`5173`**. - When that port is already in use, Vite **automatically tries the next available port** and prints the URL it actually got, so the terminal says 5174, 5175 and so on. - `server.strictPort: true` changes that: Vite **exits** when the port is taken instead of moving. - On the command line the same settings are `--port <port>` and `--strictPort`. Moving to a free port is friendly on a laptop and hostile behind anything configured for a fixed number: a container's published port, an editor's port forward, a reverse proxy, or a backend that links to the dev server's origin. All of them keep pointing at 5173, and whatever still holds 5173 — typically a dev server left running in another terminal — answers instead, or nothing does. The 5174 in the terminal is the symptom; the earlier process is the cause. ## The host: which addresses accept connections `server.host` defaults to **`'localhost'`**. | `server.host` | Listens on | Reachable from | |---|---|---| | `'localhost'` (default) | the loopback address `localhost` resolves to | the same machine only; inside a container, the container itself | | `true` or `'0.0.0.0'` | all addresses, including LAN and public ones | anything that can route to the machine, such as a published container port | | `'127.0.0.1'` | the IPv4 loopback | the same machine, over IPv4 | A published container port delivers traffic to the container's network interface, not to its loopback, so a server bound to `localhost` inside the container never sees the request and the browser on the host gets a refused or reset connection. `server.host: true`, or `vite --host`, listens on every address and fixes it. A quick test separates the cases. If a request from a shell **inside** the container to `http://localhost:5173` succeeds while the host's browser fails on the same port, the server is up and bound to loopback: a host problem. If the request inside the container reaches an older page or a different app, the port drifted: a port problem. If both succeed but the browser shows a refusal naming the hostname, it is the hostname check below. Two details from Vite's documentation are worth knowing: 1. When an editor's **port-forwarding** feature does the forwarding rather than a published port, the troubleshooting guide recommends `server.host: '127.0.0.1'`, because one widely used forwarder does not support IPv6 and `localhost` may resolve to the IPv6 loopback. 2. Node's DNS result order can make `localhost` resolve differently for Vite and for the browser; Vite prints the resolved address when the two differ. ## The hostname check: `server.allowedHosts` Listening on every address brings in a second gate. The dev server only answers requests whose **hostname** it allows: - by default `localhost`, any name under `.localhost`, and every IP address; - extra names listed in `server.allowedHosts`, where a leading `.` admits a domain and all its subdomains; - the check is skipped when the dev server itself serves HTTPS. Opening the console as `dispatch.devbox.test` is therefore refused until that name is in the list. `allowedHosts: true` switches the check off and, as the docs warn, lets any website reach the dev server through **DNS rebinding** and download your source, so name the hosts instead. ## A container-ready setup 1. Pin the port with `port: 5173` and `strictPort: true`, so a clash fails loudly instead of drifting. 2. Listen beyond loopback with `host: true`. 3. List the hostnames you really use in `allowedHosts`. 4. Publish or forward exactly the port the config pins. ## What to take away - **5174** means 5173 was busy and `strictPort` was off; find the other process. - **Unreachable on the right port** means the server listens on loopback only; set `host`. - **"Not allowed" on the right port** means the hostname check; extend `allowedHosts`, never set it to `true`. - None of these options changes what the production build emits. `vite preview` has its own `preview.*` counterparts, which default to `server.host`, `server.strictPort` and `server.allowedHosts`, but listens on port `4173`.
- The server now listens on all addresses, but opening it as dispatch.devbox.test returns a refusal page; what is missing?The hostname check. Vite's dev server answers `localhost`, names under `.localhost` and IP addresses by default; any other hostname must be listed in `server.allowedHosts` (a leading `.` admits a domain and its subdomains). Setting it to `true` disables the check and opens the dev server to DNS-rebinding attacks, so add the one name instead.
- Why not leave strictPort off and simply forward whatever port Vite picks?Because everything around the dev server was configured for one number: the container's published port, an editor's forward, a backend that links to the dev origin. A silently changed port leaves all of them pointing at a stale process. Failing on start with `strictPort: true` turns a confusing half-working setup into one clear error.
- Vite stops with an error whenever port 5173 is already taken
- The dev server listens on all network addresses by default
- server.host also controls which hostnames the server will answer
- Setting allowedHosts to true is a harmless way to silence host errors
- server.port and server.host change the production build's output