Skip to content

Commit

Permalink
Generate docker images automatically (#415)
Browse files Browse the repository at this point in the history
* Generate docker images automatically

* Modify docker section of README

* Add test script to build docker images
  • Loading branch information
mschoema authored Jan 11, 2024
1 parent 2561319 commit 80f7843
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 4 deletions.
68 changes: 68 additions & 0 deletions .github/workflows/docker-image.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: Docker image
on:
push:
branches:
- 'develop'
tags:
- 'v[0-9]+.[0-9]+.[0-9]+'

jobs:
build:
name: Build & push docker image
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
psql: [12,13,14,15,16]
postgis: [3.4]
os: [ubuntu-latest]

env:
IMG_NAME: ${{ github.repository }}
steps:
- name: Checkout
uses: actions/[email protected]

- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
# list of Docker images to use as base name for tags
images: mobilitydb/mobilitydb
# generate Docker tags based on the following events/attributes
flavor: |
latest=${{ matrix.psql == '16' && github.ref_type == 'tag' }}
prefix=${{ matrix.psql }}-${{ matrix.postgis }}-
tags: |
type=ref,event=branch
type=semver,pattern={{major}}.{{minor}}
- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
# Add linux/arm64 when postgis builds arm64 version
platforms: linux/amd64

- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
# Add linux/arm64 when postgis builds arm64 version
platforms: linux/amd64
context: .
file: ./docker/Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
build-args: |
POSTGRES_VERSION=${{ matrix.psql }}
POSTGIS_VERSION=${{ matrix.postgis }}
MOBILITYDB_TAG=${{ github.ref_name }}
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,22 @@ psql mobility -c "CREATE EXTENSION MobilityDB"
Docker Container
-----------------

Docker containers with MobilityDB and all its dependencies are available [here](https://github.com/MobilityDB/MobilityDB-docker). These images are based on the official [Postgres](https://github.com/docker-library/postgres) and [Postgis](https://github.com/postgis/docker-postgis) docker images, please refer to them for more information.
Docker images with MobilityDB and all its dependencies are available [here](https://hub.docker.com/r/mobilitydb/mobilitydb). These images are based on the official [Postgres](https://github.com/docker-library/postgres) and [Postgis](https://github.com/postgis/docker-postgis) docker images, please refer to them for more information.

If you have docker installed in your system you can run:
```bash
docker pull mobilitydb/mobilitydb
docker volume create mobilitydb_data
docker run --name "mobilitydb" -d -p 25432:5432 -v mobilitydb_data:/var/lib/postgresql mobilitydb/mobilitydb
psql -h localhost -p 25432 -d mobilitydb -U docker
docker run --name mobilitydb -e POSTGRES_PASSWORD=mysecretpassword \
-p 25432:5432 -v mobilitydb_data:/var/lib/postgresql -d mobilitydb/mobilitydb
psql -h localhost -p 25432 -U postgres
```
The first command is to download the latest most up-to-date image of MobilityDB. The second command creates a volume container on the host, that we will use to persist the PostgreSQL database files outside of the MobilityDB container. The third command executes this binary image of PostgreSQL, PostGIS, and MobilityDB with the TCP port 5432 in the container mapped to port 25432 on the Docker host (user = postgres, db = postgres, pw=*mysecretpassword*). The fourth command is to connect to the database using psql.

Note that you can define the environment variable PGPASSWORD to avoid an interactive pw prompt.
```bash
PGPASSWORD=mysecretpassword psql -h localhost -p 25432 -U postgres
```
The first command is to download the latest most up-to-date image of MobilityDB. The second command creates a volume container on the host, that we will use to persist the PostgreSQL database files outside of the MobilityDB container. The third command executes this binary image of PostgreSQL, PostGIS, and MobilityDB with the TCP port 5432 in the container mapped to port 25432 on the Docker host (user = pw = docker, db = mobilitydb). The fourth command is to connect to the database using psql.

Issues
------
Expand Down
47 changes: 47 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
ARG POSTGRES_VERSION
ARG POSTGIS_VERSION

FROM postgis/postgis:$POSTGRES_VERSION-$POSTGIS_VERSION

# Configuration Parameters
LABEL maintainer="MobilityDB Project - https://github.com/MobilityDB/MobilityDB"

# Fix the Release file expired problem
RUN printf "Acquire::Check-Valid-Until \"false\";\nAcquire::Check-Date \"false\";" | cat > /etc/apt/apt.conf.d/10no--check-valid-until

ARG MOBILITYDB_TAG

# 1. Install Prerequisites
# 2. Get, build and install MobilityDB
# 3. Uninstall prerequisites
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
build-essential \
cmake \
wget \
libproj-dev \
libjson-c-dev \
libgsl-dev \
libgeos-dev \
postgresql-server-dev-"${PG_MAJOR}" \
&& rm -rf /var/lib/apt/lists/* \
&& wget -O MobilityDB.tar.gz "https://github.com/MobilityDB/MobilityDB/archive/$MOBILITYDB_TAG.tar.gz" \
&& mkdir -p /usr/local/src/MobilityDB/build \
&& tar \
--extract \
--file MobilityDB.tar.gz \
--directory /usr/local/src/MobilityDB \
--strip-components 1 \
&& rm MobilityDB.tar.gz \
&& cd /usr/local/src/MobilityDB/build \
&& cmake .. \
&& make -j"$(nproc)" \
&& make install \
&& cp ../docker/initdb-mobilitydb.sh /docker-entrypoint-initdb.d/11_mobilitydb.sh \
&& cd / \
&& rm -rf /usr/local/src/MobilityDB \
&& apt-get purge -y --auto-remove \
build-essential \
cmake \
wget \
postgresql-server-dev-"${PG_MAJOR}"
26 changes: 26 additions & 0 deletions docker/initdb-mobilitydb.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash

set -e

echo "shared_preload_libraries = 'postgis-3.so'" >> "$PGDATA"/postgresql.conf

# Perform all actions as $POSTGRES_USER
export PGUSER="$POSTGRES_USER"

# Create the 'template_mobilitydb' template db
"${psql[@]}" <<- 'EOSQL'
CREATE DATABASE template_mobilitydb IS_TEMPLATE true;
EOSQL

# Load mobilitydb into both template_database and $POSTGRES_DB
# Since $POSTGRES_DB already has postgis, we must use
# LOAD 'postgis-3.so' to be able to create the mobilitydb extension
# (the shared_preload_libraries setting above has not taken effect yet)
for DB in template_mobilitydb "$POSTGRES_DB"; do
echo "Loading mobilitydb extensions into $DB"
"${psql[@]}" --dbname="$DB" <<-'EOSQL'
CREATE EXTENSION IF NOT EXISTS postgis;
LOAD 'postgis-3.so';
CREATE EXTENSION IF NOT EXISTS mobilitydb;
EOSQL
done
18 changes: 18 additions & 0 deletions docker/test_docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash
set -Eeo pipefail
# shellcheck disable=SC2043

# mobilitydb local build matrix
for mobilitydb_ver in develop ; do
for pg_major in 16 15 14 13 12 ; do
for postgis_ver in 3.4 ; do
docker build --pull --progress=plain \
--build-arg POSTGRES_VERSION=$pg_major \
--build-arg POSTGIS_VERSION=$postgis_ver \
--build-arg MOBILITYDB_TAG=$mobilitydb_ver \
-t testmobilitydb:${pg_major}-${postgis_ver}-${mobilitydb_ver} \
-f docker/Dockerfile\
.
done
done
done

0 comments on commit 80f7843

Please sign in to comment.