[RUBY] I tried to build a simple application using Dockder + Rails Scaffold

Introduction

How to build an application in Ruby on Rails using Docker-compose by referring to books, videos, and Qiita articles I had a hard time, so I tried various things and finally got it to work, so I will leave the final file and execution procedure.

Work procedure

File creation

Dockerfile
docker-compose.yml
Gemfile
Gemfile.lock
#Ruby in image name(Ver2.6.5)Specify the image of the execution environment of
FROM ruby:2.6.5

#Update the list of packages and install the packages required to build the rails environment
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs

#Create a directory for your project
RUN mkdir /myapp

#Set to working directory
WORKDIR /myapp

#Copy to project directory
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock

#Execute bundle install
RUN bundle install

#Copy all the contents of the build context to myapp
COPY . /myapp

docker-compose.yml


version: '3'
services:
  db:
    #Get image of postgres
    image: postgres
    environment:
      POSTGRES_USER: 'postgresql'
      POSTGRES_PASSWORD: 'postgresql-pass'
    restart: always
    volumes:
      - pgdatavol:/var/lib/postgresql/data
  web:
    #Build and use images from Dockerfile
    build: .
    #Executed when container starts
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    #Current directory/Bind mount to myapp
    volumes:
      - .:/myapp
    #Publish at 3000 and transfer to 3000 in container
    ports:
      - "3000:3000"
    #Start the db service before starting the web service
    depends_on:
      - db
#Create pgdatabol volume for data persistence and mount postgresql data area
volumes:
  pgdatavol:
source 'https://rubygems.org'
gem 'rails', '5.2.4.2'

Gemfile.lock


rails application creation

docker-compose run web rails new . --force --database=postgresql

Fixed database config file used for rails project

database.yml


default: &default
  adapter: postgresql
  encoding: unicode
  # --------add to--------
  host: db
  username: postgresql
  password: postgresql-pass
  # --------So far--------

Start in detach mode (background)

docker-compose up -d

What to do if bundle install is not reflected

docker-compose build --no-cache

Database creation command

docker-compose run web rails db:create

Create a simple application with Scaffold

docker-compose run web bin/rails g scaffold User name:string
docker-compose run web bin/rails db:migrate

http://localhost:3000/users

Reference URL

[I've just started Docker, so I've summarized it in an easy-to-understand manner](https://qiita.com/gold-kou/items/44860fbda1a34a001fc1#%E3%83%9B%E3%82%B9%E3%83%88% E5% 9E% 8B% E4% BB% AE% E6% 83% B3% E5% 8C% 96)

How to delete Docker image and container

[Creating a Docker container, starting and stopping](https://qiita.com/kooohei/items/0e788a2ce8c30f9dba53#5%E3%82%B3%E3%83%B3%E3%83%86%E3%83% 8A% E3% 81% AE% E5% 81% 9C% E6% AD% A2)

I should have bundle install in the image with Docker Compose + Rails, but I get an error if there is no gem.

Solution for Docker not bundle install in container

Recommended Posts

I tried to build a simple application using Dockder + Rails Scaffold
I tried using Hotwire to make Rails 6.1 scaffold a SPA
I tried to make a simple face recognition Android application using OpenCV
java I tried to break a simple block
I tried to implement a server using Netty
I tried to build a Firebase application development environment with Docker in 2020
I tried to make a talk application in Java using AI "A3RT"
I tried to decorate the simple calendar a little
Rails6 I tried to introduce Docker to an existing application
I tried to build an environment using Docker (beginner)
I tried to modernize a Java EE application with OpenShift.
[Rails] I tried to create a mini app with FullCalendar
I tried to build the environment little by little using docker
I tried to implement a buggy web application in Kotlin
[Rails] I tried to implement "Like function" using rails and js
Preparing to create a Rails application
[Rails] I tried deleting the application
I tried to create a simple map app in Android Studio
[Unity] I tried to make a native plug-in UniNWPathMonitor using NWPathMonitor
I tried to build a laravel operating environment while remembering Docker
I tried to make a group function (bulletin board) with Rails
I want to develop a web application!
I tried to introduce CircleCI 2.0 to Rails app
[Rails] Implementation of multi-layer category function using ancestry "I tried to make a window with Bootstrap 3"
After learning Progate, I tried to make an SNS application using Rails in the local environment
I tried to build AdoptOpenjdk 11 on CentOS 7
I tried to build Ruby 3.0.0 from source
How to build a Ruby on Rails environment using Docker (for Docker beginners)
[iOS] I tried to make a processing application like Instagram with Swift
I tried barcode scanning using Rails + React + QuaggaJS
[Rails] How to create a graph using lazy_high_charts
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I tried to organize the session in Rails
I tried to develop a man-hour management tool
I tried to develop a DUO3.0 study website.
I tried to create a LINE clone app
[Rails] I made a draft function using enum
I tried to build AdoptOpenJDK 8 (Addition: Amazon Corretto 8)
I tried to build Micra mackerel in 1 hour!
I tried to develop an application in 2 languages
I tried to develop a website to record expenses.
I tried to break a block with java (1)
I tried to make a machine learning application with Dash (+ Docker) part3 ~ Practice ~
[First environment construction] I tried to create a Rails 6 + MySQL 8.0 + Docker environment on Windows 10.
[Rails / JavaScript / Ajax] I tried to create a like function in two ways.
I want to be able to read a file using refile with administrate [rails6]
[Java] I tried to connect using a connection pool with Servlet (tomcat) & MySQL & Java
I tried to clone a web application full of bugs with Spring Boot
[Azure] I tried to create a Java application for free-Web App creation- [Beginner]
[Rails] How to connect to an external API using HTTP Client (I tried connecting to Qiita API)
I tried using a database connection in Android development
[Introduction] Try to create a Ruby on Rails application
I tried to make a simple game with Javafx ① "Let's find happiness game" (unfinished version ②)
Implement a reservation system using Rails and simple calendar! Let's add validation to datetime!
I tried to develop a ramen shop sharing website.
I tried using Gson
I tried to operate SQS using AWS Java SDK
I want to use a little icon in Rails
I tried using Wercker to create and publish a Docker image that launches GlassFish 5.
I tried using the Migration Toolkit for Application Binaries
I tried to create a Clova skill in Java