Skip to content

Commit 3b91755

Browse files
authored
Docker development environment (#88)
Based on Debian 12 Bookworm.
1 parent f10185d commit 3b91755

File tree

8 files changed

+240
-0
lines changed

8 files changed

+240
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ electrification.xml
77
gauge.xml
88
*.mapcss
99
*.d
10+
tmp
11+
.kosmtik-config.yml
12+
.env
13+
*.osm.pbf

Dockerfile

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
FROM debian:12-slim
2+
3+
# https://serverfault.com/questions/949991/how-to-install-tzdata-on-a-ubuntu-docker-image
4+
ARG DEBIAN_FRONTEND=noninteractive
5+
6+
# Style dependencies
7+
RUN apt-get update \
8+
&& apt-get install --no-install-recommends -y \
9+
ca-certificates \
10+
curl \
11+
gnupg \
12+
postgresql-client \
13+
python3 \
14+
python3-distutils \
15+
fonts-hanazono \
16+
fonts-noto-cjk \
17+
fonts-noto-hinted \
18+
fonts-noto-unhinted \
19+
mapnik-utils \
20+
nodejs \
21+
npm \
22+
fonts-unifont \
23+
unzip \
24+
git \
25+
&& rm -rf /var/lib/apt/lists/* \
26+
&& apt-get clean
27+
28+
# Kosmtik with plugins, forcing prefix to /usr because Ubuntu sets
29+
# npm prefix to /usr/local, which breaks the install
30+
# We install kosmtik not from release channel, but directly from a specific commit on github.
31+
RUN npm set prefix /usr && npm install -g --unsafe-perm "git+https://[email protected]/kosmtik/kosmtik.git"
32+
33+
WORKDIR /usr/lib/node_modules/kosmtik/
34+
RUN kosmtik plugins --install kosmtik-overpass-layer \
35+
--install kosmtik-fetch-remote \
36+
--install kosmtik-overlay \
37+
--install kosmtik-open-in-josm \
38+
--install kosmtik-map-compare \
39+
--install kosmtik-osm-data-overlay \
40+
--install kosmtik-mapnik-reference \
41+
--install kosmtik-geojson-overlay \
42+
&& cp /root/.config/kosmtik.yml /tmp/.kosmtik-config.yml
43+
44+
# Closing section
45+
RUN mkdir -p /openrailwaymap
46+
WORKDIR /openrailwaymap
47+
48+
USER 1000
49+
ENTRYPOINT ["sh", "docker-startup.sh", "kosmtik"]

Dockerfile.db

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FROM postgis/postgis:10-2.5-alpine
2+
3+
COPY ./setup/tune-postgis.sh /docker-entrypoint-initdb.d/tune-postgis.sh

Dockerfile.import

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
FROM debian:12-slim
2+
3+
# https://serverfault.com/questions/949991/how-to-install-tzdata-on-a-ubuntu-docker-image
4+
ARG DEBIAN_FRONTEND=noninteractive
5+
6+
RUN apt-get update && apt-get install -y --no-install-recommends \
7+
ca-certificates \
8+
curl \
9+
gnupg \
10+
osm2pgsql \
11+
osmium-tool \
12+
gdal-bin \
13+
python3-psycopg2 \
14+
python3-yaml \
15+
python3-requests \
16+
unzip \
17+
postgresql-client \
18+
&& rm -rf /var/lib/apt/lists/* \
19+
&& apt-get clean
20+
21+
ADD setup/openstreetmap-carto.style setup
22+
23+
RUN mkdir -p /openrailwaymap
24+
WORKDIR /openrailwaymap
25+
26+
CMD ["sh", "docker-startup.sh", "import"]

SETUP.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,15 @@ Your development work flow would be the following:
189189
* edit the .mml or .mss files
190190
* call `carto $STYLE.mml > $STYLE.xml`
191191
* render map images of the location and zoom you would like to see: `nik4 -c $CENTER_LONGITUDE $CENTER_LATITUDE -z $ZOOM_LEVEL -x 2048 2048 $STYLE.xml output.png`
192+
193+
## Docker
194+
195+
Use the Docker setup to get a development environment up and running quickly.
196+
197+
This setup only requires Docker and Docker Compose to be installed, but and requires no other dependencies. This setup should also work on Windows systems (the containers will run in a virtual machine).
198+
199+
- Run `docker compose up db` to start the Postgres database.
200+
- Download a file with the OSM data, and name it `data.osm.pbf`. Run `docker compose up import` which will import the data, run the post-import database setup. This step also creates a file `.env` with environment variables that you can use to tune the import.
201+
- Run `docker compose up kosmtik` to start Kosmtik and view the map style. Edit the `command` argument in `docker-compose.yml` in order to render a different style (default `standard.mml`).
202+
203+
Go to http://127.0.0.1:6789 to view the OpenRailwayMap. Make changes to the OpenRailwayMap style or assets, and Kosmtik will auto-reload the changes.

docker-compose.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
version: '2'
2+
services:
3+
kosmtik:
4+
image: kosmtik:v1
5+
platform: linux/amd64
6+
build:
7+
context: .
8+
dockerfile: Dockerfile
9+
volumes:
10+
- .:/openrailwaymap
11+
depends_on:
12+
- db
13+
ports:
14+
- "127.0.0.1:6789:6789"
15+
environment:
16+
- PGHOST=db
17+
- PGUSER=postgres
18+
command:
19+
# Edit this argument to render a different style
20+
- standard.mml
21+
db:
22+
image: db:v1
23+
build:
24+
context: .
25+
dockerfile: Dockerfile.db
26+
environment:
27+
- POSTGRES_HOST_AUTH_METHOD=trust
28+
- PG_WORK_MEM
29+
- PG_MAINTENANCE_WORK_MEM
30+
import:
31+
image: import:v1
32+
build:
33+
context: .
34+
dockerfile: Dockerfile.import
35+
volumes:
36+
- .:/openrailwaymap
37+
depends_on:
38+
- db
39+
environment:
40+
- PGHOST=db
41+
- PGUSER=postgres
42+
- PG_WORK_MEM
43+
- PG_MAINTENANCE_WORK_MEM
44+
- OSM2PGSQL_CACHE
45+
- OSM2PGSQL_NUMPROC
46+
- OSM2PGSQL_DATAFILE
47+
- EXTERNAL_DATA_SCRIPT_FLAGS

docker-startup.sh

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/bin/sh
2+
3+
# This script is used to start the import of kosmtik containers for the Docker development environment.
4+
# You can read details about that in SETUP.md
5+
6+
# Testing if database is ready
7+
i=1
8+
MAXCOUNT=60
9+
echo "Waiting for PostgreSQL to be running"
10+
while [ $i -le $MAXCOUNT ]
11+
do
12+
pg_isready -q && echo "PostgreSQL running" && break
13+
sleep 2
14+
i=$((i+1))
15+
done
16+
test $i -gt $MAXCOUNT && echo "Timeout while waiting for PostgreSQL to be running"
17+
18+
case "$1" in
19+
import)
20+
# Creating default database
21+
psql -c "SELECT 1 FROM pg_database WHERE datname = 'gis';" | grep -q 1 || createdb gis && \
22+
psql -d gis -c 'CREATE EXTENSION IF NOT EXISTS postgis;' && \
23+
psql -d gis -c 'CREATE EXTENSION IF NOT EXISTS hstore;'
24+
25+
# Creating default import settings file editable by user and passing values for osm2pgsql
26+
if [ ! -e ".env" ]; then
27+
cat > .env <<EOF
28+
# Environment settings for importing to a Docker container database
29+
PG_WORK_MEM=${PG_WORK_MEM:-16MB}
30+
PG_MAINTENANCE_WORK_MEM=${PG_MAINTENANCE_WORK_MEM:-256MB}
31+
OSM2PGSQL_CACHE=${OSM2PGSQL_CACHE:-512}
32+
OSM2PGSQL_NUMPROC=${OSM2PGSQL_NUMPROC:-1}
33+
OSM2PGSQL_DATAFILE=${OSM2PGSQL_DATAFILE:-data.osm.pbf}
34+
EXTERNAL_DATA_SCRIPT_FLAGS=${EXTERNAL_DATA_SCRIPT_FLAGS:-}
35+
EOF
36+
chmod a+rw .env
37+
export OSM2PGSQL_CACHE=${OSM2PGSQL_CACHE:-512}
38+
export OSM2PGSQL_NUMPROC=${OSM2PGSQL_NUMPROC:-1}
39+
export OSM2PGSQL_DATAFILE=${OSM2PGSQL_DATAFILE:-data.osm.pbf}
40+
fi
41+
42+
osmium tags-filter \
43+
-o filtered.osm.pbf \
44+
"$OSM2PGSQL_DATAFILE" \
45+
nwr/railway \
46+
nwr/disused:railway \
47+
nwr/abandoned:railway \
48+
nwr/razed:railway \
49+
nwr/construction:railway \
50+
nwr/proposed:railway \
51+
n/public_transport=stop_position \
52+
nwr/public_transport=platform \
53+
r/route=train \
54+
r/route=tram \
55+
r/route=light_rail \
56+
r/route=subway
57+
58+
# Importing data to a database
59+
osm2pgsql \
60+
--create \
61+
--database gis \
62+
--hstore \
63+
--slim \
64+
--merc \
65+
--style setup/openstreetmap-carto.style \
66+
--tag-transform setup/openstreetmap-carto.lua \
67+
--multi-geometry \
68+
--cache $OSM2PGSQL_CACHE \
69+
--number-processes $OSM2PGSQL_NUMPROC \
70+
filtered.osm.pbf
71+
72+
# Post processing imported data
73+
psql -d gis -f sql/osm_carto_views.sql && \
74+
psql -d gis -f sql/functions.sql && \
75+
psql -d gis -f sql/get_station_importance.sql
76+
;;
77+
78+
kosmtik)
79+
# Creating default Kosmtik settings file
80+
if [ ! -e ".kosmtik-config.yml" ]; then
81+
cp /tmp/.kosmtik-config.yml .kosmtik-config.yml
82+
fi
83+
export KOSMTIK_CONFIGPATH=".kosmtik-config.yml"
84+
85+
# Second argument is the kosmtik options
86+
shift
87+
# Starting Kosmtik
88+
exec kosmtik serve --host 0.0.0.0 "$@"
89+
# It needs Ctrl+C to be interrupted
90+
;;
91+
92+
esac

setup/tune-postgis.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
3+
set -e
4+
export PGUSER="$POSTGRES_USER"
5+
6+
psql -c "ALTER SYSTEM SET work_mem='${PG_WORK_MEM:-16MB}';"
7+
psql -c "ALTER SYSTEM SET maintenance_work_mem='${PG_MAINTENANCE_WORK_MEM:-256MB}';"

0 commit comments

Comments
 (0)