Running OSRM with Docker

I’ve now been through the process of setting up OSRM a few times. While it’s not exactly taxing, it seemed like a prime candidate for automation.

Although there are existing Docker images for OSRM, I elected to roll my own to have a little more control. You can find the Dockerfile and a startup script here.

To use, do as follows:

  1. Build the image.

    $ docker build -t osrm:latest .
  2. Download map data. For the sake of illustration, we’ll assume that the resulting file is called map.xml.

  3. Launch a container.

    $ docker run -p 5000:5000 -v `pwd`:/data osrm:latest map.xml

The image exposes the service on port 5000, which is mapped to port 5000 on the host. Now go ahead and submit requests!

Preprocessing

The OSRM Docker images are also really useful for pre-processing large maps. Typically I’ll spin up a large EC2 instance to prepare the maps and then transfer the results to a smaller machine to actually run the server.

First install the OpenStreetMap tools.

sudo apt install osmctools

Now, suppose that you were preparing a large map and have a map file, north-america-latest.osm.pbf, in your working directory.

First convert to a compact binary format.

osmconvert north-america-latest.osm.pbf -o=north-america-latest.osm.o5m

Filter out some map attributes.

osmfilter north-america-latest.osm.o5m --keep="place=city" -o=north-america-latest.osm

Next use the OSRM Docker image to preprocess the map data.

docker run -t -v "${PWD}:/data" osrm/osrm-backend osrm-extract -p /opt/car.lua /data/north-america-latest.osm
docker run -t -v "${PWD}:/data" osrm/osrm-backend osrm-contract /data/north-america-latest.osrm
docker run -t -v "${PWD}:/data" -p 5000:5000 osrm/osrm-backend osrm-routed /data/north-america-latest.osrm

No OSRM install required. Also, using image tags it’s very easy to ensure that you use a specific version of OSRM.