Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
ab505e6
moving to mysql (a read database :)
BugWrestler5000 Jul 8, 2022
c8561eb
fixing readme
BugWrestler5000 Jul 8, 2022
a1c8ef3
Update ruby.yml
BugWrestler5000 Jul 8, 2022
91cae36
Update ruby.yml
BugWrestler5000 Jul 8, 2022
a5ab463
Update database.yml
BugWrestler5000 Jul 8, 2022
19bf56d
Update database.yml
BugWrestler5000 Jul 8, 2022
d1a6ebf
Update ruby.yml
BugWrestler5000 Jul 8, 2022
6c833cc
Update ruby.yml
BugWrestler5000 Jul 8, 2022
454c112
Update database.yml
BugWrestler5000 Jul 8, 2022
ce75d64
Update ruby.yml
BugWrestler5000 Jul 8, 2022
65ecdc4
Update ruby.yml
BugWrestler5000 Jul 8, 2022
b160542
Update database.yml
BugWrestler5000 Jul 8, 2022
5116625
Update database.yml
BugWrestler5000 Jul 8, 2022
d97f4a1
Update ruby.yml
BugWrestler5000 Jul 8, 2022
2fed162
Update ruby.yml
BugWrestler5000 Jul 8, 2022
1d23b17
Update ruby.yml
BugWrestler5000 Jul 8, 2022
2602c48
Update ruby.yml
BugWrestler5000 Jul 8, 2022
eb12e17
Update ruby.yml
BugWrestler5000 Jul 8, 2022
a14de73
Update ruby.yml
BugWrestler5000 Jul 8, 2022
cc71eba
Update ruby.yml
BugWrestler5000 Jul 8, 2022
10a4aa3
Update ruby.yml
BugWrestler5000 Jul 8, 2022
8576c3e
Update ruby.yml
BugWrestler5000 Jul 8, 2022
2764f83
Update ruby.yml
BugWrestler5000 Jul 8, 2022
4bb829b
Update ruby.yml
BugWrestler5000 Jul 8, 2022
4bc120b
Update ruby.yml
BugWrestler5000 Jul 8, 2022
f7df91d
Update ruby.yml
BugWrestler5000 Jul 8, 2022
5c7884a
Update ruby.yml
BugWrestler5000 Jul 8, 2022
ffe3727
Update ruby.yml
BugWrestler5000 Jul 8, 2022
852cd4c
Update ruby.yml
BugWrestler5000 Jul 8, 2022
7eb4992
Update ruby.yml
BugWrestler5000 Jul 8, 2022
d004d44
Update ruby.yml
BugWrestler5000 Jul 8, 2022
6980600
Update ruby.yml
BugWrestler5000 Jul 8, 2022
1caaace
Update ruby.yml
BugWrestler5000 Jul 8, 2022
182908d
Update database.yml
BugWrestler5000 Jul 8, 2022
25b40d0
Update database.yml
BugWrestler5000 Jul 8, 2022
889838d
Update ruby.yml
BugWrestler5000 Jul 8, 2022
9bf65d5
Update ruby.yml
BugWrestler5000 Jul 8, 2022
5065d5d
Update ruby.yml
BugWrestler5000 Jul 8, 2022
bc05bed
Update ruby.yml
BugWrestler5000 Jul 8, 2022
6c4c39a
Update database.yml
BugWrestler5000 Jul 8, 2022
3fc6044
Update ruby.yml
BugWrestler5000 Jul 8, 2022
2f92bfa
Update README.md
BugWrestler5000 Jul 8, 2022
0d2c6c8
Update README.md
BugWrestler5000 Jul 8, 2022
707a6fc
Update ruby.yml
BugWrestler5000 Jul 10, 2022
9bc2f9b
Fix typo in database.yml development host
DanDanilyuk Jan 19, 2023
bbc13dc
Merge pull request #1 from DanDanilyuk/mysql-db-host-typo
BugWrestler5000 Jul 31, 2023
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
6 changes: 3 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
PGHOST=db
PGUSER=postgres
PGPASSWORD=changeme
HOST=db
USER=mysql
PASSWORD=change_me_right_now
31 changes: 18 additions & 13 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,24 @@ jobs:

env:
RAILS_ENV: test
DATABASE_URL: postgres://postgres:postgres@localhost:5432
DISABLE_BOOTSNAP: true
DISABLE_BOOTSNAP_LOAD_PATH_CACHE: true

services:
postgres:
image: postgres:12
env:
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
mysql:
image: mysql
#
env:
MYSQL_ROOT_PASSWORD: change_me_right_now
MYSQL_ALLOW_EMPTY_PASSWORD: true
MYSQL_ROOT_HOST: 172.18.0.1
MYSQL_LOG_CONSOLE: true
MYSQL_PASSWORD: change_me_right_now
ports:
- 8888:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

steps:
steps:
- uses: actions/checkout@v3

- name: Set up Ruby
Expand All @@ -59,6 +59,11 @@ jobs:

- name: Create database
run: |
sudo /etc/init.d/mysql start --skip-grant-tables
mysql -h 127.0.0.1 --port 8888 -pchange_me_right_now -uroot -e "SHOW DATABASES"
echo "************"
printenv
echo "************"
bundle exec rails db:create
bundle exec rails db:schema:load

Expand Down
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ RUN apt-get update -qq && apt-get install -yq --no-install-recommends \
gnupg2 \
less \
git \
libpq-dev \
postgresql-client \
telnet \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN apt-get update -qq && apt-get install -y default-libmysqlclient-dev

ENV LANG=C.UTF-8 \
BUNDLE_JOBS=4 \
BUNDLE_RETRY=3
Expand Down
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ gem "rails", "~> 7.0.3"
# The original asset pipeline for Rails [https://github.com/rails/sprockets-rails]
gem "sprockets-rails"

# Use postgresql as the database for Active Record
gem "pg", "~> 1.4"
# Use mysql as the database for Active Record
gem "mysql2", "~> 0.5"

# Use the Puma web server [https://github.com/puma/puma]
gem "puma", "~> 5.6"
Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ GEM
mini_mime (1.1.2)
minitest (5.16.1)
msgpack (1.5.2)
mysql2 (0.5.4)
net-imap (0.2.3)
digest
net-protocol
Expand All @@ -148,7 +149,6 @@ GEM
parallel (1.22.1)
parser (3.1.2.0)
ast (~> 2.4.1)
pg (1.4.1)
public_suffix (4.0.7)
puma (5.6.4)
nio4r (~> 2.0)
Expand Down Expand Up @@ -285,7 +285,7 @@ DEPENDENCIES
image_processing (~> 1.12)
importmap-rails
jbuilder
pg (~> 1.4)
mysql2 (~> 0.5)
puma (~> 5.6)
rack-mini-profiler
rails (~> 7.0.3)
Expand Down
52 changes: 12 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,80 +1,52 @@
![Ruby](https://github.com/ryanwi/rails7-on-docker/workflows/Ruby/badge.svg)

# Rails 7 on Docker demo application
# Rails 7 on Docker ** MYSQL ** demo application

This app demonstrates Rails 7 with PostgreSQL, import maps, turbo, stimulus and hotwire, all running in Docker.

**NOTE:** [There is also an example Rails 6 application working in Docker with Webpacker](https://github.com/ryanwi/rails-on-docker)
This app demonstrates Rails 7 with Mysql8, import maps, turbo, stimulus and hotwire, all running in Docker.

## Features

* Mysql 8
* Rails 7
* Ruby 3
* Dockerfile and Docker Compose configuration
* PostgreSQL database
* MYSQL database
* Redis
* GitHub Actions for
* tests
* Rubocop for linting
* Security checks with [Brakeman](https://github.com/presidentbeef/brakeman) and [bundler-audit](https://github.com/rubysec/bundler-audit)
* Dependabot for automated updates

## Initial setup
```
cp .env.example .env
docker compose build
docker compose run --rm web bin/rails db:setup
```

## Running the Rails app
```
docker compose up
sh start.sh
```

## Running the Rails console
When the app is already running with `docker-compose` up, attach to the container:
```
docker compose exec web bin/rails c
docker-compose exec web bin/rails c
```

When no container running yet, start up a new one:
```
docker compose run --rm web bin/rails c
docker-compose run --rm web bin/rails c
```

## Running tests
```
docker compose run --rm web bin/rspec
docker-compose run --rm web bin/rspec
```

## Updating gems
```
docker compose run --rm web bundle update
docker compose up --build
```

## Production build

```
docker build -f production.Dockerfile .
docker-compose run --rm web bundle update
docker-compose up --build
```

## Credits/References
Thanks!! to ryanwi for the postgres version. The conversion to MYSQL8 was pretty easy!!

### Rails with Docker
* [Quickstart: Compose and Rails](https://docs.docker.com/compose/rails/)
* [Docker for Rails Developers
Build, Ship, and Run Your Applications Everywhere](https://pragprog.com/titles/ridocker/docker-for-rails-developers/)
* [Ruby on Whales:
Dockerizing Ruby and Rails development](https://evilmartians.com/chronicles/ruby-on-whales-docker-for-ruby-rails-development)

### Rails 7 with importmaps

* [Alpha preview: Modern JavaScript in Rails 7 without Webpack](https://www.youtube.com/watch?v=PtxZvFnL2i0)

### Rails 7 with hotwire
https://github.com/ryanwi/rails7-on-docker

* [Stimulus 3 + Turbo 7 = Hotwire 1.0](https://world.hey.com/dhh/stimulus-3-turbo-7-hotwire-1-0-9d507133)
* [Turbo 7](https://world.hey.com/hotwired/turbo-7-0dd7a27f)
* [Rails 7 will have three great answers to JavaScript in 2021+](https://world.hey.com/dhh/rails-7-will-have-three-great-answers-to-javascript-in-2021-8d68191b)
* [Hotwire Turbo Replacing Rails UJS](https://www.driftingruby.com/episodes/hotwire-turbo-replacing-rails-ujs)
1 change: 1 addition & 0 deletions clean.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docker container stop $(docker container ls -aq) && docker system prune -af --volumes
75 changes: 9 additions & 66 deletions config/database.yml
Original file line number Diff line number Diff line change
@@ -1,86 +1,29 @@
# PostgreSQL. Versions 9.3 and up are supported.
#
# Install the pg driver:
# gem install pg
# On macOS with Homebrew:
# gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On macOS with MacPorts:
# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
# gem install pg
# Choose the win32 build.
# Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem "pg"
#
default: &default
adapter: postgresql
encoding: unicode
adapter: mysql2
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
<<: *default
database: railsondocker_development

# The specified database role being used to connect to postgres.
# To create additional roles in postgres see `$ createuser --help`.
# When left blank, postgres will use the default role. This is
# the same name as the operating system user running Rails.
#username: railsondocker

# The password associated with the postgres role (username).
#password:

# Connect on a TCP socket. Omitted by default since the client uses a
# domain socket that doesn't need configuration. Windows does not have
# domain sockets, so uncomment these lines.
#host: localhost

# The TCP port the server listens on. Defaults to 5432.
# If your server runs on a different port number, change accordingly.
#port: 5432

# Schema search path. The server defaults to $user,public
#schema_search_path: myapp,sharedapp,public

# Minimum log levels, in increasing order:
# debug5, debug4, debug3, debug2, debug1,
# log, notice, warning, error, fatal, and panic
# Defaults to warning.
#min_messages: notice
username: railsondocker
password: change_me_right_now
host: rails7-on-docker-mysql-db-1

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: railsondocker_test
username: root
password: change_me_right_now
host: 127.0.0.1
port: 8888

# As with config/credentials.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password or a full connection URL as an environment
# variable when you boot the app. For example:
#
# DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
#
# If the connection URL is provided in the special DATABASE_URL environment
# variable, Rails will automatically merge its configuration values on top of
# the values provided in this file. Alternatively, you can specify a connection
# URL environment variable explicitly:
#
# production:
# url: <%= ENV["MY_APP_DATABASE_URL"] %>
#
# Read https://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full overview on how database connection configuration can be specified.
#
production:
<<: *default
database: railsondocker_production
username: railsondocker
password: <%= ENV["RAILSONDOCKER_DATABASE_PASSWORD"] %>
password: change_me_right_now
36 changes: 16 additions & 20 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,61 +10,57 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2021_12_16_185503) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "action_text_rich_texts", force: :cascade do |t|
ActiveRecord::Schema[7.0].define(version: 2021_12_16_185503) do
create_table "action_text_rich_texts", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "name", null: false
t.text "body"
t.text "body", size: :long
t.string "record_type", null: false
t.bigint "record_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["record_type", "record_id", "name"], name: "index_action_text_rich_texts_uniqueness", unique: true
end

create_table "active_storage_attachments", force: :cascade do |t|
create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
t.bigint "record_id", null: false
t.bigint "blob_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "created_at", null: false
t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
end

create_table "active_storage_blobs", force: :cascade do |t|
create_table "active_storage_blobs", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "key", null: false
t.string "filename", null: false
t.string "content_type"
t.text "metadata"
t.string "service_name", null: false
t.bigint "byte_size", null: false
t.string "checksum"
t.datetime "created_at", precision: 6, null: false
t.datetime "created_at", null: false
t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
end

create_table "active_storage_variant_records", force: :cascade do |t|
create_table "active_storage_variant_records", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.bigint "blob_id", null: false
t.string "variation_digest", null: false
t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
end

create_table "articles", force: :cascade do |t|
create_table "articles", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "title"
t.text "content"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "comments", force: :cascade do |t|
create_table "comments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.bigint "article_id", null: false
t.text "content"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["article_id"], name: "index_comments_on_article_id"
end

Expand Down
Loading