diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..8d5c7288 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,16 @@ +.bundle +.git +.github +.vscode +config/database.yml +config/deploy.yml +config/deploy.yml.example +docker_data +log +node_modules +public/assets +public/packs +public/packs-test +public/system +public/sitemap.xml +tmp diff --git a/.gitignore b/.gitignore index 86956cbc..034ca1ef 100644 --- a/.gitignore +++ b/.gitignore @@ -21,7 +21,8 @@ config/deploy.yml public/sitemap.xml public/system -# Ignore this Node thingy. +# Ignore Docker +docker_data/ node_modules/ /public/packs/ -/public/packs-test/ \ No newline at end of file +/public/packs-test/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..b2a3708f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,27 @@ +# This Dockerfile compiles the makigas web application. It can be +# used to deploy a static image to a production server or to +# develop or test the application in a standalone application +# without having to install the stuff. + +FROM ruby:2.4.1-alpine +LABEL maintainer="dani@danirod.es" + +# Install dependencies. +RUN apk add --update alpine-sdk postgresql-dev imagemagick nodejs tzdata && \ + npm install -g yarn + +# Initializes the working directory. +RUN mkdir /makigas +WORKDIR /makigas + +# Install dependencies +ADD Gemfile Gemfile +ADD Gemfile.lock Gemfile.lock +ADD package.json package.json +ADD docker/database.yml config/database.yml +ADD yarn.lock yarn.lock +RUN bundle install && yarn install + +# Remaining files. +ADD . . +CMD ["rails", "server", "-b", "0.0.0.0"] diff --git a/README.md b/README.md index 6bfc86c5..90b73ec9 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,8 @@ and playlists in the system. # Setting up +**To use the experimental Docker images, see below.** + ## Requirements Supported operating systems: @@ -90,6 +92,68 @@ you will have to use the Rails console to seed the first user, like so: ... > User.create(email: 'foo@example.com', password: '123456') +# Development and testing using Docker + +This repository uses Docker and Docker Compose. Docker Swarm is being closely +evaluated, but it is not officially supported and it may or may not work right +away. + +## Workflows + +* The Dockerfile for the makigas web application is located at `/Dockerfile`. + It builds the system image by packaging the web application code and required + dependencies (Node.js, Ruby, Imagemagick...) into a Docker image that, once + started, starts the Puma server. + +* `/docker-compose.yml` sets up a complete development platform including + PostgreSQL as a database running on another container. This is the one that + you will always use to operate the machine as it already sets up the + environment so that the web application image built by the Dockerfile can use + the database. + +* `/spec/docker-compose.yml` sets up a second development platform including + another PostgreSQL instance. This is the one that you'll use when running + tests since it's connected to a **different** database, so it doesn't + matter if the testing database gets trashed, it won't affect development + (or production) databases. + +## Development commands + +Start a development session using `docker-compose up -d`. First issue of this +command will take significantly longer as it has to pull PostgreSQL and +build the web application. Then, it will boot the required containers. Once +they are ready, you can browse the server at the URL `http://localhost:3000`. + +**Migrations are pending** (a.k.a. how do I run commands on this VM?): +Use `docker-compose exec web [command]`. Examples: + +* Attach a shell: `docker-compose exec web /bin/sh` +* Attach another shell: `docker-compose exec web /bin/bash` +* Run a rake task: `docker-compose exec web rake -T` +* Migration: `docker-compose exec web rake db:migrate` + +Stop a development session by using `docker-compose stop` or +`docker-compose down`. First command will stop the containers but won't delete +them. Second command will also delete the containers. + +## Testing commands + +* Change directory to `spec` to use `spec/docker-compose.yml`. This compose + file uses a different database to avoid destroying data on development or + production databases. Additionally, it doesn't run `rails server` as it's + not required. + +* Start a testing session using `docker-compose up -d`. + +* Run RSpec: `docker-compose run --rm test rspec`. Since `run` will create + an additional container, keep the `--rm` parameter to remove that container + once RSpec ends. + +* Stop the testing session using `docker-compose down`. + +`docker-compose run` will return the status code of the given command. So, +if RSpec fails, `docker-compose run` will also return 1. + # Contributing Read the [CONTRIBUTING.md][1] file for more information on how to contribute to @@ -143,4 +207,4 @@ PGP key is on my personal website][3]. [1]: https://github.com/makigas/makigas/blob/master/CONTRIBUTING.md [2]: https://github.com/makigas/makigas/blob/master/CODE_OF_CONDUCT.md -[3]: https://www.danirod.es/contact/ \ No newline at end of file +[3]: https://www.danirod.es/contact/ diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..271fe6f3 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,31 @@ +# Development & Testing docker-compose, not suitable for production. + +version: '3' + +services: + database: + image: postgres:9.4-alpine + volumes: + - './docker_data/postgres:/var/lib/postgresql/data' + environment: + POSTGRES_USER: 'makigas' + POSTGRES_PASSWORD: 'makigas' + web: + build: . + image: danirod/makigas + container_name: makigas + restart: always + depends_on: + - database + volumes: + - '.:/makigas' + - '/makigas/node_modules/' + - '/makigas/tmp/' + ports: + - '3000:3000' + environment: + RAILS_ENV: development + DB_HOST: database + DB_NAME: makigas + DB_USERNAME: makigas + DB_PASSWORD: makigas diff --git a/docker/README.md b/docker/README.md new file mode 100644 index 00000000..531d4960 --- /dev/null +++ b/docker/README.md @@ -0,0 +1,19 @@ +This directory contains resources designed to be used with the web +application software when the web application is running inside a +Docker container. + +There are a few configuration files that may not work properly when +running inside a Docker container unless they are changed, such as the +database configuration file. + +This README document describes the changes made to these files. + +## database.yml (=> /config/database.yml) + +The default database.yml is optimized for multiple environments (i.e. +you can use the same application instance to code and test using the +`development` and `test` environments). + +On Docker images you don't usually want that behaviour, but instead +a single running environment, usually configured through environment +variables such as RAILS\_ENV, DATABASE\_HOST and such. \ No newline at end of file diff --git a/docker/database.yml b/docker/database.yml new file mode 100644 index 00000000..c0a9e88e --- /dev/null +++ b/docker/database.yml @@ -0,0 +1,12 @@ +<%= ENV['RAILS_ENV'] %>: + # Database connection + host: <%= ENV['DB_HOST'] || 'localhost' %> + port: <%= ENV['DB_PORT'] || '5432' %> + database: <%= ENV['DB_NAME'] || "makigas_#{ENV['RAILS_ENV']}" %> + username: <%= ENV['DB_USERNAME'] || 'makigas' %> + password: <%= ENV['DB_PASSWORD'] || '' %> + + # Adapter settings + adapter: <%= ENV['DB_ADAPTER'] || 'postgresql' %> + encoding: unicode + pool: <%= ENV['DB_POOL'] || 5 %> \ No newline at end of file diff --git a/spec/docker-compose.yml b/spec/docker-compose.yml new file mode 100644 index 00000000..5f7a5376 --- /dev/null +++ b/spec/docker-compose.yml @@ -0,0 +1,28 @@ +# Testing file for Docker + +version: '3' + +services: + dbtest: + image: postgres:alpine + volumes: + - '../docker_data/postgres-test:/var/lib/postgresql/data' + environment: + POSTGRES_USER: 'makigas' + POSTGRES_PASSWORD: 'makigas' + test: + image: danirod/makigas + depends_on: + - dbtest + volumes: + - '..:/makigas' + - '/makigas/node_modules' + - '/makigas/tmp' + environment: + RAILS_ENV: test + DB_HOST: dbtest + DB_NAME: makigas + DB_USERNAME: makigas + DB_PASSWORD: makigas + command: /bin/true +