Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
build: optimise osm dockerfile for production build
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Aug 20, 2024
1 parent 0308e90 commit 308b885
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 33 deletions.
71 changes: 46 additions & 25 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
ARG OSM_COMMIT_DATE=2024-04-29
ARG OSM_COMMIT=183e5c6d67820db1a8740bfda6b3af0b0c0de554
ARG OSM_COMMIT_SHA=99a7d21a9bc50cdb38559dd0a8b60bc60072b5a1



FROM docker.io/ruby:3.3.0-slim-bookworm as openstreetmap-repo
RUN set -ex \
&& apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install \
-y --no-install-recommends \
"git" \
"ca-certificates" \
"curl" \
"unzip" \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /repo
RUN update-ca-certificates
ARG OSM_COMMIT
ARG OSM_COMMIT_DATE
RUN git clone --branch master --shallow-since="${OSM_COMMIT_DATE}" \
https://github.com/openstreetmap/openstreetmap-website.git \
&& cd openstreetmap-website && git checkout "${OSM_COMMIT}"
WORKDIR /openstreetmap-website
ARG OSM_COMMIT_SHA
RUN curl -L \
"https://github.com/openstreetmap/openstreetmap-website/archive/${OSM_COMMIT_SHA}.zip" \
--output website.zip \
&& unzip website.zip \
&& mv openstreetmap-website-$OSM_COMMIT_SHA/* /openstreetmap-website/ \
&& rm website.zip



FROM docker.io/ruby:3.3.0-slim-bookworm as build
ENV RAILS_ENV=production
ENV DEBIAN_FRONTEND=noninteractive
RUN set -ex \
&& apt-get update \
Expand All @@ -47,32 +49,42 @@ RUN set -ex \
"libxml2-dev" \
"libxslt1-dev" \
"libyaml-dev" \
"python3-pip" \
&& rm -rf /var/lib/apt/lists/* \
&& npm install --global yarn
WORKDIR /app
COPY --from=openstreetmap-repo \
/repo/openstreetmap-website/ /app/
/openstreetmap-website/ /app/
# Install Ruby packages
RUN bundle config set --global path /usr/local/bundle \
&& bundle install \
# Install NodeJS packages using yarn
&& bundle exec bin/yarn install
WORKDIR /importer
COPY importer /importer
RUN pip3 install --break-system-packages pdm==2.15.1
RUN pdm install
# A dummy config is required for precompile step below
RUN cp config/example.database.yml config/database.yml \
&& touch config/settings.local.yml \
&& echo "#session key \n\
production: \n\
secret_key_base: $(bundle exec bin/rails secret)" > config/secrets.yml \
# Precompile assets for faster initial load
&& bundle exec bin/rails i18n:js:export assets:precompile
# Package svgo dependency required by image_optim into node single file exe
RUN \
mkdir /bins && cd /bins \
# TODO update this to use node@21 single file executable?
&& npm install -g svgo @yao-pkg/pkg \
&& pkg -t node18-linux /usr/local/bin/svgo



FROM docker.io/ruby:3.3.0-slim-bookworm as runtime
ARG OSM_COMMIT
ARG OSM_COMMIT_SHA
ARG GIT_COMMIT
LABEL org.hotosm.osm-sandbox.app-name="osm" \
org.hotosm.osm-sandbox-version="${OSM_COMMIT}" \
org.hotosm.osm-sandbox-version="${OSM_COMMIT_SHA}" \
org.hotosm.osm-sandbox-commit-ref="${COMMIT_REF:-none}" \
org.hotosm.osm-sandbox="[email protected]"
ENV PIDFILE=/tmp/pids/server.pid
ENV RAILS_ENV=production \
PIDFILE=/tmp/pids/server.pid
RUN set -ex \
&& apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install \
Expand All @@ -90,6 +102,15 @@ RUN set -ex \
"libxml2-dev" \
"libxslt1-dev" \
"libyaml-dev" \
# Required image optimisation libraries in OSM prod
"advancecomp" \
"gifsicle" \
"libjpeg-progs" \
"jhead" \
"jpegoptim" \
"optipng" \
"pngcrush" \
"pngquant" \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY --from=build /app/Gemfile* /app/Rakefile /app/config.ru /app/
Expand All @@ -103,16 +124,16 @@ COPY --from=build /app/lib /app/lib
COPY --from=build /app/public /app/public
COPY --from=build /app/script /app/script
COPY --from=build /app/vendor /app/vendor
# Python libs and script
COPY --from=build /importer/.venv/lib/python3.11/site-packages /usr/local/lib/python3.11/dist-packages
COPY importer/importer.py /app/importer.py
COPY osm-entrypoint.sh /
RUN bundle config set --global path /usr/local/bundle \
# Copy svgo requirement as single file executable
COPY --from=build /bins/svgo /usr/local/bin/svgo
RUN \
bundle config set --global path /usr/local/bundle \
# Copy the required config to correct location
# https://github.com/openstreetmap/openstreetmap-website/blob/master/DOCKER.md#initial-setup
&& cp config/example.storage.yml config/storage.yml \
&& cp config/docker.database.yml config/database.yml \
# Replace db --> osm-db compose service
# Replace db --> osm-db compose network service name
&& sed -i 's/host: db/host: osm-db/' config/database.yml \
&& touch config/settings.local.yml \
&& chmod +x /osm-entrypoint.sh
Expand Down
35 changes: 27 additions & 8 deletions osm-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
#!/bin/bash

# First start web server & run migrations
# Add db creds to production setup
echo " # Production DB
production:
adapter: postgresql
host: ${POSTGRES_HOST:-osm-db}
database: ${POSTGRES_DB:-openstreetmap}
username: ${POSTGRES_USER:-openstreetmap}
password: ${POSTGRES_PASSWORD:-openstreetmap}
encoding: utf8" >config/database.yml

# Start web server & run migrations
echo ""
echo "------------------"
echo "Running migrations"
echo "------------------"
echo ""
bundle exec rails s -d -p 3000 -b '0.0.0.0'
bundle exec rails db:migrate

echo ""
echo "-------------------"
echo "Creating OAuth apps"
echo "-------------------"
echo ""
# Ruby script to create admin (to file)
# NOTE ID_EDITOR_REDIRECT_URI env var is injected
cat << EOF > create_admin_user.rb
Expand Down Expand Up @@ -103,12 +123,11 @@ echo
echo "Admin OAuth Token: $ADMIN_OAUTH_TOKEN"
echo

if [ -n "$IMPORT_BBOX" ] && [ -n "${IMPORT_BBOX}" ]; then
sed -i "s/OSM_CLIENT_ID=\"GmZNCPz5j7HgTOMzmw94lrsCpnzbtuorgqsYxzxRa2w\"/OSM_CLIENT_ID=\"${OSM_CLIENT_ID}\"/" /app/importer.py
sed -i "s/OSM_CLIENT_SECRET=\"c2c18c031e6d647e1e02dee103f9bbca5befdf369001439fc2c7f2a820c89e56\"/OSM_CLIENT_SECRET=\"${OSM_CLIENT_SECRET}\"/" /app/importer.py
sed -i "s/OSM_ACCESS_TOKEN=\"_uEeRxVawGHSOtIhvb_wS1dAwCL0YALQ0zlMAmVG7-Y\"/OSM_ACCESS_TOKEN=\"${OSM_ACCESS_TOKEN}\"/" /app/importer.py

python3 /app/importer.py "$IMPORT_BBOX"
fi
# This starts a background worker
echo ""
echo "-------------------"
echo "Running command: $@"
echo "-------------------"
echo ""

exec "$@"

0 comments on commit 308b885

Please sign in to comment.