How to build an environment with Docker, which is the minimum required to start a Rails application

Introduction

Purpose

The purpose is to start the Rails application with the minimum required implementation while building the Docker environment. In this article, you can also build a Ruby on Rails environment that supports Rails 6 with Docker. It is possible.

Own skill level

I've been using Rails for about half a year and Docker for about a month. I'm used to touching it, but there are some parts that I don't understand, so I would appreciate it if you could point out any inappropriate expressions.

Target audience

"I touched Rails, but I don't know what to start with." "I often hear Docker, but it seems difficult and I don't know where to start." "I want to make something using Docker for the time being!"

I am the same programming beginner as me. I had a more difficult image of Docker than before, but I was able to actually work on it and dispel that image. I will try to implement it with the minimum implementation first and start the application.

Environment construction procedure

1. Preparation for creating necessary folders and files

1-1. Creating a working folder

First, create a working folder for developing the app with mkdir in any location. Suppose you want to create a folder named "work_space".

Use the cd command to move to the working folder.

$ cd work_space(= Created folder name)

In this working folder, save the files necessary for building the rails and Docker environment.

1-2. Create Dcokerfile

Create a Dockerfile in the folder created in 1-1 (the same applies to the following files). This Dockerfile contains the steps to build a Docker image.

Create the Ruby version as follows in the latest 2.7.2 (as of December 2020).

Dockerfile


FROM ruby:2.7.2-alpine3.12

RUN apk update && apk upgrade && apk add build-base \
                                         curl \
                                         curl-dev \
                                         less \
                                         linux-headers \
                                         libc6-compat \
                                         libxml2-dev \
                                         libxslt-dev \
                                         mariadb-dev \
                                         pcre-dev \
                                         nodejs \
                                         ruby-dev \
                                         tzdata \
                                         yaml-dev \
                                         zlib-dev

RUN mkdir /app
WORKDIR /app

ADD Gemfile /app/Gemfile
ADD Gemfile.lock /app/Gemfile.lock
RUN gem update bundler && bundle install

ADD . /app

EXPOSE 3000
CMD bundle exec rails s

I will briefly explain the contents. ** [FROM] ** Image and version to use ** [RUN] ** Command execution Install the package required to start Rails

1-3. Create Docker compose

docker-compose.yml is a file that describes each service that makes up an application and is the basis for executing them all at once. Here, "db (specify MYSQL)" and "web (specify application, port, etc.)" are described.

Dockecompose


version: '3'
services:
  app:
    build: .
    command: /bin/sh -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    ports:
      - 3000:3000
    depends_on:
      - db
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: root
    ports:
      - 3306:3306
    volumes:
      - db-store:/var/lib/mysql
volumes:
  db-store:

** [version] ** This is the version of docker-compose. As of December 2020, the latest is '3'. ** [services] ** Let's create services in the hash below. This time the name is app I named it db. ** [image] ** image to use (mysql5.7 is specified in db) ** [environment] ** Setting environment variables in the container ** [volumes] ** Directory mount settings (db can be made persistent) ** [build] ** Path with Dockerfile etc. (basically current directory) ** [command] ** command (delete the server.pid file and then start the rails server) ** [ports] ** Port number. Set in [Host: Container]. ** [depends_on] ** Shows dependencies and allows you to specify the startup order. Here, start from db → web

1-4. Create Gemfile

gem 'rails', '~> 6'

Specify the version of Rails to install. Here, it is compatible with Rails 6. The contents of the Gemfile will be rewritten later by doing rails new.

2. Launch the app

2-1.rails new app creation

Create an empty app with rails new under the working directory created earlier.

rails new sample_app

Put the Docker file and Docker compose file created earlier under the rails app I will move it. The final folder structure should look like the one below.

work_space
  sample_app
   .....
   - Dockerfile
   - Gemfile
   - docker-compose.yml
   .....

2-2 Editing Gemfile

The Gemfile has been rewritten by rails new earlier. Since this database connection is MYSQL, edit the inside of Gemfile.

gem 'sqlite3'

gem 'mysql2'

2-3.bundle install

Bundle install based on the contents of the Gemfile you edited earlier. If you execute the following command, bundle install will be done when building the Docker image.

$ docker-compose build

2-4. DB connection settings

Rewrite the Rails config/database.yml file as follows.

rconfig/database.yml


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

development:
  <<: *default

test:
  <<: *default

production:
  <<: *default

2-5.Docker Start the container and application with the following command.

$ docker-compose up

Check if the last container is running with the following command. If the container is running It's a success.

$ docker-compose ps

2-6. Make sure Rails is running

When I access http: // localhost: 3000/with a browser, Rails is launched successfully.

スクリーンショット 2020-12-31 16.44.44.png

Summary

I built an environment for Ruby on Rails that supports Rails 6 with Docker. The goal was to launch the app with minimal implementation for those who started Docker. If you understand each process, it will not be so difficult. I hope it will be a reference article for people like me.

List of reference sites

I referred to the following article. Thank you very much.

https://knowledge.sakura.ad.jp/13265/ https://qiita.com/daichi41/items/dfea6195cbb7b24f3419

Recommended Posts

How to build an environment with Docker, which is the minimum required to start a Rails application
[Rails] How to build an environment with Docker
How to build Rails 6 environment with Docker
[Rails] [Docker] Copy and paste is OK! How to build a Rails development environment with Docker
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
How to build Rails, Postgres, ElasticSearch development environment with Docker
How to install Docker in the local environment of an existing Rails application [Rails 6 / MySQL 8]
How to build Rails + Vue + MySQL environment with Docker [2020/09 latest version]
How to build a Ruby on Rails environment using Docker (for Docker beginners)
How to make an application with ruby on rails (assuming that the environment has been built)
I tried to build a Firebase application development environment with Docker in 2020
How to build an environment of [TypeScript + Vue + Express + MySQL] with Docker ~ Vue edition ~
How to build docker environment with Gradle for intelliJ
[Docker] How to see the contents of Volumes. Start a container with root privileges.
How to quit Docker for Mac and build a Docker development environment with Ubuntu + Vagrant
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Express ~
Build a Node-RED environment with Docker to move and understand
Build a Node.js environment with Docker
Build Rails environment with Docker Compose
How to start Camunda with Docker
I tried to build the environment of PlantUML Server with Docker
Super beginner builds Rails6 + Postgresql environment with Docker to the end
Procedure for building a Rails application development environment with Docker [Rails, MySQL, Docker]
How to install Pry after building Rails development environment with Docker
Steps to build a Ruby on Rails development environment with Vagrant
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Sequelize ~
One file of Docker x Laravel threat! Build a local development environment with the minimum configuration
Build a PureScript development environment with Docker
[Rails] How to use rails console with docker
Build a Wordpress development environment with Docker
Build an environment with Docker on AWS
[Docker] How to create a virtual environment for Rails and Nuxt.js apps
Build a local development environment for Rails tutorials with Docker (Rails 6 + PostgreSQL + Webpack)
[Docker] How to build when the source code is bind-mounted on the container
How to migrate a web application created in a local docker environment to AWS
Template: Build a Ruby / Rails development environment with a Docker container (Ubuntu version)
How to start a Docker container with a volume mounted in a batch file
Template: Build a Ruby / Rails development environment with a Docker container (Mac version)
About the matter that tends to be confused with ARG of Dockerfile which is a multi-stage build
Build a development environment for Docker + Rails6 + Postgresql
Build a Laravel / Docker environment with VSCode devcontainer
Build a WordPress development environment quickly with Docker
Rails6.0 ~ How to create an eco-friendly development environment
How to build API with GraphQL and Rails
How to build a Pytorch environment on Ubuntu
[First team development ②] Build an environment with Docker
[Error resolution] Occurs when trying to build an environment for spring with docker
A memorandum when building an environment with Ruby3.0 x Rails6.1 x Docker x CentOS Stream
[Rails] How to operate the helper method used in the main application with Administrate
Build a development environment to create Ruby on Jets + React apps with Docker
What to do if you get an "A server is already running." Error when you try to start the rails server
How to push an app developed with Rails to Github
Build an environment of Ruby2.7.x + Rails6.0.x + MySQL8.0.x with Docker
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (5)
How to make an almost static page with rails
Easily build a Vue.js environment with Docker + Vue CLI
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (6)
[Docker environment] How to deal with ActiveSupport :: MessageEncryptor :: InvalidMessage
How to use git with the power of jgit in an environment without git commands
[Note] Build a Python3 environment with Docker in EC2