Skip to content
This repository has been archived by the owner on May 5, 2022. It is now read-only.

Docker and Docker Compose support #34

Merged
merged 1 commit into from
Oct 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,30 @@ $ bin/yarn test
- Add Variable.
- Name the variable `PRONTO_GITHUB_ACCESS_TOKEN` with the value from the previous Personal Access Token.

# Usage with Docker

A `Dockerfile` and `docker-compose.yml` file are created by default. When run with `--webpack=react`,
there are two `Dockerfile`s: one for the Rails server and one for the assets server.

To use Docker, run:

```sh
docker-compose build
```

then set up the the database:

```sh
docker-compose run web rails db:create
docker-compose run web rails db:migrate
```

finally, start the server and visit `localhost:3000`:

```sh
docker-compose up
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the documentation. This has led me to create #35 to handle separately - which basically says we should take all this information and populate it in the README of the projects we're creating from this template as well, along with other setup info.


# Switching out Capybara Driver
If you'd like to not run your tests headless, for example, to troubleshoot an issue and see what's on the screen, modify the `Capybara.javascript_driver` in `spec/rails_helper.rb` to use `:chome` instead of `:headless_chrome`. After the change, this line should look as follows:

Expand Down
17 changes: 16 additions & 1 deletion gnarly.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,24 @@ def modify_npm_scripts
"Capybara.default_max_wait_time = 3\n"
end

react = options[:webpack] == "react"

# Docker
copy_file "templates/Dockerfile", "Dockerfile"

if react
copy_file "templates/Dockerfile-assets", "Dockerfile-assets"
copy_file "templates/.env.docker/.env.docker-webpack", ".env.docker"
copy_file "templates/.env.docker-assets", ".env.docker-assets"
copy_file "templates/docker-compose.yml/docker-compose-webpack.yml", "docker-compose.yml"
Copy link
Contributor Author

@zfletch zfletch Oct 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way I organized the files that are different for React and non-React is:

templates/
  file_name.extension/
    file_name-standard.extension
    file_name-webpack.extension

I think this is reasonable, but I could change it if there's already a standard in place.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Granted, this is not reflected in the current directory structure of templates (as it started off flat), but I've been thinking of modifying it to group related elements together, like having a directory for ci, db, style, etc.

Perhaps we could start that by having a directory templates/docker that all of the docker files in this diff live in? Other than that, I'm fine with your organization.

Copy link
Contributor Author

@zfletch zfletch Oct 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to group the files, and to do that in another PR so the locations of all the files are consistent. I also think before doing that it would be good to figure out #16 since testing that everything works with and without Docker and with and without React takes a long time

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. #16 has been my white whale since starting the project. I agree with your thoughts though.

else
copy_file "templates/.env.docker/.env.docker-standard", ".env.docker"
copy_file "templates/docker-compose.yml/docker-compose-standard.yml", "docker-compose.yml"
end

# Retrieve all gems, set up git, display next steps
after_bundle do
if options[:webpack] == "react"
if react
install_js_deps
add_js_files
modify_npm_scripts
Expand Down
1 change: 1 addition & 0 deletions templates/.env.docker-assets
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
WEBPACKER_DEV_SERVER_HOST=0.0.0.0
Copy link
Contributor Author

@zfletch zfletch Oct 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using environment variables to configure the Webpack Dev server is a new feature merged just a few days ago. Previously you had to change the config/webpacker.yml file.

You still can't use command line arguments, because why would you need to use command line arguments when you can just edit a yaml file?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the convention you have, should this be in the .env.docker directory?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are three .env files involved: .env.docker/.env-docker-standard, .env.docker/.env-docker-webpack, and .env-docker-assets.

The first two are copied as .env.docker in the created application. They need to have different values for the Webpack and non-Webpack cases.

The third one is copied as .env.docker-assets in the created application, but it's only copied when --webpack=react is passed in.

1 change: 1 addition & 0 deletions templates/.env.docker/.env.docker-standard
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DATABASE_HOST=db
2 changes: 2 additions & 0 deletions templates/.env.docker/.env.docker-webpack
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DATABASE_HOST=db
WEBPACKER_DEV_SERVER_HOST=assets
20 changes: 20 additions & 0 deletions templates/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM ruby:2.4.2

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming you've tested this w/spinning up a postgres DB with some data in it and such with it working fine?

I only ask because EEC's Dockerfile has some stuff about setting up keys for postgres, which I can't claim to understand why it's needed, but figured I'd toss it out there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if what that code is for or whether it's currently necessary in EEC Web. I commented it out locally and was able to build and run the app successfully.

# use a newer version of Node to use Yarn
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash -

# nodejs: Rails needs a JS runtime
# cmake: required by Rugged, dependency of Pronto
RUN apt-get update -qq \
&& apt-get install -y -qq nodejs cmake

RUN npm install -g yarn

RUN mkdir /app
WORKDIR /app

ADD Gemfile /app/Gemfile
ADD Gemfile.lock /app/Gemfile.lock
RUN bundle install

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tried running acceptance tests with this? Again, just basing on EEC's file having various lines for chrome-driver setup.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't tested Chrome driver. I think there's potentially enough work getting that running that it should be a separate issue.

ADD . /app
24 changes: 24 additions & 0 deletions templates/Dockerfile-assets
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM ruby:2.4.2

# use a newer version of Node to use Yarn
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash -

# nodejs: Rails needs a JS runtime
# cmake: required by Rugged, dependency of Pronto
RUN apt-get update -qq \
&& apt-get install -y -qq nodejs cmake

RUN npm install -g yarn

RUN mkdir /app
WORKDIR /app

ADD Gemfile /app/Gemfile
ADD Gemfile.lock /app/Gemfile.lock
RUN bundle install

ADD package.json /app/package.json
ADD yarn.lock /app/yarn.lock
RUN yarn install
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this file should only be the above 3 lines (as everything else looks the same as the Dockerfile).

Then we could copy over Dockerfile twice - once for web, and another for assets. For the assets version, we can then use insert_into_file "Dockerfile-assets", before: " \ADD . /app\n" do...end, reading in and inserting these 3 lines here.

That way, if we need to change anything here, like the ruby version, we only have to do it once and it'll be used by both files for future apps generated from the template.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there could be a lot if churn in this file. In theory you shouldn't really need Ruby to run the development.

The way Rails and Webpacker is currently written, you have to have the full Rails app installed with all of its gems in order to call the dev server, which is a wrapper around a JavaScript library.

This makes a lot of sense when you're running the server locally -- you already have Rails and everything installed, but it doesn't work as well with Docker Compose. I can imagine a future where this could be changed to a nodejs image that doesn't have to know anything about Rails, but Webpacker is something I'm not that familiar with. At this point I don't know enough to know whether having the files completely separate or not will end up being easier to manage.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. Let's keep this as-is for now, get some experience with using it on new projects/various updates, and figure out what feels best in terms of managing it over time.

What's here is simple (in a good way) and that may very well be best.


ADD . /app
15 changes: 15 additions & 0 deletions templates/docker-compose.yml/docker-compose-standard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: '3'
services:
db:
image: postgres
web:
build: .
command: bash -c 'rm -f tmp/pids/server.pid && bundle exec rails s --port 3000 --binding 0.0.0.0'
volumes:
- .:/app
ports:
- 3000:3000
depends_on:
- db
env_file:
- .env.docker
26 changes: 26 additions & 0 deletions templates/docker-compose.yml/docker-compose-webpack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
version: '3'
services:
db:
image: postgres
assets:
build:
context: .
dockerfile: Dockerfile-assets
command: yarn start
volumes:
- .:/app
- /app/node_modules
env_file:
- .env.docker-assets
web:
build: .
command: bash -c 'rm -f tmp/pids/server.pid && bundle exec rails s --port 3000 --binding 0.0.0.0'
volumes:
- .:/app
ports:
- 3000:3000
depends_on:
- db
- assets
env_file:
- .env.docker