From 3e42182551389877eba4cb207299717e2082d67a Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 14 Nov 2017 04:33:05 -0600 Subject: [PATCH 1/7] added Dockerfile and docker-compose.yml --- Dockerfile | 37 +++++++++++++++++++++++++++++++++++++ Gemfile | 3 ++- config/database.yml.example | 8 +++++--- docker-compose.yml | 28 ++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..9d47c6d9b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,37 @@ +# # Select ubuntu as the base image +FROM coreapps/ruby2.3 + +# Install essential Linux packages +RUN apt-get update -qq && apt-get install -y build-essential libpq-dev postgresql-client nodejs + + + +# Define where our application will live inside the image +ENV RAILS_ROOT /var/www/consul + +# Create application home. App server will need the pids dir so just create everything in one shot +RUN mkdir -p $RAILS_ROOT/tmp/pids + +# Set our working directory inside the image +WORKDIR $RAILS_ROOT + +# Use the Gemfiles as Docker cache markers. Always bundle before copying app src. +# (the src likely changed and we don't want to invalidate Docker's cache too early) +# http://ilikestuffblog.com/2014/01/06/how-to-skip-bundle-install-when-deploying-a-rails-app-to-docker/ +COPY Gemfile Gemfile + +COPY Gemfile.lock Gemfile.lock + +# Prevent bundler warnings; ensure that the bundler version executed is >= that which created Gemfile.lock +RUN gem install bundler + +# Finish establishing our Ruby enviornment +RUN bundle install --full-index + +# Copy the Rails application into place +COPY . . + +# Define the script we want run once the container boots +# Use the "exec" form of CMD so our script shuts down gracefully on SIGTERM (i.e. `docker stop`) +#CMD [ "config/containers/app_cmd.sh" ] +CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0"] diff --git a/Gemfile b/Gemfile index 409f763e9..da8c87841 100644 --- a/Gemfile +++ b/Gemfile @@ -92,4 +92,5 @@ group :development do gem 'web-console', '~> 3.3.0' end -eval_gemfile './Gemfile_custom' +#this is crashing with the Dockerfile +#eval_gemfile './Gemfile_custom' diff --git a/config/database.yml.example b/config/database.yml.example index 2f8b7463f..dc1efbd7e 100644 --- a/config/database.yml.example +++ b/config/database.yml.example @@ -1,10 +1,12 @@ default: &default adapter: postgresql encoding: unicode - host: localhost + #host: localhost + host: database #<--the name of the db in the docker-compose pool: 5 - username: - password: + port: 5432 + username: postgres + password: <%= ENV['POSTGRES_PASSWORD'] %> development: &development <<: *default diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 000000000..701653da1 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,28 @@ +# service configuration for our database +database: + + # use the preferred version of the official Postgres image + # see https://hub.docker.com/_/postgres/ + image: postgres:9.4.5 + + # persist the database between containers by storing it in a volume + volumes: + - docker-example-postgres:/var/lib/postgresql/data + +# service configuration for our dockerized Rails app +app: + + # use the Dockerfile next to this file + build: . + + # rely on the RAILS_ENV value of the host machine + # environment: + #RAILS_ENV: $RAILS_ENV + + # makes the app container aware of the DB container + links: + - database + + # expose the port we configured Unicorn to bind to + ports: + - "3000:3000" From 2fba146c9baac3cc1d6629bb5470e526d27f1fc6 Mon Sep 17 00:00:00 2001 From: denialtorres Date: Tue, 14 Nov 2017 04:35:23 -0600 Subject: [PATCH 2/7] Update README.md --- README.md | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/README.md b/README.md index 48f6bfcb8..405ba1960 100644 --- a/README.md +++ b/README.md @@ -76,3 +76,42 @@ Code published under AFFERO GPL v3 (see [LICENSE-AGPLv3.txt](LICENSE-AGPLv3.txt) ## Contributions See [CONTRIBUTING.md](CONTRIBUTING.md) + + +## Working with Docker + +Prerequisites +------------- + +You should have installed Docker and Docker Compose in your machine. + +The First step is to build the container + + sudo docker build -t consul . + Then run + + + sudo docker-compose up -d database + to create your application and database images. + +Once built you can initialize your development DB and populate it with + + sudo docker-compose run app rake db:create + sudo docker-compose run app rake db:migrate + sudo docker-compose run app rake db:seed + +Now we can finally run the application with + + + sudo docker-compose up + +To verify the containers are up execute **sudo docker ps .** You should see output similar to this: + +![enter image description here](https://i.imgur.com/ASvzXrd.png) + +now you can enter to localhost:3000 + +to verify that CONSUL is running + +![enter image description here](https://i.imgur.com/Fl3XOIg.png) + From f32fae3065bb12b9f22e7e30d17a6c00ac9d0dd0 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 14 Nov 2017 05:00:18 -0600 Subject: [PATCH 3/7] separate database.yml configs files for each scenario --- config/database-docker.yml.example | 28 ++++++++++++++++++++++++++++ config/database.yml.example | 8 +++----- 2 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 config/database-docker.yml.example diff --git a/config/database-docker.yml.example b/config/database-docker.yml.example new file mode 100644 index 000000000..dc1efbd7e --- /dev/null +++ b/config/database-docker.yml.example @@ -0,0 +1,28 @@ +default: &default + adapter: postgresql + encoding: unicode + #host: localhost + host: database #<--the name of the db in the docker-compose + pool: 5 + port: 5432 + username: postgres + password: <%= ENV['POSTGRES_PASSWORD'] %> + +development: &development + <<: *default + database: consul_development + +# The staging, preproduction and production dbs are only needed +# for running rake assets:precompile locally before deploying +staging: + <<: *development + +preproduction: + <<: *development + +production: + <<: *development + +test: + <<: *default + database: consul_test diff --git a/config/database.yml.example b/config/database.yml.example index dc1efbd7e..2f8b7463f 100644 --- a/config/database.yml.example +++ b/config/database.yml.example @@ -1,12 +1,10 @@ default: &default adapter: postgresql encoding: unicode - #host: localhost - host: database #<--the name of the db in the docker-compose + host: localhost pool: 5 - port: 5432 - username: postgres - password: <%= ENV['POSTGRES_PASSWORD'] %> + username: + password: development: &development <<: *default From ff09e623ff7462043f3337598f45f58da2129447 Mon Sep 17 00:00:00 2001 From: denialtorres Date: Tue, 14 Nov 2017 05:03:36 -0600 Subject: [PATCH 4/7] Update README.md --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 405ba1960..02f50c9a3 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,12 @@ Prerequisites You should have installed Docker and Docker Compose in your machine. +Also if you are going to work with docker first replace the database.yml with the preconfigured file made it to work it + + + cp config/database-docker.yml.example config/database.yml + + The First step is to build the container sudo docker build -t consul . From a715df3431c29d0c647940c114128580e1d719ee Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 15 Nov 2017 08:10:14 -0600 Subject: [PATCH 5/7] fix the warning of secret_key for devise --- Dockerfile | 3 +++ Gemfile | 2 +- config/initializers/devise.rb | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 9d47c6d9b..adfc146f4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,6 +6,7 @@ RUN apt-get update -qq && apt-get install -y build-essential libpq-dev postgresq + # Define where our application will live inside the image ENV RAILS_ROOT /var/www/consul @@ -22,6 +23,8 @@ COPY Gemfile Gemfile COPY Gemfile.lock Gemfile.lock +COPY Gemfile_custom Gemfile_custom + # Prevent bundler warnings; ensure that the bundler version executed is >= that which created Gemfile.lock RUN gem install bundler diff --git a/Gemfile b/Gemfile index da8c87841..ed3337438 100644 --- a/Gemfile +++ b/Gemfile @@ -93,4 +93,4 @@ group :development do end #this is crashing with the Dockerfile -#eval_gemfile './Gemfile_custom' +eval_gemfile './Gemfile_custom' diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index a0b6ac249..6acce5068 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -6,7 +6,7 @@ Devise.setup do |config| # confirmation, reset password and unlock tokens in the database. # Devise will use the `secret_key_base` on Rails 4+ applications as its `secret_key` # by default. You can change it below and use your own secret key. - # config.secret_key = Rails.application.secrets.secret_key_base + config.secret_key = Rails.application.secrets.secret_key_base # ==> Mailer Configuration # Configure the e-mail address which will be shown in Devise::Mailer, From 2041c9b8bb749a93de05bddd9bb5885ed91e20e9 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 15 Nov 2017 08:53:37 -0600 Subject: [PATCH 6/7] modify docker compose to working with the code in realtime --- docker-compose.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docker-compose.yml b/docker-compose.yml index 701653da1..d5399089e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -26,3 +26,6 @@ app: # expose the port we configured Unicorn to bind to ports: - "3000:3000" + # map our application source code, in full, to the application root of our container + volumes: + - .:/var/www/consul From c58ea7e8a4c5e40391a6e4b5d2dd90e39843bdd9 Mon Sep 17 00:00:00 2001 From: denialtorres Date: Wed, 15 Nov 2017 09:00:59 -0600 Subject: [PATCH 7/7] add new commands --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 02f50c9a3..70cd5ed06 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,13 @@ Once built you can initialize your development DB and populate it with sudo docker-compose run app rake db:create sudo docker-compose run app rake db:migrate sudo docker-compose run app rake db:seed + sudo docker-compose run app rake db:dev_seed + +If you want to run the rails console just run in another terminal: + + + `sudo docker-compose run app rails console` + Now we can finally run the application with