Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Homme Zwaagstra committed Mar 4, 2014
0 parents commit 22e8cc9
Show file tree
Hide file tree
Showing 12 changed files with 367 additions and 0 deletions.
128 changes: 128 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
## -*- docker-image-name: "homme/openstreetmap-tiles:latest" -*-

##
# The OpenStreetMap Tile Server
#
# This creates an image with containing the OpenStreetMap tile server stack as
# described at
# <http://switch2osm.org/serving-tiles/manually-building-a-tile-server-12-04/>.
#

FROM phusion/baseimage:0.9.8
MAINTAINER Homme Zwaagstra <[email protected]>

# Set the locale. This affects the encoding of the Postgresql template
# databases.
ENV LANG C.UTF-8
RUN update-locale LANG=C.UTF-8

# Ensure `add-apt-repository` is present
RUN apt-get install -y software-properties-common python-software-properties

# Mapnik requires a recent version of Boost
RUN add-apt-repository ppa:mapnik/boost
RUN apt-get update -y
RUN apt-get install -y libboost-dev libboost-filesystem-dev libboost-program-options-dev libboost-python-dev libboost-regex-dev libboost-system-dev libboost-thread-dev

# Install remaining dependencies
RUN apt-get install -y subversion git-core tar unzip wget bzip2 build-essential autoconf libtool libxml2-dev libgeos-dev libpq-dev libbz2-dev proj munin-node munin libprotobuf-c0-dev protobuf-c-compiler libfreetype6-dev libpng12-dev libtiff4-dev libicu-dev libgdal-dev libcairo-dev libcairomm-1.0-dev apache2 apache2-dev libagg-dev liblua5.2-dev ttf-unifont

# Install postgresql and postgis
RUN apt-get install -y postgresql-9.1-postgis postgresql-contrib postgresql-server-dev-9.1

# Install osm2pgsql
RUN cd /tmp && git clone git://github.com/openstreetmap/osm2pgsql.git
RUN cd /tmp/osm2pgsql && \
./autogen.sh && \
./configure && \
make && make install

# Install the Mapnik library
RUN cd /tmp && git clone git://github.com/mapnik/mapnik
RUN cd /tmp/mapnik && \
git branch 2.0 origin/2.0.x && \
git checkout 2.0 && \
python scons/scons.py configure INPUT_PLUGINS=all OPTIMIZATION=3 SYSTEM_FONTS=/usr/share/fonts/truetype/ && \
python scons/scons.py && \
python scons/scons.py install && \
ldconfig

# Verify that Mapnik has been installed correctly
RUN python -c 'import mapnik'

# Install mod_tile and renderd
RUN cd /tmp && git clone git://github.com/openstreetmap/mod_tile.git
RUN cd /tmp/mod_tile && \
./autogen.sh && \
./configure && \
make && \
make install && \
make install-mod_tile && \
ldconfig

# Install the Mapnik stylesheet
RUN cd /usr/local/src && svn co http://svn.openstreetmap.org/applications/rendering/mapnik mapnik-style

# Install the coastline data
RUN cd /usr/local/src/mapnik-style && ./get-coastlines.sh /usr/local/share

# Configure mapnik style-sheets
RUN cd /usr/local/src/mapnik-style/inc && cp fontset-settings.xml.inc.template fontset-settings.xml.inc
ADD datasource-settings.sed /tmp/
RUN cd /usr/local/src/mapnik-style/inc && sed --file /tmp/datasource-settings.sed datasource-settings.xml.inc.template > datasource-settings.xml.inc
ADD settings.sed /tmp/
RUN cd /usr/local/src/mapnik-style/inc && sed --file /tmp/settings.sed settings.xml.inc.template > settings.xml.inc

# Configure renderd
ADD renderd.conf.sed /tmp/
RUN cd /usr/local/etc && sed --file /tmp/renderd.conf.sed --in-place renderd.conf

# Create the files required for the mod_tile system to run
RUN mkdir /var/run/renderd && chown www-data: /var/run/renderd
RUN mkdir /var/lib/mod_tile && chown www-data /var/lib/mod_tile

# Configure mod_tile
ADD mod_tile.load /etc/apache2/mods-available/
ADD mod_tile.conf /etc/apache2/mods-available/
RUN a2enmod mod_tile

# Ensure the webserver user can connect to the gis database
RUN sed -i -e 's/local all all peer/local gis www-data peer/' /etc/postgresql/9.1/main/pg_hba.conf

# Tune postgresql
ADD postgresql.conf.sed /tmp/
RUN sed --file /tmp/postgresql.conf.sed --in-place /etc/postgresql/9.1/main/postgresql.conf

# Set the kernel memory on container boot
RUN mkdir -p /etc/my_init.d
ADD shmmax.sh /etc/my_init.d/shmmax.sh

# Create a `renderd` `runit` service
RUN mkdir /etc/sv/renderd
ADD renderd.sh /etc/sv/renderd/run

# Clean up APT when done
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Expose the webserver and database ports
EXPOSE 80 5432

# We need the volume for importing data from
VOLUME ["/data"]

# Set the osm2pgsql import cache size in MB. Used in `run import`.
ENV OSM_IMPORT_CACHE 800

# Add the README
ADD README.md /usr/local/share/doc/

# Add the help file
RUN mkdir -p /usr/local/share/doc/run
ADD help.txt /usr/local/share/doc/run/help.txt

# Add the entrypoint
ADD run.sh /usr/local/sbin/run
ENTRYPOINT ["/sbin/my_init", "--", "/usr/local/sbin/run"]

# Default to showing the usage text
CMD ["help"]
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# OpenStreetMap Tile Server Container

This repository contains instructions for building a
[Docker](https://www.docker.io/) image containing the OpenStreetMap tile
serving software stack. It is based on the
[Switch2OSM instructions](http://switch2osm.org/serving-tiles/manually-building-a-tile-server-12-04/).

As well as providing an easy way to set up and run the tile serving software it
also provides instructions for managing the back end database, allowing you to:

* Create the database
* Import OSM data into the database
* Drop the database

Run `docker run homme/openstreetmap-tiles` for usage instructions.

## About

The container runs Ubuntu 12.04 (Precise) and is based on the
[phusion/baseimage-docker](https://github.com/phusion/baseimage-docker). It
includes:

* Postgresql 9.1
* Apache 2.2
* The latest [Osm2pgsql](http://wiki.openstreetmap.org/wiki/Osm2pgsql) code (at
the time of image creation)
* The latest [Mapnik](http://mapnik.org/) code (at the time of image creation)
* The latest [Mod_Tile](http://wiki.openstreetmap.org/wiki/Mod_tile) code (at
the time of image creation)

## Issues

This is a work in progress and although generally adequate it could benefit
from improvements. Please
[submit issues](https://github.com/geo-data/openstreetmap-tiles-docker/issues)
on GitHub. Pull requests are very welcome!
5 changes: 5 additions & 0 deletions datasource-settings.sed
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Perform sed substitutions for `datasource-settings.xml.inc`
s/%(dbname)s/gis/
s/%(estimate_extent)s/false/
s/%(extent)s/-20037508,-19929239,20037508,19929239/
s/<Parameter name="\([^"]*\)">%(\([^)]*\))s<\/Parameter>/<!-- <Parameter name="\1">%(\2)s<\/Parameter> -->/
39 changes: 39 additions & 0 deletions help.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
Usage: run [COMMAND ...]

Run OpenStreetMap Tile Server related operations in a docker container.

Positional arguments:
COMMAND The command to run. (default: help)

Commands:
help Show this help message
initdb Initialise the postgres database
startdb Start the postgresql database
createuser Create the osm user in the database
createdb Create the osm database
import Import osm data into the database
startservices Start the osm web services
cli Drop into a bash shell
dropdb Drop the osm database

Set up a database saved on an external volume:
The following command will initialise the postgresql database on an external
volume that can be used to persist the data:

docker run -v /data/osm-postgresql:/var/lib/postgresql homme/openstreetmap-tiles \
initdb startdb createuser createdb migrate

Import data:
The following will import the .osm file at `/tmp/import.osm` into the
database.

docker run -v /data/osm-postgresql:/var/lib/postgresql -v /tmp:/data homme/openstreetmap-tiles \
startdb import

Start the webserver:
Once data is loaded and users have been created run the webserver:

docker run -P -v /data/osm-postgresql:/var/lib/postgresql homme/openstreetmap-tiles \
startdb startservices

For further information run the cli and look at /usr/local/share/doc/README.md.
6 changes: 6 additions & 0 deletions mod_tile.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
LoadTileConfigFile /usr/local/etc/renderd.conf
ModTileRenderdSocketName /var/run/renderd/renderd.sock
# Timeout before giving up for a tile to be rendered
ModTileRequestTimeout 0
# Timeout before giving up for a tile to be rendered that is otherwise missing
ModTileMissingRequestTimeout 30
1 change: 1 addition & 0 deletions mod_tile.load
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
LoadModule tile_module /usr/lib/apache2/modules/mod_tile.so
5 changes: 5 additions & 0 deletions postgresql.conf.sed
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Perform sed substitutions on `postgresql.conf`
s/shared_buffers = 24MB/shared_buffers = 128MB/
s/#checkpoint_segments = 3/checkpoint_segments = 20/
s/#maintenance_work_mem = 16MB/maintenance_work_mem = 256MB/
s/#autovacuum = on/autovacuum = off/
6 changes: 6 additions & 0 deletions renderd.conf.sed
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Perform sed substitutions for `renderd.conf`
s/;socketname=/socketname=/
s/plugins_dir=\/usr\/lib\/mapnik\/input/plugins_dir=\/usr\/local\/lib\/mapnik\/input/
s/\(font_dir=\/usr\/share\/fonts\/truetype\)/\1\/ttf-dejavu/
s/XML=.*/XML=\/usr\/local\/src\/mapnik-style\/osm.xml/
s/HOST=tile.openstreetmap.org/HOST=localhost/
12 changes: 12 additions & 0 deletions renderd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh

##
# Runit run script for the renderd daemon
#

# Ensure postgresql is running
run startdb || exit 1

#`/sbin/setuser www-data` runs the given command as the user `www-data`. If
# you omit that part, the command will be run as root.
exec /sbin/setuser www-data /usr/local/bin/renderd --config /usr/local/etc/renderd.conf --foreground yes
116 changes: 116 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#!/bin/sh

##
# Run OpenStreetMap tile server operations
#

# Command prefix that runs the command as the web user
asweb="setuser www-data"

die () {
msg=$1
echo "FATAL ERROR: " msg > 2
exit
}

startdb () {
if ! pgrep postgres > /dev/null
then
chown -R postgres /var/lib/postgresql/ || die "Could not set permissions on /var/lib/postgresql"
service postgresql start || die "Could not start postgresql"
fi
}

initdb () {
echo "Initialising postgresql"
if [ -d /var/lib/postgresql/9.1/main ] && [ $( ls -A /var/lib/postgresql/9.1/main | wc -c ) -ge 0 ]
then
die "Initialisation failed: the directory is not empty: /var/lib/postgresql/9.1/main"
fi

mkdir -p /var/lib/postgresql/9.1/main && chown -R postgres /var/lib/postgresql/
sudo -u postgres -i /usr/lib/postgresql/9.1/bin/initdb --pgdata /var/lib/postgresql/9.1/main
ln -s /etc/ssl/certs/ssl-cert-snakeoil.pem /var/lib/postgresql/9.1/main/server.crt
ln -s /etc/ssl/private/ssl-cert-snakeoil.key /var/lib/postgresql/9.1/main/server.key
}

createuser () {
USER=www-data
echo "Creating user $USER"
setuser postgres createuser -s $USER
}

createdb () {
dbname=gis
echo "Creating database $dbname"
cd /var/www

# Create the database
setuser postgres createdb -O www-data $dbname

# Install the Postgis schema
$asweb psql -d $dbname -f /usr/share/postgresql/9.1/contrib/postgis-1.5/postgis.sql

# Set the correct table ownership
$asweb psql -d $dbname -c 'ALTER TABLE geometry_columns OWNER TO "www-data"; ALTER TABLE spatial_ref_sys OWNER TO "www-data";'

# Add the 900913 Spatial Reference System
$asweb psql -d $dbname -f /usr/local/share/osm2pgsql/900913.sql
}

import () {
# Find the most recent import.pbf or import.osm
import=$( ls -1t /data/import.pbf /data/import.osm 2>/dev/null | head -1 )
test -n "${import}" || \
die "No import file present: expected /data/import.osm or /data/import.pbf"

echo "Importing ${import} into gis"
echo "$OSM_IMPORT_CACHE" | grep -P '^[0-9]+$' || \
die "Unexpected cache type: expected an integer but found: ${OSM_IMPORT_CACHE}"

number_processes=`nproc`
$asweb osm2pgsql --slim --cache $OSM_IMPORT_CACHE --database gis --number-processes $number_processes $import
}

dropdb () {
echo "Dropping database"
cd /var/www
setuser postgres dropdb gis
}

cli () {
echo "Running bash"
cd /var/www
exec bash
}

startrenderd () {
if [ ! -e /etc/service/renderd ]
then
echo "Starting renderd"
ln -s /etc/sv/renderd /etc/service/ || die "Could not link renderd into runit"
else
echo "Starting renderd"
sv start renderd || die "Could not start renderd"
fi
}

startservices () {
startrenderd

if ! pgrep apache2 > /dev/null
then
echo "Starting web server"
service apache2 start || die "Could not start apache"
fi
}

help () {
cat /usr/local/share/doc/run/help.txt
}

# Execute the specified command sequence
for arg
do
$arg;
done
6 changes: 6 additions & 0 deletions settings.sed
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Perform sed substitutions for `settings.xml.inc`
s/<!ENTITY symbols "%(symbols)s">/<!ENTITY symbols "symbols">/
s/<!ENTITY osm2pgsql_projection "&srs%(epsg)s;">/<!ENTITY osm2pgsql_projection "\&srs900913;">/
s/<!ENTITY dwithin_node_way "&dwithin_%(epsg)s;">/<!ENTITY dwithin_node_way "\&dwithin_900913;">/
s/<!ENTITY world_boundaries "%(world_boundaries)s">/<!ENTITY world_boundaries "\/usr\/local\/share\/world_boundaries">/
s/<!ENTITY prefix "%(prefix)s">/<!ENTITY prefix "planet_osm">/
7 changes: 7 additions & 0 deletions shmmax.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

##
# Allow the kernel to supply the amount of memory to be requested by postgres
#

sysctl -w kernel.shmmax=268435456

0 comments on commit 22e8cc9

Please sign in to comment.