Skip to content

Commit 043bee6

Browse files
authored
PR for issue CrunchyData#63 - Docker enhancements and example (CrunchyData#64)
Add Alpine-based self-built Dockerfile, and examples of data-loading using shp2pgsql and psql within PostGIS Container
1 parent 8ec7677 commit 043bee6

File tree

10 files changed

+207
-0
lines changed

10 files changed

+207
-0
lines changed

Dockerfile.alpine

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Lightweight Alpine-based pg_tileserv Docker Image
2+
# Author: Just van den Broecke
3+
FROM golang:1.15.5-alpine3.12
4+
5+
# Build ARGS
6+
ARG VERSION="latest-alpine-3.12"
7+
8+
RUN mkdir /app
9+
ADD . /app/
10+
WORKDIR /app
11+
RUN go build -v -ldflags "-s -w -X main.programVersion=${VERSION}"
12+
13+
# Multi-stage build: only copy build result and resources
14+
FROM alpine:3.12
15+
16+
LABEL original_developer="Crunchy Data" \
17+
contributor="Just van den Broecke <[email protected]>" \
18+
vendor="Crunchy Data" \
19+
url="https://crunchydata.com" \
20+
release="${VERSION}" \
21+
org.opencontainers.image.vendor="Crunchy Data" \
22+
os.version="3.12"
23+
24+
RUN apk --no-cache add ca-certificates && mkdir /app
25+
WORKDIR /app/
26+
COPY --from=0 /app/pg_tileserv /app/
27+
COPY --from=0 /app/assets /app/assets
28+
29+
VOLUME ["/config"]
30+
31+
USER 1001
32+
EXPOSE 7800
33+
34+
ENTRYPOINT ["/app/pg_tileserv"]
35+
CMD []
36+
37+
# To build and run specific version
38+
#
39+
# export VERSION="latest-alpine-3.12"
40+
# docker build --build-arg VERSION=${VERSION} -t pramsey/pg_tileserv:${VERSION} -f Dockerfile.alpine
41+
#
42+
# Best is to use another PostGIS Docker Container whoose host is reachable from the pg_tileserv Container.
43+
# docker run -dt -e DATABASE_URL=postgres://user:pass@host/dbname -p 7800:7800 pramsey/pg_tileserv:${VERSION}
44+
#
45+
# See a full example using Docker Compose under examples/docker
46+
#

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ SET DATABASE_URL=postgresql://username:password@host/dbname
3939
pg_tileserv.exe
4040
```
4141

42+
### Docker
43+
44+
Use [Dockerfile.alpine](Dockerfile.alpine) to build a lightweight (18MB expanded) Docker Image.
45+
See also [a full example with Docker Compose](examples/docker/README.md).
46+
4247
## Trouble-shooting
4348

4449
To get more information about what is going on behind the scenes, run with the `--debug` commandline parameter on, or turn on debugging in the configuration file:

examples/docker/README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Docker Examples
2+
3+
by: Just van den Broecke - justb4 (@) gmail.com
4+
5+
This example uses Docker Compose with `pg_tileserv` and PostGIS (v3) Docker Images.
6+
Run with these steps: Build, Run, Load Vector Data, and run the
7+
standard web-viewer examples like [Leaflet](../leaflet/leaflet-tiles.html).
8+
9+
We use a [docker-compose.yml file](docker-compose.yml) with environment settings in
10+
[pg_tileserv.env](pg_tileserv.env) and [pg.env](pg.env) for `pg_tileserv` and the PG database.
11+
12+
## Build
13+
14+
* `docker-compose build`
15+
16+
This should build the latest [Alpine-based Docker Image](../../Dockerfile.alpine) for `pg_tileserv`.
17+
18+
## Run
19+
20+
* `docker-compose up`
21+
22+
NB on the first run the PostGIS Docker Image is downloaded and the DB initialized. `pg_tileserv` may not be able to connect.
23+
In that case stop the Docker Compose process (ctrl-C) and run again. In another terminal window test
24+
if `pg_tileserv` Container is at least running:
25+
26+
* `curl -v http://localhost:7800/public.ne_50m_admin_0_countries/2/2/3.pbf`.
27+
28+
You will see an regular error message like *"Unable to get layer 'public.ne_50m_admin_0_countries"* as no data is yet in the database.
29+
30+
## Load Data
31+
32+
We load all sample data using `shp2pgsql` and `psql` within the PostGIS Docker Container, so we don't need to install any Postgres/PostGIS tools locally.
33+
The `./data` dir is mapped into the Docker Container at `/work`.
34+
35+
First Download these files into the `./data` subdir:
36+
37+
* Natural Earth [Admin 0 Countries](https://www.naturalearthdata.com/downloads/50m-cultural-vectors/).
38+
* [fire hydrant data](https://opendata.vancouver.ca/explore/dataset/water-hydrants/download/?format=shp&timezone=America/Los_Angeles&lang=en&epsg=26910)"
39+
40+
Unzip these two zip-files within the `./data` subdir.
41+
42+
To run also the [OpenLayers Voronoi example](../openlayers/openlayers-function-click.md) using Docker, we apply
43+
the [OpenLayers Function-click SQL](../openlayers/openlayers-function-click.sql). This example demonstrates the powerful "Function" capability of `pg_tileserv`,
44+
creating the `public.hydrants_delaunay()` function in your database.
45+
46+
To load the two datasets and Function SQL, use the [load-data.sh helper script](load-data.sh)
47+
48+
* `./load-data.sh`
49+
* restart the docker-compose stack
50+
51+
The above data-loading script `exec`s the running PostGIS Docker Container `pg_tileserv_db` as for example:
52+
53+
* `docker-compose exec pg_tileserv_db sh -c "shp2pgsql -d -D -s 4326 /work/ne_50m_admin_0_countries.shp | psql -U tileserv -d tileserv"`
54+
55+
## Run Webviewers
56+
57+
As the `pg_tileserv` container has a Docker port-mapping to localhost:7800, you can use the standard HTML examples locally in your browser.
58+
In a real-world application you would run these in a web-server container like `nginx` or `Apache httpd`.
59+
60+
See [Leaflet](../leaflet/leaflet-tiles.html), [MapBox](../mapbox-gl-js/mapbox-gl-js-tiles.html) and [OpenLayers](../openlayers/openlayers-tiles.html).
61+
And the [openlayers-function-click.html](../openlayers/openlayers-function-click.html) for the Voronoi Function example.
62+
63+
## Clean/Restart
64+
65+
If something goes wrong along the way, or you want a clean restart, run this script:
66+
67+
* `./cleanup.sh`
68+
69+
This will delete dangling Docker Containers and and Images and the DB volume

examples/docker/cleanup.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
# General cleanup, first stop
3+
4+
# first make sure the compose stack is stopped
5+
docker-compose stop
6+
7+
#
8+
# Remove all exited containers
9+
for c in $(docker ps -a -f status=exited -q)
10+
do
11+
docker rm ${c}
12+
done
13+
14+
# And dangling images
15+
for i in $(docker images -f dangling=true -q)
16+
do
17+
docker rmi ${i}
18+
done
19+
20+
# Remove the DB Volume, removes database
21+
docker volume rm docker_pg_tileserv_db

examples/docker/data/.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*.zip
2+
*.cpg
3+
*.dbf
4+
*.prj
5+
*.html
6+
*.shp
7+
*.shx
8+
*.txt

examples/docker/data/.gitkeep

Whitespace-only changes.

examples/docker/docker-compose.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# pg-tileserv Docker Compose example setup.
2+
#
3+
# To build/run, see README.md
4+
#
5+
version: "3"
6+
7+
services:
8+
pg_tileserv:
9+
image: pramsey/pg_tileserv:latest-alpine-3.12
10+
build:
11+
context: ../..
12+
dockerfile: Dockerfile.alpine
13+
args:
14+
VERSION: latest-alpine-3.12
15+
16+
container_name: pg_tileserv
17+
18+
env_file:
19+
- pg_tileserv.env
20+
21+
depends_on:
22+
- pg_tileserv_db
23+
24+
ports:
25+
- 7800:7800
26+
27+
pg_tileserv_db:
28+
image: postgis/postgis:13-3.0-alpine
29+
30+
container_name: pg_tileserv_db
31+
32+
volumes:
33+
- ./data:/work
34+
- pg_tileserv_db:/var/lib/postgresql/data
35+
36+
env_file:
37+
- pg.env
38+
39+
volumes:
40+
pg_tileserv_db:

examples/docker/load-data.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Shortcuts to load all data in PostGIS DB in Docker Container
2+
# NB the docker-compose stack must be running !
3+
#
4+
5+
# Load Admin 0 countries
6+
docker-compose exec pg_tileserv_db sh -c "shp2pgsql -D -s 4326 /work/ne_50m_admin_0_countries.shp | psql -U tileserv -d tileserv"
7+
8+
# Load Vancouver Water Hydrants
9+
docker-compose exec pg_tileserv_db sh -c "shp2pgsql -D -s 26910 -I /work/water-hydrants.shp hydrants | psql -U tileserv -d tileserv"
10+
11+
# Load SQL Functions for OpenLayers example
12+
cp ../openlayers/openlayers-function-click.sql ./data/
13+
docker-compose exec pg_tileserv_db sh -c "cat /work/openlayers-function-click.sql | psql -U tileserv -d tileserv"
14+
rm ./data/openlayers-function-click.sql

examples/docker/pg.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
POSTGRES_USER=tileserv
2+
POSTGRES_PASSWORD=tileserv
3+
POSTGRES_DB=tileserv

examples/docker/pg_tileserv.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DATABASE_URL=postgres://tileserv:tileserv@pg_tileserv_db/tileserv

0 commit comments

Comments
 (0)