# How to setup a docker container with pg_routing 

By [i0rch](https://paragraph.com/@i0rch) · 2022-03-21

---

Dockerfile
----------

*   Choose base image and Install dependencies, create a `Dockerfile`:
    

    FROM postgres:12.8-bullseye
    
    ENV POSTGIS_MAJOR=3
    ENV POSTGIS_VERSION=3.1.4+dfsg-1.pgdg100+1
    
    RUN apt-get update \
        && apt-cache showpkg postgres-$PG_MAJOR-postgis-$POSTGIS_MAJOR \
        && apt-get install -y --no-install-recommends \
            postgresql-$PG_MAJOR-postgis-$POSTGIS_MAJOR \
            postgresql-$PG_MAJOR-postgis-$POSTGIS_MAJOR-scripts \
            postgresql-$PG_MAJOR-pgrouting \
        && rm -rf /var/lib/apt/list/*
    
    RUN mkdir -p /docker-entrypoint-initdb.d/
    COPY ./initdb-postgis.sh /docker-entrypoint-initdb.d/postgis.sh
    

*   Setup `postgres`+`postgis`+`pg_routing`, create a file `initdb-postgis.sh`:
    

    #!/bin/sh  
    
    set -e   
    
    ## Perform all actions as $POSTGRES_USER 
    export PGUSER="$POSTGRES_USER"  
    
    "${psql[@]}" <<-EOSQL 		
        CREATE DATABASE template_postgis;
        UPDATE pg_database SET datistemplate = TRUE 
            WHERE datname = 'template_postgis';
    EOSQL  
    
    ## Load PostGIS in both template_database and $POSTGRES_DB 
    for DB in template_postgis "$POSTGRES_DB"; do
        echo "Loading PostGIS extensions into $DB"  
        "${psql[@]}" --dbname="$DB" <<-EOSQL
            CREATE EXTENSION IF NOT EXISTS postgis;
            CREATE EXTENSION IF NOT EXISTS postgis_topology;
            CREATE EXTENSION IF NOT EXISTS pgRouting;
        EOSQL
    done
    

docker-compose
--------------

    version: '3'
    services:
      serv_pgrouting:
        build: .
        container_name: dc_pgrouting
        env_file: .env
        volumes:
          - ./db-data:/var/lib/postgresql/data
        ports:
          - 5433:5432
     
    volumes:
      db-data:
        driver: local
    

How to use it
-------------

---

*Originally published on [i0rch](https://paragraph.com/@i0rch/how-to-setup-a-docker-container-with-pg-routing)*
