From b1f90c5219f3d103e9d9d9942230861de3203327 Mon Sep 17 00:00:00 2001 From: Paul Sturgess Date: Fri, 16 Sep 2016 00:18:16 +0100 Subject: [PATCH] Enable the app to be built using Docker --- .dockerignore | 6 ++++ DOCKER.md | 67 +++++++++++++++++++++++++++++++++++++++++++ Dockerfile | 35 ++++++++++++++++++++++ Dockerfile.postgres | 11 +++++++ INSTALL.md | 4 ++- db/docker_postgres.sh | 7 +++++ docker-compose.yml | 23 +++++++++++++++ 7 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 .dockerignore create mode 100644 DOCKER.md create mode 100644 Dockerfile create mode 100644 Dockerfile.postgres create mode 100755 db/docker_postgres.sh create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000000..51cd8ff5e6 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,6 @@ +* +!Gemfile +!Gemfile.lock +!db/functions +!db/docker_postgres.sh +!lib/quad_tile diff --git a/DOCKER.md b/DOCKER.md new file mode 100644 index 0000000000..a84e6a6dac --- /dev/null +++ b/DOCKER.md @@ -0,0 +1,67 @@ +# Using Docker to run OpenStreetMap + +Using [Docker](https://www.docker.com/) will allow you to install the OpenStreetMap application and all its' dependencies in a container, almost with a single command. + +These instructions gloss over the precise details of the dependencies and their configuration but these can be found in full detail at [INSTALL.md](INSTALL.md). + +The first step is to fork/clone the repo to your local machine. Then run these commands: + +### App configuration + +``` +cp config/example.application.yml config/application.yml +``` + +### Database + +``` +cp config/example.database.yml config/database.yml +``` + +Set `username` to postgres and `host` to db leave the password blank + +### Installation + +In the root directory run: + +``` +docker-compose up +``` + +### Migrations + +``` +docker-compose exec web bundle exec rake db:migrate +``` + +Once these are complete you should be able to visit the app at http://localhost:3000 + +If localhost does not work, you can use the IP address of the docker-machine. + +### Tests + +``` +docker-compose exec web bundle exec rake test:db +``` + +### Bash + +If you want to get onto the web container and run specific commands you can fire up bash via: + +``` +docker-compose exec web /bin/bash +``` + +Similarly, if you want to get onto the db container use: + +``` +docker-compose exec db /bin/bash +``` + +### General Information + +The [docker-compose.yml](docker-compose.yml) specifies the configuration for the two web and db containers. For example port that the Postgres database is exposed on that you can point your local db admin tool at. + +Note that the [Dockerfile.postgres](Dockerfile.postgres) for the db container includes various build tools to run [db/docker_postgres.sh](db/docker_postgres.sh). This script installs extensions and functions required by the database and it is run automatically because it is specifically added to the location `docker-entrypoint-initdb.d` on the container. + +There is a [.dockerignore](.dockerignore) that ignores all files except those required to be added to the containers. diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000000..df5b4d734d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,35 @@ +FROM ruby:2.3-slim +MAINTAINER OpenStreetMap +ENV REFRESHED_AT 2016-09-15 + +# Install packages +RUN apt-get update +RUN apt-get install -y --no-install-recommends build-essential +RUN apt-get install -y --no-install-recommends ruby-dev +RUN apt-get install -y --no-install-recommends libxml2-dev +RUN apt-get install -y --no-install-recommends libxslt1-dev +RUN apt-get install -y --no-install-recommends libpq-dev +RUN apt-get install -y --no-install-recommends libsasl2-dev +RUN apt-get install -y --no-install-recommends imagemagick +RUN apt-get install -y --no-install-recommends libmagickwand-dev +RUN apt-get install -y --no-install-recommends nodejs +RUN apt-get install -y --no-install-recommends file +RUN apt-get install -y --no-install-recommends postgresql-client +RUN apt-get install -y --no-install-recommends locales +RUN apt-get clean && rm -rf /var/lib/apt/lists/* + +# Setup app location +RUN mkdir -p /app +WORKDIR /app + +# Install gems +ADD Gemfile /app/Gemfile +ADD Gemfile.lock /app/Gemfile.lock +RUN bundle install + +RUN sed -i -e 's/# en_GB.UTF-8 UTF-8/en_GB.UTF-8 UTF-8/' /etc/locale.gen && \ + echo 'LANG="en_GB.UTF-8"'>/etc/default/locale && \ + dpkg-reconfigure --frontend=noninteractive locales && \ + update-locale LANG=en_GB.UTF-8 + +ENV LANG en_GB.UTF-8 diff --git a/Dockerfile.postgres b/Dockerfile.postgres new file mode 100644 index 0000000000..371f1207b6 --- /dev/null +++ b/Dockerfile.postgres @@ -0,0 +1,11 @@ +FROM postgres:9.4 + +ADD db/docker_postgres.sh docker-entrypoint-initdb.d/docker_postgres.sh +ADD db/functions/ db/functions/ +ADD lib/quad_tile/ lib/quad_tile/ + +RUN apt-get update +RUN apt-get install -y make \ + postgresql-server-dev-all \ + build-essential \ + && apt-get clean && rm -rf /var/lib/apt/lists/* diff --git a/INSTALL.md b/INSTALL.md index 53a7838461..c9136417e0 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -3,7 +3,9 @@ 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 following the instructions below, which is the traditional and probably best-supported approach. + +Alternatively there are guides to use either [Vagrant](VAGRANT.md) or [Docker](DOCKER.md). These instructions are based on Ubuntu 12.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 diff --git a/db/docker_postgres.sh b/db/docker_postgres.sh new file mode 100755 index 0000000000..9da4956ec4 --- /dev/null +++ b/db/docker_postgres.sh @@ -0,0 +1,7 @@ +#!/bin/bash +set -e +psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -c "CREATE EXTENSION btree_gist" openstreetmap +make -C db/functions libpgosm.so +psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -c "CREATE FUNCTION maptile_for_point(int8, int8, int4) RETURNS int4 AS '/db/functions/libpgosm', 'maptile_for_point' LANGUAGE C STRICT" openstreetmap +psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -c "CREATE FUNCTION tile_for_point(int4, int4) RETURNS int8 AS '/db/functions/libpgosm', 'tile_for_point' LANGUAGE C STRICT" openstreetmap +psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -c "CREATE FUNCTION xid_to_int4(xid) RETURNS int4 AS '/db/functions/libpgosm', 'xid_to_int4' LANGUAGE C STRICT" openstreetmap diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000000..bfc1b9f52d --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,23 @@ +version: '2' +services: + web: + image: openstreetmap-website:v1 + build: + context: . + dockerfile: Dockerfile + volumes: + - .:/app + ports: + - "3000:3000" + command: bundle exec rails s -p 3000 -b '0.0.0.0' + depends_on: + - db + db: + image: openstreetmap-db:v1 + build: + context: . + dockerfile: Dockerfile.postgres + ports: + - "5432:5432" + environment: + POSTGRES_DB: openstreetmap