Web & Networking › Networking Fundamentals
localhost / 127.0.0.1
The address that always means "this machine".
Also known as: 127.0.0.1, loopback, local host
localhost is a special name that always means “this machine”. Connecting to it never leaves your computer. It uses the loopback address 127.0.0.1 (and ::1 in IPv6).
python -m http.server 8000
# now visit http://localhost:8000 in your browser
curl http://127.0.0.1:8000
The server and the client are both on your machine, so no network is involved.
Why it matters for development
- Run and test your app locally, with a dev server on
localhost:3000, a database onlocalhost:5432, and so on. - Safe by default. A service bound to
127.0.0.1accepts connections only from the same machine, so others on the network can’t reach it. - Fast, with no real network.
Binding addresses
The address a server listens on decides who can connect:
| Bind to | Reachable from |
|---|---|
127.0.0.1 / localhost | this machine only |
0.0.0.0 | all network interfaces: other machines too |
| a specific LAN IP | that network only |
A common problem: a server works on your laptop but not from your phone, because it’s bound to localhost. Bind to 0.0.0.0 for LAN testing (and be careful about what you expose). In Docker, a server inside a container must listen on 0.0.0.0 for published ports to work (port mapping).
Gotchas
localhostinside a container means the container itself, not your host machine. To reach the host, use the platform’s special name, or a Compose service name.localhoston a server means that server, not your laptop.- IPv4 vs IPv6:
localhostmay resolve to::1, while the server listens only on127.0.0.1. If one works and the other doesn’t, check both. - Browsers treat
http://localhostas a secure context for many features, so you often don’t need HTTPS locally, but cookies and CORS still apply (CORS). - The name comes from the hosts file, not the internet (DNS).