Merge pull request #2409 from jalessio/docker-compose-take2
authorAndy Allan <git@gravitystorm.co.uk>
Wed, 3 Feb 2021 18:07:29 +0000 (18:07 +0000)
committerGitHub <noreply@github.com>
Wed, 3 Feb 2021 18:07:29 +0000 (18:07 +0000)
Add Docker Compose Support for Development Environment

12 files changed:
.dockerignore [new file with mode: 0644]
.github/workflows/docker.yml [new file with mode: 0644]
.gitignore
DOCKER.md [new file with mode: 0644]
Dockerfile [new file with mode: 0644]
INSTALL.md
config/boot.rb
config/docker.database.yml [new file with mode: 0644]
docker-compose.yml [new file with mode: 0644]
docker/null-island.osm.xml [new file with mode: 0644]
docker/postgres/Dockerfile [new file with mode: 0644]
docker/postgres/openstreetmap-postgres-init.sh [new file with mode: 0755]

diff --git a/.dockerignore b/.dockerignore
new file mode 100644 (file)
index 0000000..e3e5e1b
--- /dev/null
@@ -0,0 +1,6 @@
+docker-db-data
+log
+tmp
+docker-cache
+.travis.yml
+.git
diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml
new file mode 100644 (file)
index 0000000..852e62c
--- /dev/null
@@ -0,0 +1,36 @@
+name: Docker
+on:
+  - push
+  - pull_request
+jobs:
+  test:
+    name: Docker
+    runs-on: ubuntu-20.04
+    steps:
+    - name: Checkout source
+      uses: actions/checkout@v1
+    - name: Poke config
+      run: |
+        cp config/example.storage.yml config/storage.yml
+        cp config/docker.database.yml config/database.yml
+        touch config/settings.local.yml
+    - name: Build Docker Image
+      run: |
+        docker-compose build
+    - name: Start Docker-Compose
+      run: |
+        docker-compose up -d
+        sleep 15 # let the DB warm up a little
+    - name: Prepare Database
+      run: |
+        docker-compose run --rm web rake db:migrate
+        docker-compose run web bundle exec rake i18n:js:export
+        docker-compose run --rm web osmosis --rx docker/null-island.osm.xml --wd host=db database=openstreetmap user=openstreetmap password=openstreetmap validateSchemaVersion=no
+    - name: Test Basic Website
+      run: |
+        curl -siL http://127.0.0.1:3000 | egrep '^HTTP/1.1 200 OK'
+        curl -siL http://127.0.0.1:3000 | grep 'OpenStreetMap is the free wiki world map'
+        curl -siL http://127.0.0.1:3000/api/0.6/node/1 | grep 'Null Island'
+    - name: Test Complete Suite
+      run: |
+        docker-compose run --rm web bundle exec rails test:db
index 1328658c6ab5b70d2809f6c665e51ff05b447514..73316769e63ae7989ff9507682a1011cdf9db21a 100644 (file)
@@ -18,3 +18,6 @@ public/attachments
 public/export
 storage
 tmp
+
+# docker-compose database directory
+docker-db-data
diff --git a/DOCKER.md b/DOCKER.md
new file mode 100644 (file)
index 0000000..d5a673b
--- /dev/null
+++ b/DOCKER.md
@@ -0,0 +1,98 @@
+# Using Docker and Docker Compose for Development and Testing
+
+These instructions are designed for setting up The Rails Port for development and testing using [Docker](https://www.docker.com/). This will allow you to install the OpenStreetMap application and all its dependencies in Docker images and then run them in containers, almost with a single command. You will need to install Docker and Docker Compose on your development machine:
+
+- [Install Docker](https://docs.docker.com/install/)
+- [Install Docker Compose](https://docs.docker.com/compose/install/)
+
+The first step is to fork/clone the repo to your local machine:
+
+    git clone https://github.com/openstreetmap/openstreetmap-website.git
+
+Now change working directory to the `openstreetmap-website`:
+
+    cd openstreetmap-website
+
+## Initial Setup
+
+### Storage
+
+    cp config/example.storage.yml config/storage.yml
+
+### Database
+
+    cp config/docker.database.yml config/database.yml
+
+## Prepare local settings file
+
+This is a workaround. [See issues/2185 for details](https://github.com/openstreetmap/openstreetmap-website/issues/2185#issuecomment-508676026).
+
+    touch config/settings.local.yml
+
+## Installation
+
+To build local Docker images run from the root directory of the repository:
+
+    docker-compose build
+
+If this is your first time running or you have removed cache this will take some time to complete. Once the Docker images have finished building you can launch the images as containers.
+
+To launch the app run:
+
+    docker-compose up -d
+
+This will launch one Docker container for each 'service' specified in `docker-compose.yml` and run them in the background. There are two options for inspecting the logs of these running containers:
+
+- You can tail logs of a running container with a command like this: `docker-compose logs -f web` or `docker-compose logs -f db`.
+- Instead of running the containers in the background with the `-d` flag, you can launch the containers in the foreground with `docker-compose up`. The downside of this is that the logs of all the 'services' defined in `docker-compose.yml` will be intermingled. If you don't want this you can mix and match - for example, you can run the database in background with `docker-compose up -d db` and then run the Rails app in the foreground via `docker-compose up web`.
+
+### Migrations
+
+Run the Rails database migrations:
+
+    docker-compose run --rm web bundle exec rake db:migrate
+
+### Tests
+
+Run the test suite by running:
+
+    docker-compose run --rm web bundle exec rake test:db
+
+### Loading an OSM extract
+
+This installation comes with no geographic data loaded. You can either create new data using one of the editors (Potlatch 2, iD, JOSM etc) or by loading an OSM extract. Here an example for loading an OSM extract into your Docker-based OSM instance.
+
+For example, let's download the District of Columbia from Geofabrik or [any other region](https://download.geofabrik.de):
+
+    wget https://download.geofabrik.de/north-america/us/district-of-columbia-latest.osm.pbf
+
+You can now use Docker to load this extract into your local Docker-based OSM instance:
+
+    docker-compose run --rm web osmosis \
+        -verbose    \
+        --read-pbf district-of-columbia-latest.osm.pbf \
+        --write-apidb \
+            host="db" \
+            database="openstreetmap" \
+            user="openstreetmap" \
+            validateSchemaVersion="no"
+
+Once you have data loaded for Washington, DC you should be able to navigate to [`http://localhost:3000/#map=12/38.8938/-77.0146`](http://localhost:3000/#map=12/38.8938/-77.0146) to begin working with your local instance.
+
+### Additional Configuration
+
+See [`CONFIGURE.md`](CONFIGURE.md) for information on how to manage users and enable OAuth for iD, JOSM etc.
+
+### Bash
+
+If you want to get into a web container and run specific commands you can fire up a throwaway container to run bash in via:
+
+    docker-compose run --rm web bash
+
+Alternatively, if you want to use the already-running `web` container then you can `exec` into it via:
+
+    docker-compose exec web bash
+
+Similarly, if you want to `exec` in the db container use:
+
+    docker-compose exec db bash
diff --git a/Dockerfile b/Dockerfile
new file mode 100644 (file)
index 0000000..33812d2
--- /dev/null
@@ -0,0 +1,50 @@
+FROM ubuntu:20.04
+
+ENV DEBIAN_FRONTEND=noninteractive
+
+# Install system packages
+RUN apt-get update \
+ && apt-get install --no-install-recommends -y \
+      build-essential \
+      curl \
+      default-jre-headless \
+      file \
+      firefox-geckodriver \
+      imagemagick \
+      libarchive-dev \
+      libffi-dev \
+      libgd-dev \
+      libmagickwand-dev \
+      libpq-dev \
+      libsasl2-dev \
+      libxml2-dev \
+      libxslt1-dev \
+      locales \
+      nodejs \
+      postgresql-client \
+      ruby2.7 \
+      ruby2.7-dev \
+      tzdata \
+      unzip \
+      yarnpkg \
+ && apt-get clean \
+ && rm -rf /var/lib/apt/lists/*
+
+# Install current Osmosis
+RUN curl -OL https://github.com/openstreetmap/osmosis/releases/download/0.47.2/osmosis-0.47.2.tgz \
+ && tar -C /usr/local -xzf osmosis-0.47.2.tgz
+
+ENV DEBIAN_FRONTEND=dialog
+
+# Setup app location
+RUN mkdir -p /app
+WORKDIR /app
+
+# Install Ruby packages
+ADD Gemfile Gemfile.lock /app/
+RUN gem install bundler \
+ && bundle install
+
+# Install NodeJS packages
+ADD package.json yarn.lock /app/
+RUN yarnpkg install
index 49f4a78d03d30d62760120e9a236b0156ea5c948..c8811fc3fcc54ccff2068765268a75b7438660b8 100644 (file)
@@ -3,7 +3,11 @@
 These instructions are designed for setting up The Rails Port for development and testing.
 If you want to deploy the software for your own project, then see the notes at the end.
 
-You can install the software directly on your machine, which is the traditional and probably best-supported approach. However, there is an alternative which may be easier: Vagrant. This installs the software into a virtual machine, which makes it easier to get a consistent development environment and may avoid installation difficulties. For Vagrant instructions, see [VAGRANT.md](VAGRANT.md).
+You can install the software directly on your machine, which is the traditional and probably best-supported approach. However, there
+are two alternatives which make it easier to get a consistent development environment and may avoid installation difficulties:
+
+* **Vagrant** This installs the software into a virtual machine. For Vagrant instructions see [VAGRANT.md](VAGRANT.md).
+* **Docker** This installs the software using containerization. For Docker instructions see [DOCKER.md](DOCKER.md).
 
 These instructions are based on Ubuntu 20.04 LTS, which is the platform used by the OSMF servers.
 The instructions also work, with only minor amendments, for all other current Ubuntu releases, Fedora and MacOSX
index 988a5ddc460f03c2cafb3b91d98f0cc930ebc8ed..630904bfcc63fc033b45834ce853223dc2d055c8 100644 (file)
@@ -1,4 +1,4 @@
 ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
 
 require "bundler/setup" # Set up gems listed in the Gemfile.
-require "bootsnap/setup" # Speed up boot time by caching expensive operations.
+require "bootsnap/setup" if ENV.fetch("ENABLE_BOOTSNAP", "true") == "true" # Speed up boot time by caching expensive operations.
diff --git a/config/docker.database.yml b/config/docker.database.yml
new file mode 100644 (file)
index 0000000..29a3961
--- /dev/null
@@ -0,0 +1,20 @@
+# This configuration is tailored for use with docker-compose. See DOCKER.md for more information.
+
+development:
+  adapter: postgresql
+  database: openstreetmap
+  username: openstreetmap
+  password: openstreetmap
+  host: db
+  encoding: utf8
+
+# Warning: The database defined as 'test' will be erased and
+# re-generated from your development database when you run 'rake'.
+# Do not set this db to the same as development or production.
+test:
+  adapter: postgresql
+  database: osm_test
+  username: openstreetmap
+  password: openstreetmap
+  host: db
+  encoding: utf8
diff --git a/docker-compose.yml b/docker-compose.yml
new file mode 100644 (file)
index 0000000..1111ad0
--- /dev/null
@@ -0,0 +1,40 @@
+version: "3"
+
+services:
+  web:
+    build:
+      context: .
+    volumes:
+      - .:/app
+      # Prevent these directories from mounting so they're not shared between host OS and Docker
+      - /app/node_modules/
+      - /app/tmp/
+      # Mount these upload directories so they persist between runs
+      - web-traces:/home/osm/traces
+      - web-images:/home/osm/images
+    ports:
+      - "3000:3000"
+    environment:
+      # https://github.com/Shopify/bootsnap/issues/262
+      ENABLE_BOOTSNAP: 'false'
+    command: bundle exec rails s -p 3000 -b '0.0.0.0'
+    depends_on:
+      - db
+
+  db:
+    build:
+      context: .
+      dockerfile: docker/postgres/Dockerfile
+    ports:
+      - "54321:5432"
+    environment:
+      POSTGRES_HOST_AUTH_METHOD: trust
+      POSTGRES_DB: openstreetmap
+    volumes:
+      # Mount the Postgres data directory so it persists between runs
+      - db-data:/var/lib/postgresql/data
+
+volumes:
+  web-traces:
+  web-images:
+  db-data:
diff --git a/docker/null-island.osm.xml b/docker/null-island.osm.xml
new file mode 100644 (file)
index 0000000..96edeab
--- /dev/null
@@ -0,0 +1,7 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<osm version="0.6" generator="Osmosis 0.47.2">
+  <bounds minlon="-180.00000" minlat="-90.00000" maxlon="180.00000" maxlat="90.00000" origin="Osmosis 0.47.2"/>
+  <node id="1" version="1" timestamp="1970-01-01T00:00:00Z" uid="1" user="nobody" lat="0" lon="0">
+    <tag k="name" v="Null Island"/>
+  </node>
+</osm>
diff --git a/docker/postgres/Dockerfile b/docker/postgres/Dockerfile
new file mode 100644 (file)
index 0000000..9faddd8
--- /dev/null
@@ -0,0 +1,7 @@
+FROM postgres:11
+
+# Add db init script to install OSM-specific Postgres functions/extensions.
+ADD docker/postgres/openstreetmap-postgres-init.sh /docker-entrypoint-initdb.d/
+
+# Custom database functions are in a SQL file.
+ADD db/functions/functions.sql /usr/local/sbin/osm-db-functions.sql
diff --git a/docker/postgres/openstreetmap-postgres-init.sh b/docker/postgres/openstreetmap-postgres-init.sh
new file mode 100755 (executable)
index 0000000..3973311
--- /dev/null
@@ -0,0 +1,14 @@
+#!/bin/bash
+set -ex
+
+# Create 'openstreetmap' user
+psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" <<-EOSQL
+    CREATE USER openstreetmap SUPERUSER PASSWORD 'openstreetmap';
+    GRANT ALL PRIVILEGES ON DATABASE openstreetmap TO openstreetmap;
+EOSQL
+
+# Create btree_gist extensions
+psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -c "CREATE EXTENSION btree_gist" openstreetmap
+
+# Define custom functions
+psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -f "/usr/local/sbin/osm-db-functions.sql" openstreetmap