Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/thedayisntgray/rails-7-mysql

Dockerized Rails 7 & MySql DB project
https://github.com/thedayisntgray/rails-7-mysql

Last synced: 21 days ago
JSON representation

Dockerized Rails 7 & MySql DB project

Awesome Lists containing this project

README

        

# ChatGPT 4o Generate Guide to Create a Rails 7 and MySql DB project

## 2 Prompts where used:

### Initial Prompts:
> Generate the steps to create a rails 7 app and a mysql instance in 2 docker containers that I can run.

Unfortunatuely GPT4o thought it should skip active record passing ```--skip-active-record```. I was curious why it did this and I prompted it with the following prompt.

> Why are you skipping this?

These two prompts are the precursor of the guide you see below.

### Step 1: Install Docker

First, make sure Docker is installed on your system. You can download and install Docker from [here](https://www.docker.com/products/docker-desktop).

### Step 2: Create a Rails Application

Open a terminal and create a new Rails 7 application:

```bash
rails new myapp -d mysql
cd myapp
```

### Step 3: Configure the Database

Update the `config/database.yml` file with the following content if needed (the default configuration is usually correct):

```yaml
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password: password
host: db

development:
<<: *default
database: myapp_development

test:
<<: *default
database: myapp_test

production:
<<: *default
database: myapp_production
username: myapp
password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>
```

### Step 4: Create a Dockerfile

Create a `Dockerfile` in the root of your Rails application with the following content:

```Dockerfile
# Dockerfile
FROM ruby:3.1.1

# Install dependencies
RUN apt-get update -qq && apt-get install -y nodejs yarn

# Set up working directory
WORKDIR /myapp

# Install gems
COPY Gemfile Gemfile.lock ./
RUN bundle install

# Copy the rest of the application code
COPY . .

# Precompile assets
RUN bundle exec rails assets:precompile

# Expose port 3000 to the Docker host
EXPOSE 3000

# Start the server
CMD ["rails", "server", "-b", "0.0.0.0"]
```

### Step 5: Create a Docker Compose File

Create a `docker-compose.yml` file in the root of your Rails application with the following content:

```yaml
version: '3.8'
services:
db:
image: mysql:8.0
volumes:
- mysql_data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: myapp_development
ports:
- "3306:3306"

web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails server -b '0.0.0.0'"
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- db

volumes:
mysql_data:
```

### Step 6: Build and Run the Containers

Build and run the Docker containers using Docker Compose:

```bash
docker-compose up --build
```

This command will build the Docker image for your Rails application and start both the Rails and MySQL containers.

### Step 7: Create the Database

Open a new terminal and run the following command to create the database:

```bash
docker-compose run web rake db:create
```

### Step 8: Verify the Setup

Visit `http://localhost:3000` in your web browser. You should see the default Rails welcome page, indicating that your Rails application is running and connected to the MySQL database.

### Summary

You've created a Rails 7 application with a MySQL database, both running in Docker containers. The `docker-compose.yml` file configures the services and ensures that the containers can communicate with each other. You can manage your Rails application and database using Docker Compose commands.