[RUBY] Rails application building with Docker

Overview

At the Docker study session, I thought it would be a waste to fill the created repository as it is, so I wrote it as an article. I hope it will be helpful to someone.

https://github.com/yodev21/docker_tutorial_rails

Work procedure

File creation

Create the following file

Dockerfile
docker-compose.yml
Gemfile
Gemfile.lock
#Ruby in the 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

#run 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 a pgdatabol volume for data persistence and mount the 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

Stop container

docker-compose stop

Delete container

docker-compose down

Move into container

docker-compose run web bash

Try using the Go language

Directory move

cd doc/golang/
FROM golang:1.9

RUN mkdir /echo
COPY main.go /echo
CMD ["go", "run", "/echo/main.go"]

Build image

docker image build -t example/echo:latest .

Check the image

docker image ls

Start container

docker container run -d -p 9000:8080 example/echo:latest

Confirmation of GET request

curl http://localhost:9000

Recommended Posts

Rails application building with Docker
A memo about building a Django (Python) application with Docker
Deploy a Django application with Docker
Application development with Docker + Python + Flask
Run a Python web application with Docker
Launch Flask application with Docker on Heroku
Tftp server with Docker
Proxy server with Docker
Hello, World with Docker
Launch a Python web application with Nginx + Gunicorn with Docker
Web application development with Flask
WebSocket application with Flask-Socket IO
Web application creation with Django
PySpark life starting with Docker
Prepare python3 environment with Docker
Try Selenium Grid with Docker
Web application with Python + Flask ④
(Note) Notes on building TensorFlow + Flask + Nginx environment with Docker Compose
Building a kubernetes environment with ansible 2
Japaneseize Matplotlib with Alpine using Docker
Until you start Jupyter with Docker
Easy Slackbot with Docker and Errbot
Measure Django application coverage with Coverage.py
Creating a Flask server with Docker
Build a deb file with Docker
Tips for running Go with docker
Build Mysql + Python environment with docker
Monitor Python application performance with Dynatrace ♪
Google App Engine development with Docker
Twitter posting application made with Django
Build PyPy execution environment with Docker
Build a web application with Django
Rebuild Django's development environment with Docker! !! !! !!
Data science environment construction with Docker
Python application: Data cleansing # 2: Data cleansing with DataFrame
Application of graphs with plotly sliders
Automate Windows Application Testing with Windows Application Driver-Python
(Note) Notes on building TensorFlow environment with Docker Compose (only one container)