17👍
NOTE: There is an official image for postgis now https://hub.docker.com/r/postgis/postgis. Use that instead. Also in example below I am using alpine which is not recommended as python base due to issues with wheels (read more https://pythonspeed.com/articles/alpine-docker-python/)
Problem here that you are not using correct postgis image
version: '3'
services:
web:
build: .
command: python3 manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
db:
image: mdillon/postgis:10 # <- here
environment:
- POSTGRES_HOST=db # default postgres
- POSTGRES_PASSWORD=password # default postgres
# all env vars bellow are redundant because they are same as default values
# - POSTGRES_PORT=5432
# - POSTGRES_NAME=postgres
# - POSTGRES_USER=postgres
Here is my alpine based config for working with postgis
NOTE: all my django code is stored in ./backend
folder and I use Pipenv
, also
this project is using https://github.com/joke2k/django-environ to set DATABASES
via DATABASE_URL
in my settings
DATABASES = {
'default': env.db(default='postgis://postgres:postgres@postgres:5432/postgres')
}
docker-compose.yml
version: "3.7"
services:
postgres:
image: mdillon/postgis:10-alpine
volumes:
- ./docker/postgres/:/docker-entrypoint-initdb.d/
- postgres_data:/var/lib/postgresql/data
backend:
build:
context: .
dockerfile: docker/backend/Dockerfile
ports:
- "8000:8000"
depends_on:
- postgres
volumes:
- ./backend:/project/backend
volumes:
postgres_data:
docker/backend/Dockerfile
FROM python:3.6-alpine3.8
# postgresql-client is required by psql
# postgresql-dev musl-dev gcc are required by psycopg
# NOTE: there is py3-psycopg2
# libxml2-dev libxslt-dev are required by lxml
# gdal-dev geos-dev proj4-dev are required by geodjango
# libcrypto1.1 is required by gdal
# NOTE: we actually need gdal-dev not gdal
# linux-headers is required by uwsgi
# gettext-dev is required by ./manage.py makemessages
# TODO: optimize installation by using --virtual
RUN apk update && apk upgrade \
&& apk add postgresql-client \
postgresql-dev \
musl-dev \
gcc \
libxml2-dev \
libxslt-dev \
linux-headers \
gettext-dev \
&& apk add --repository http://dl-cdn.alpinelinux.org/alpine/edge/main \
libcrypto1.1 \
&& apk add --repository http://dl-cdn.alpinelinux.org/alpine/edge/testing \
gdal-dev \
geos-dev \
proj4-dev \
&& pip install pipenv
ENV PYTHONUNBUFFERED=1 \
PYTHONIOENCODING=UTF-8
COPY docker/backend/docker-entrypoint.sh /
ENTRYPOINT ["/docker-entrypoint.sh"]
WORKDIR /project/backend
COPY backend/Pipfile backend/Pipfile.lock /project/backend/
RUN pipenv install --system --ignore-pipfile --dev
docker/backend/docker-entrypoint.sh
#!/bin/sh
# NOTE: if there is no bash can cause
# standard_init_linux.go:190: exec user process caused "no such file or directory"
# https://docs.docker.com/compose/startup-order/
set -euo pipefail
WAIT_FOR_POSTGRES=${WAIT_FOR_POSTGRES:-true}
if [[ "$WAIT_FOR_POSTGRES" = true ]]; then
DATABASE_URL=${DATABASE_URL:-postgres://postgres:postgres@postgres:5432/postgres}
# convert to connection string
# https://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-CONNSTRING
POSTGRES_URL=${DATABASE_URL%%\?*}
# https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion
POSTGRES_URL=${POSTGRES_URL/#postgis:/postgres:}
# let postgres and other services (e.g. elasticsearch) to warm up...
# https://www.caktusgroup.com/blog/2017/03/14/production-ready-dockerfile-your-python-django-app/
until psql $POSTGRES_URL -c '\q'; do
>&2 echo "Postgres is not available - sleeping"
sleep 1
done
# >&2 echo "Postgres is up - executing command"
fi
if [[ $# -ge 1 ]]; then
exec "$@"
else
echo "Applying migrations"
python manage.py migrate --noinput -v 0
echo "Generate translations"
python manage.py compilemessages --locale ru -v 0
echo "Starting server"
exec python manage.py runserver 0.0.0.0:8000
fi
0👍
I solved this problem on ubuntu 18.04 by following the installation instructions for postgis detailed here: https://docs.djangoproject.com/en/2.1/ref/contrib/gis/install/postgis/
In a nutshell, run: sudo apt install postgis
.
You may also need to execute commands detailed here: https://docs.djangoproject.com/en/2.1/ref/contrib/gis/install/spatialite/
I’m not fluent int docker, so translate this theory accordingly.
- [Django]-How to move a model between two Django apps (Django 1.7)
- [Django]-How to view database and schema of django sqlite3 db
- [Django]-Migrating existing auth.User data to new Django 1.5 custom user model?