Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make docker work with docker-compose #1

Merged
merged 1 commit into from Feb 25, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions Dockerfile
@@ -0,0 +1,18 @@
FROM ruby:2.7.2
RUN apt-get update -qq && apt-get install -y postgresql-client
WORKDIR /myapp
ENV RAILS_ENV=production

COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp

# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]
3 changes: 0 additions & 3 deletions Gemfile
Expand Up @@ -37,6 +37,3 @@ group :development do
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
2 changes: 1 addition & 1 deletion Gemfile.lock
Expand Up @@ -95,7 +95,7 @@ GEM
mini_mime (1.0.2)
mini_portile2 (2.5.0)
minitest (5.14.3)
msgpack (1.4.1)
msgpack (1.4.2)
nio4r (2.5.4)
nokogiri (1.11.1)
mini_portile2 (~> 2.5.0)
Expand Down
24 changes: 24 additions & 0 deletions README.md
Expand Up @@ -6,3 +6,27 @@ This is a small ongoing series that discusses what it takes to build a basic API

- Read more at: [https://web-crunch.com/posts/create-a-basic-api-with-ruby-on-rails](https://web-crunch.com/posts/create-a-basic-api-with-ruby-on-rails)
- [Subscribe to the YouTube channel](https://youtube.com/c/webcrunch)

## Docker

Docker has been added with docker-compose to make the execution a lot easier.

To get up and running with run the following:

```
docker-commpose up
```

After the server is running, run the following command to create the database schema:

```
docker-compose exec web /myapp/bin/rails db:migrate RAILS_ENV=development
```

Next, run the following command to fill up some data from the seeds:

```
docker-compose exec web /myapp/bin/rails db:seed RAILS_ENV=development
```

Consequently, we can try `http://localhost:3000/api/v1/bands` and it should show us 5 bands and 4 members for the first band `The Beatles`.
Empty file added config/secrets.yml
Empty file.
17 changes: 17 additions & 0 deletions db/seeds.rb
Expand Up @@ -5,3 +5,20 @@
#
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
# Character.create(name: 'Luke', movie: movies.first)

# creating bands from: https://www.businessinsider.com/the-100-most-popular-rock-bands-of-all-time-2018-9
bands = Band.create([
{name: "The Beatles"},
{name: "Led Zeppelin"},
{name: "Queen"},
{name: "Pink Floyd"},
{name: "The Rolling Stones"}
]);

# From : https://en.wikipedia.org/wiki/List_of_members_of_bands_featuring_members_of_the_Beatles - Nov 1956
Member.create([
{name: "John Lenon", band: bands.first},
{name: "Eric Griffiths", band: bands.first},
{name: "Pete Shotton", band: bands.first},
{name: "Bill Smith", band: bands.first}
])
12 changes: 12 additions & 0 deletions docker-compose.yml
@@ -0,0 +1,12 @@
version: "3.9"
services:
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/myapp
ports:
- "3000:3000"
environment:
- SECRET_KEY_BASE=Qy6VTnREtzwBbzgvxY2LJA5QQwFNex4wUtj2qNTTnSXtHykdZM
- RAILS_ENV=development
8 changes: 8 additions & 0 deletions entrypoint.sh
@@ -0,0 +1,8 @@
#!/bin/bash
set -e

# Remove a potentially pre-existing server.pid for Rails.
rm -f /myapp/tmp/pids/server.pid

# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"