Overview
We run OSRM ourselves to estimate how long a driver takes to reach a customer. One Docker container serves car routes and distance matrices for one city or region. Place names, coordinates and file names in this log are examples: swap in your own area.
Hostnames, IP addresses and API keys in this log are placeholders. Replace them with your own.
Architecture
App backend
| HTTPS + X-API-Key header
v
nginx reverse proxy (small public VM, TLS)
| private network (VPN)
v
OSRM container, port 5000 (private server)
|
v
/data/city.osrm (routing graph built from OpenStreetMap)
Stack
• OSRM backend, Docker image ghcr.io/project-osrm/osrm-backend, MLD algorithm, car profile
• Map data: OpenStreetMap extract from Geofabrik, cut to your city with osmium-tool
• Ubuntu server with Docker for OSRM; nginx and Let's Encrypt on a small public VM; a VPN (Tailscale) between the two
Decisions
• Car profile only: roads open to cars, with one-ways and turn restrictions respected.
• One city or region only: a smaller map keeps builds fast and memory low (our city-sized build peaked at 359 MB while customizing).
• OSRM is used for ETAs only, with a 2 to 3 second timeout. Maps, search and everything else stay with another provider.
• No live traffic: OSRM returns free-flow travel times.
LOG 01: Prepare the working folder
2026-10-13 · host: private Ubuntu server · status: OK after one fix
Everything OSRM needs lives in one folder. The container mounts it later as /data, read-only.
$ mkdir -p ~/osrm
$ cd ~/osrm
If Docker creates this folder first through a bind mount, it belongs to root and the download fails:
region.osm.pbf: Permission denied
Cannot write to 'region.osm.pbf' (Permission denied).
Fix: hand the folder back to your user.
$ sudo chown -R $USER:$USER ~/osrm
$ ls -ld ~/osrm
LOG 02: Download the OpenStreetMap data
2026-10-13 · source: Geofabrik · status: OK
Start from the smallest published region that contains your city. Geofabrik URLs follow the pattern //-latest.osm.pbf. Our example is the southern zone of India, 532 MB; your file will differ.
$ cd ~/osrm
$ wget -O region.osm.pbf \
https://download.geofabrik.de/asia/india/southern-zone-latest.osm.pbf
HTTP request sent, awaiting response... 307 Temporary Redirect
Location: .../southern-zone-261012.osm.pbf [following]
HTTP request sent, awaiting response... 200 OK
Length: 558170847 (532M) [application/octet-stream]
Do not build routing data from this file. We started osrm-extract on the whole region, stopped it after a few minutes and cut the city out instead.
LOG 03: Cut your area out of the region
2026-10-13 · tool: osmium-tool · status: OK
$ sudo apt install -y osmium-tool
$ osmium extract -b ,,, --strategy=smart \
region.osm.pbf -o city.osm.pbf --overwrite
$ ls -lh city.osm.pbf
• The box is lon_min,lat_min,lon_max,lat_max. Pick one that covers the city and its ring road with some margin. Example for Hyderabad, India: 78.10,17.10,78.80,17.75.
• --strategy=smart keeps relations such as turn restrictions complete.
• Places outside the box cannot be routed: OSRM answers NoRoute for them.
• Once the next step works, delete the large source file and keep city.osm.pbf.
LOG 04: Build the routing graph
2026-10-13 · image: osrm-backend · profile: car.lua · status: OK, a few minutes
Three commands turn the map into files the server can load. Run them in order from ~/osrm.
$ docker run --rm -v "$PWD:/data" ghcr.io/project-osrm/osrm-backend \
osrm-extract -p /opt/car.lua /data/city.osm.pbf
$ docker run --rm -v "$PWD:/data" ghcr.io/project-osrm/osrm-backend \
osrm-partition /data/city.osrm
$ docker run --rm -v "$PWD:/data" ghcr.io/project-osrm/osrm-backend \
osrm-customize /data/city.osrm
• osrm-extract applies the car profile: which roads a car may use, and at what speed.
• osrm-partition splits the graph into cells for the MLD algorithm.
• osrm-customize computes the weights inside the cells.
The last lines of our run:
[info] Using script /opt/car.lua
[info] Profile: car.lua
[info] MLD customization writing took 0.141272 seconds
[info] Graph writing took 0.114269 seconds
[info] RAM: peak bytes used: 358871040
Lines such as Level 3 unreachable boundary nodes per cell are graph statistics, not errors.
Files written (the containers ran as root, so root owns them):
$ ls -lh city.osrm*
-rw-r--r-- 1 root root 106M city.osrm.cell_metrics
-rw-r--r-- 1 root root 880K city.osrm.cells
-rw-r--r-- 1 root root 46M city.osrm.ebg
-rw-r--r-- 1 root root 12M city.osrm.ebg_nodes
-rw-r--r-- 1 root root 14M city.osrm.edges
-rw-r--r-- 1 root root 12M city.osrm.enw
-rwx------ 1 root root 20M city.osrm.fileIndex
...
LOG 05: Run the OSRM server
2026-10-15 · container: osrm · status: OK
$ docker run -d --name osrm --restart unless-stopped \
-p 127.0.0.1:5000:5000 \
-p :5000:5000 \
-v "$HOME/osrm:/data:ro" \
ghcr.io/project-osrm/osrm-backend \
osrm-routed --algorithm mld /data/city.osrm
• Port 5000 is published on localhost and on the private VPN address only, never on the public interface.
• --algorithm mld must match the partition and customize steps in LOG 04.
• The data folder is mounted read-only (:ro).
• --restart unless-stopped brings the server back after a reboot.
The server is ready when the log ends with running and waiting for requests. Loading takes a few seconds, and requests sent before that fail.
$ docker logs osrm 2>&1 | tail -4
[info] Keepalive timeout: 5
[info] Maximum header size: 25024
[info] Maximum request body size: 1048576
[info] running and waiting for requests
LOG 06: Test the Route and Table APIs
2026-10-15 · host: the OSRM server · status: OK (code: Ok)
OSRM reads coordinates as longitude,latitude, the opposite of most map apps. The profile name in the URL is driving, which is the car profile we built.
One route between two points inside your map (all coordinates in this log are examples; use your own):
$ curl -s "http://127.0.0.1:5000/route/v1/driving/78.3489,17.4400;78.4483,17.4126?overview=false"
A good answer starts with "code":"Ok". In routes[0], distance is in meters and duration is in seconds.
Several drivers against one customer in a single request, which is what a dispatcher needs:
$ curl -s "http://127.0.0.1:5000/table/v1/driving/78.3489,17.4400;78.3915,17.4483;78.4483,17.4126?sources=0;1&destinations=2&annotations=duration,distance"
The first two points are drivers (sources), the third is the customer (destinations). The answer holds a durations matrix in seconds and a distances matrix in meters, one row per driver.
A point that cannot be matched to a road inside the map returns an error code such as NoSegment or NoRoute instead of Ok.
LOG 07: Expose it through nginx with an API key
2026-10-16 · host: public VM with nginx · status: OK
OSRM has no login of its own, so it never faces the internet directly. A small public VM runs nginx with a Let's Encrypt certificate and forwards two paths to the OSRM server over the VPN. Every request must carry an X-API-Key header. The config below is an example to adapt, not a copy of our file.
Generate the key and keep it out of Git:
$ openssl rand -hex 32
Example nginx site (map sits outside server; certbot adds the certificate lines):
map $http_x_api_key $osrm_key_ok {
default 0;
"REPLACE_WITH_LONG_RANDOM_KEY" 1;
}
server {
listen 443 ssl;
server_name osrm.example.com;
location /route/ { if ($osrm_key_ok = 0) { return 401; } proxy_pass http://<PRIVATE_IP>:5000; proxy_read_timeout 5s; } location /table/ { if ($osrm_key_ok = 0) { return 401; } proxy_pass http://<PRIVATE_IP>:5000; proxy_read_timeout 5s; } location / { return 404; } }
Reload and check all three outcomes:
$ sudo nginx -t && sudo systemctl reload nginx
$ curl -s -o /dev/null -w "%{http_code}\n" https://osrm.example.com/
404
$ curl -s -o /dev/null -w "%{http_code}\n" \
"https://osrm.example.com/route/v1/driving/78.3489,17.4400;78.4483,17.4126"
401
$ curl -s -H "X-API-Key: $OSRM_KEY" \
"https://osrm.example.com/route/v1/driving/78.3489,17.4400;78.4483,17.4126?overview=false"
{"code":"Ok", ...}
The root URL answers 404 on purpose. Only /route and /table pass through, and only with the key. Treat the key like a password: share it over a private channel and rotate it before it goes to anyone new.
LOG 08: Call OSRM from the app
2026-10-16 · service: backend (Node.js) · status: handed to the app developer
The backend needs two settings. OSRM_URL is the public address from LOG 07 without a trailing slash. OSRM_KEY is the shared secret the backend sends in the X-API-Key header; OSRM itself knows nothing about it, nginx checks it.
OSRM_URL=https://osrm.example.com
OSRM_KEY=
A minimal ETA function with a short timeout:
type Point = { lat: number; lng: number };
export async function driverEta(from: Point, to: Point) {
const url =
${process.env.OSRM_URL}/route/v1/driving/ +
${from.lng},${from.lat};${to.lng},${to.lat}?overview=false;
const res = await fetch(url, {
headers: { "X-API-Key": process.env.OSRM_KEY! },
signal: AbortSignal.timeout(2500),
});
if (!res.ok) throw new Error(OSRM ${res.status});
const data = await res.json();
if (data.code !== "Ok") throw new Error(OSRM ${data.code});
const route = data.routes[0];
return { meters: route.distance, seconds: route.duration };
}
Rules of thumb:
• Timeout of 2 to 3 seconds. A slow ETA is worse than no ETA.
• OSRM answers ETA questions only. Everything else stays with the other map provider.
• If the call fails or times out, show a fallback (for example the other provider's estimate) instead of blocking the booking.
• OSRM gives free-flow times. Compare them with real trip times before you promise customers an arrival minute.
Troubleshooting log
Every problem below happened to us during the build.
- Download fails with Permission denied. The ~/osrm folder belongs to root because Docker created it. Run sudo chown -R $USER:$USER ~/osrm (LOG 01).
- The container restarts in a loop and the log says Required files are missing, cannot continue. Lines such as Missing/Broken File: /data/...osrm.ebg mean the graph was never built, or the file name in the command does not match the files in the folder. Run the three build commands from LOG 04, check ls ~/osrm/.osrm, then start the container again.
- osrm-extract on the whole region takes far too long. Cut the city out first with osmium (LOG 03) and build from that file.
- osmium: command not found after apt update && apt install .... An unrelated third-party apt repository on the server failed its signature check (NO_PUBKEY ...), so apt update returned an error and && skipped the install. Run sudo apt install -y osmium-tool on its own.
- A test right after docker run fails with fetch failed. The server was still loading the graph. Wait for running and waiting for requests in docker logs, then try again.
- wget: not found inside a Node container. Slim images do not ship wget. Test with Node's built-in fetch, or with curl from the host.
- The public root URL returns 404. That is intended (LOG 07). Call /route/... or /table/... with the key.
- nginx returns 401. The X-API-Key header is missing or does not match the key in the nginx config.
- NoSegment or NoRoute for a valid place. The point is outside the box from LOG 03. Widen the box and rebuild. Maintenance and limits Refresh the map. Roads change, so rebuild from a fresh OpenStreetMap download from time to time (monthly is a reasonable start). Build in a new folder, then swap the container, so the old graph keeps serving until the new one is ready: $ mkdir ~/osrm-new && cd ~/osrm-new # repeat LOG 02, 03 and 04 here $ docker rm -f osrm $ docker run -d --name osrm --restart unless-stopped \ -p 127.0.0.1:5000:5000 -p :5000:5000 \ -v "$HOME/osrm-new:/data:ro" \ ghcr.io/project-osrm/osrm-backend \ osrm-routed --algorithm mld /data/city.osrm Back up the small file. Keep city.osm.pbf. Everything else in the folder can be rebuilt from it in minutes. Watch it. Run a known route every minute from a monitor and alert when the answer is not "code":"Ok". Know the limits. • Free-flow times only: no live traffic, so rush-hour trips take longer than OSRM says. • Car profile only: no bikes, no walking. • Roads outside your box do not exist for OSRM. • One container is one point of failure. Keep the timeout and the fallback from LOG 08. • Never publish port 5000 to the internet, and rotate the API key when people leave or the key leaks.
