Set up your own geocoder API with Docker
For one of my projects I need an API for autocomplete geographical data. So for example, when someone types "27th street lo" that it returns "27th Street, Long Island, New York" in a formatted way including the geographical location.
Google and other service providers, provide these kinds of APIs. But in the end, they come with a cost per request. In this era, nothing is easier than run it on your own. You need some Docker knowledge, but if you have, you can have a geocoder API running in less than 10 minutes.
In this blog post I will explain how you set up a geocoder API based on Open Street Map data using Docker. I will explain every step. And I assure you, that it is easy to do.
Ok, let's go. Some info:
- We are going to use Komoot Photon (see example) as the Geocoder API.
- I suppose you have a local system or VPS that runs docker.
A. Get the necessary sources
I have added a project to GitHub
- Clone this Photon Github project:
git clone https://github.com/tonsnoei/photon-docker
B. (Optional) Build your own docker image
You can skip this step. But if, in the future, the used docker image is lost for some reason. You can build it yourself:
- Create a docker hub account at the Docker Hub website
- Go to the directory where the clone is placed and run the following commands:
- (Optional) Open the
Dockerfile
and check thephoton.jar
file version that is used. - (Optional) Check the available latest release here
https://github.com/komoot/photon/releases/
and change theDockerfile
accordingly - Build the docker image of Photon API
docker build -t <DOCKER HUB USERNAME>/docker-photon:latest -f Dockerfile .
- Push the docker image to the docker hub:
docker push <DOCKER HUB USERNAME>/docker-photon:latest
- Change the
docker-compose.yml
file to use this new docker image:
1version: '2'
2services:
3 photon:
4 build:
5 context: .
6 dockerfile: Dockerfile
7>>> image: tonsnoei/photon-geocoder:latest
8 volumes:
9 - data:/photon/photon_data
10 ports:
11 - 2322:2322
12
13volumes:
14 data:
15 driver: local
C. Prepare your docker
- Be sure you are in the directory where
https://github.com/tonsnoei/photon-docker
is cloned. - Create a directory "data":
mkdir data
- By default, the whole world is available within the API. This means that you need a relatively heavy server. This means at least 4 cores with at least 16Gb of RAM.
- If you want to host just a single country, you can modify the entrypoint.sh file to use a country extract only. Extracts can be found here. The explanation of changing the entrypoint.sh can be found within the entrypoint.sh file
- Run:
docker-compose build
. This will rebuild the contents of the Dockerfile. This is especially important when you made changes to the entrypoint.sh. - Now you are all set, run:
docker-compose up -d && docker-compose logs -f
- It will now start with downloading the extract from the web as first step.
- If you stop de docker container while downloading the data half way, clear the
data
directory first before continuing. - After the extract is downloaded, the API will be available at http://localhost:2322/api?q=amsterdam
- If you want to use a different region extract:
- Turn off the docker
docker-compose down
- Modify the entrypoint.sh file
- Run
docker-compose build
- Run
docker-compose up -d && docker-compose logs -f
- Turn off the docker
To run this on a production environment, use a reverse proxy like Nginx or Traefik to handle the HTTP(S) traffic.
That's it. Have fun.