こんにちはDockerを使用してPOSTGISをDjangoに接続する方法を教えてください。エラーが発生しましたcould not open extension control file "/usr/share/postgresql/10/extension/postgis.control": No such file or directory
出力
root@localhost:~/try-geodjango# docker-compose run web python manage.py migrate
Starting try-geodjango_db_1 ... done
/usr/local/lib/python3.5/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
""")
Traceback (most recent call last):
File "/usr/local/lib/python3.5/site-packages/Django/db/backends/utils.py", line 62, in execute
return self.cursor.execute(sql)
psycopg2.OperationalError: could not open extension control file "/usr/share/postgresql/10/extension/postgis.control": No such file or directory
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.5/site-packages/Django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.5/site-packages/Django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.5/site-packages/Django/core/management/base.py", line 294, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.5/site-packages/Django/core/management/base.py", line 345, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.5/site-packages/Django/core/management/commands/migrate.py", line 81, in handle
connection.prepare_database()
File "/usr/local/lib/python3.5/site-packages/Django/contrib/gis/db/backends/postgis/base.py", line 25, in prepare_database
cursor.execute("CREATE EXTENSION IF NOT EXISTS postgis")
File "/usr/local/lib/python3.5/site-packages/Django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/usr/local/lib/python3.5/site-packages/Django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.5/site-packages/Django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/local/lib/python3.5/site-packages/Django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/usr/local/lib/python3.5/site-packages/Django/db/backends/utils.py", line 62, in execute
return self.cursor.execute(sql)
Django.db.utils.OperationalError: could not open extension control file "/usr/share/postgresql/10/extension/postgis.control": No such file or directory
settings.py
DATABASES = {
'default': {
'ENGINE': 'Django.contrib.gis.db.backends.postgis',
'NAME': 'postgres',
'USER': 'postgres',
'Host': 'db',
'PORT': '5432',
'CONN_MAX_AGE': 300
}
}
docker-compose.yml
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:9.4
environment:
- POSTGRES_Host=db
- POSTGRES_PORT=5432
- POSTGRES_NAME=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=password
Dockerfile
FROM evili/geodjango
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -U pip
RUN pip install -r requirements.txt
ADD . /code/
正しいpostgis画像を使用していないという問題
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
Postgisを操作するための私のAlpineベースの構成は次のとおりです
注:すべての私のDjangoコードは./backend
フォルダーに保存されており、私はPipenv
を使用しています。また、このプロジェクトは https://github.com/joke2k/Django-environ 設定でDATABASE_URL
を介してDATABASES
を設定します
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
ここに詳述されているpostgisのインストール手順に従って、ubuntu 18.04でこの問題を解決しました: https://docs.djangoproject.com/en/2.1/ref/contrib/gis/install/postgis/
簡単に言えば、次を実行します:Sudo apt install postgis
。
ここで詳しく説明されているコマンドを実行する必要がある場合もあります。 https://docs.djangoproject.com/en/2.1/ref/contrib/gis/install/spatialite/
私は流暢なint Dockerではないので、この理論をそれに応じて翻訳してください。