Docker Desktop预填充PostGIS+OSM正常,Azure Pipeline构建后数据库为空
问题:PostGIS镜像在Azure Pipeline构建后数据库数据丢失
本地构建正常场景
在Windows笔记本手动执行以下Docker构建命令后,生成的PostGIS镜像可正常预填充OSM数据,Docker Desktop启动容器后数据库数据存在:
docker build -f Dockerfile --no-cache --tag my-postgis:16-3.4 \ --build-arg DOWNLOAD_URLS="https://download.geofabrik.de/europe/germany/berlin-latest.osm.pbf https://download.geofabrik.de/europe/germany/hamburg-latest.osm.pbf" \ --build-arg OSM_PASSWORD=osm_password \ --build-arg POSTGIS_TAG=16-3.4 .
Azure Pipeline构建异常场景
通过Azure Pipeline执行以下构建脚本,将镜像推送到ACR后,拉取镜像在Kubernetes中启动时,数据库文件夹为空:
- task: AzureCLI@2 displayName: Build and push PostGIS image inputs: azureSubscription: $(ArmConnection) scriptType: bash scriptLocation: inlineScript inlineScript: | image_tag='$(ContainerRegistry)/postgis-${{ parameters.OsmRegion }}:${{ parameters.PostGisTag }}' # delete old images to avoid the error "no space left on device" docker system prune --all --force docker build --file $(Build.SourcesDirectory)/src/Services/PostGIS/Dockerfile --no-cache --tag $image_tag --build-arg POSTGIS_TAG=${{parameters.PostGisTag}} --build-arg OSM_PASSWORD=$(OsmPassword) --build-arg DOWNLOAD_URLS="$(DownloadUrls)" $(Build.SourcesDirectory)/src/ # building the Docker image takes longer time, so be sure to acr login just before pushing the image az acr login -n $(ContainerRegistry) docker push $image_tag
背景说明
已知在docker build阶段启停服务并非推荐做法,但因生产环境使用28GB的europe-latest.osm.pbf,若在/docker-entrypoint-initdb.d中执行脚本会导致Pod启动耗时数小时,故采用当前构建时预填充数据的方案。相关Dockerfile参考postgis/docker-postgis模板编写,内容如下:
ARG POSTGIS_TAG FROM postgis/postgis:$POSTGIS_TAG EXPOSE 5432 # Install ps, top, netstat, curl, wget, osmium, osm2pgsql RUN apt-get update && \ apt-get upgrade -y && \ apt install -y procps net-tools curl wget osmium-tool osm2pgsql WORKDIR /data RUN chown -R postgres:postgres /data USER postgres # Download one or multiple OSM files ARG DOWNLOAD_URLS RUN wget $DOWNLOAD_URLS # Print summaries of the downloaded OSM files RUN for f in *.osm.pbf; do osmium fileinfo -e "$f"; done # Merge one or multiple files into a new file map.osm.pbf RUN osmium merge *.osm.pbf -o map.osm.pbf # Remove all files except the map.osm.pbf RUN find . -type f -name '*.osm.pbf' | grep -v 'map.osm.pbf' | xargs rm -f ARG OSM_PASSWORD ENV PGPASSWORD=$OSM_PASSWORD ENV PGUSER=osm_user ENV PGDATABASE=osm_database ENV PGDATA=/var/lib/postgresql/data # create PostgreSQL instance in the $PGDATA folder # configure PostgreSQL as recommended by osm2pgsql doc # start PostgreSQL and create database and user # load data from the map.osm.pbf file into the database # remove the map.osm.pbf file and stop PostgreSQL # configure password based access for osm_user RUN set -eux && \ pg_ctl init && \ echo "shared_buffers = 1GB" >> $PGDATA/postgresql.conf && \ echo "work_mem = 50MB" >> $PGDATA/postgresql.conf && \ echo "maintenance_work_mem = 10GB" >> $PGDATA/postgresql.conf && \ echo "autovacuum_work_mem = 2GB" >> $PGDATA/postgresql.conf && \ echo "wal_level = minimal" >> $PGDATA/postgresql.conf && \ echo "checkpoint_timeout = 60min" >> $PGDATA/postgresql.conf && \ echo "max_wal_size = 10GB" >> $PGDATA/postgresql.conf && \ echo "checkpoint_completion_target = 0.9" >> $PGDATA/postgresql.conf && \ echo "max_wal_senders = 0" >> $PGDATA/postgresql.conf && \ echo "random_page_cost = 1.0" >> $PGDATA/postgresql.conf && \ echo "password_encryption = scram-sha-256" >> $PGDATA/postgresql.conf && \ pg_ctl start && \ createuser --username=postgres $PGUSER && \ createdb --username=postgres --encoding=UTF8 --owner=$PGUSER $PGDATABASE && \ psql --username=postgres $PGDATABASE --command="ALTER USER $PGUSER WITH PASSWORD '$PGPASSWORD';" && \ psql --username=postgres $PGDATABASE --command='CREATE EXTENSION IF NOT EXISTS postgis;' && \ psql --username=postgres $PGDATABASE --command='CREATE EXTENSION IF NOT EXISTS hstore;' && \ osm2pgsql --username=$PGUSER --database=$PGDATABASE --create --cache=60000 --hstore --latlong /data/map.osm.pbf && \ rm -f /data/map.osm.pbf && \ pg_ctl stop && \ echo '# TYPE DATABASE USER ADDRESS METHOD' > $PGDATA/pg_hba.conf && \ echo "local all postgres peer" >> $PGDATA/pg_hba.conf && \ echo "local $PGDATABASE $PGUSER scram-sha-256" >> $PGDATA/pg_hba.conf && \ echo "host $PGDATABASE $PGUSER 0.0.0.0/0 scram-sha-256" >> $PGDATA/pg_hba.conf
该问题已同步至docker-postgis的GitHub讨论区。
内容的提问来源于stack exchange,提问作者Alexander Farber
相关产品推荐
相关产品推荐

