Build Rails environment with Docker Compose

I found a video of building a Rails environment using Docker Compose on the channel of Kiyoto Programming University, which is sent to beginners, and it was very easy to understand, so I would like to summarize it as a memorandum.

If you want to create a Rails environment with Docker, you can watch this video. https://youtu.be/ltDdZAJli8c

What is Docker Compose?

A tool that allows you to operate multiple applications at once. For example, when developing an application with Rails, a web server and a database server are required at a minimum, but usually operations such as building and connecting each server are required. With docker-compose, you can launch and connect them with a single command, so you can build a development environment very easily.

Overall flow

--Prepare Docker related files

Prepare Docker related files

① Dockerfile Here is the description to create an image of rails.

Dockerfile



#Specify base image
FROM ruby:2.7

#Install JavaScript related libraries to install nodejs and yarn
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
  && echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list \
  #Install nodejs and yarn
  && apt-get update -qq \
  && apt-get install -y nodejs yarn
#Specify working directory
WORKDIR /app
COPY ./src /app
#Ruby related libraries(gem)Installation of
RUN bundle config --local set path 'vendor/bundle' \
  && bundle install

② Gemfile Next, prepare a Gemfile.

Gemfile


source 'https://rubygems.org'

gem 'rails', '~> 6.1.0'

As mentioned above, if you execute rails new with the rails library installed, you can create a template of rails files all at once. At that time, a new Gemfile will be created to replace it.

③ docker-compose.yml Finally, prepare docker-compose.yml.

docker-compose.yml


ersion: '3'
services:
  db:
    image: mysql:8.0
    # mysql8.The authentication format has changed from 0, and it is set to return to the 5 system authentication format. Failure to do this will result in an error
    command: --default-authentication-plugin=mysql_native_password
    volumes:
      - ./src/db/mysql-data:/var/lib/mysql
    #Setting environment variables
    #MySQL will give an error if you don't set a password
    environment:
      MYSQL_ROOT_PASSWORD: password
  web:
    build: .
    #Settings to enable standard I / O
    #If you don't do this, you can't debug
    tty: true
    stdin_open: true
    command: /bin/sh -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - ./src:/app
      #Settings that do not need to be rebuilt when gem is inserted
      - bundle:/usr/local/bundle
    ports:
      - "3000:3000"
    #When connecting from rails to MySQL, it is necessary to specify the IP address of the db service as the connection information. Settings that allow the IP address of the connection destination to be connected with db
    depends_on:
      - db
volumes:
  bundle:
    driver: local

During development, I sometimes get addicted to the A server is already running. Check /app/tmp/pids/server.pid. error. This happens when the presence or absence of server.pid determines whether the server is running, and for some reason the server is not running but the server.pid file remains. To prevent this, delete server.pid at startup.

command: /bin/sh -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"

Initial setting

Run the rails new command when the file is ready.

$ docker-compose run web rails new . --force --database=mysql

Image rebuild due to changes to Gemfile

$ docker-compose build

Refer to config/database.yml and modify the connection destination information to the database.

database.yml


default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  # docker-compose.Enter the password set in yml
  password: password
  # docker-compose.yml depends_Enter the value set in on
  host: db
  #The following is omitted

Start-up

$ docker-compose run --rm web rails db:create
$ docker-compose up -d

Access localhost: 3000 with a browser, and if the lower screen is displayed, it is successful! image.png

Frequently used commands

--Build image

$ docker-compose build

--Creating and starting a container

$ docker-compose up -d

--Stop and delete the container

$ docker-compose down

--Display the list of containers

$ docker-compose ps

--View log

docker-compose logs

--Create a container and execute a command

$ docker-compose run <service> <command>

--Command execution on the running container

$ docker-compose exec <service> <command>

Recommended Posts

Build Rails environment with Docker Compose
Build environment with vue.js + rails + docker
Create Rails 6 + MySQL environment with Docker compose
How to build Rails 6 environment with Docker
[Docker] Rails 5.2 environment construction with docker
Build docker environment with WSL
[Rails] How to build an environment with Docker
Node.js environment construction with Docker Compose
Build Couchbase local environment with Docker
Build a Node.js environment with Docker
Build PlantUML environment with VSCode + Docker
[Environment construction with Docker] Rails 6 & MySQL 8
Build Rails (API) x MySQL x Nuxt.js environment with Docker
Build docker + laravel environment with laradock
How to build Rails, Postgres, ElasticSearch development environment with Docker
Rails environment construction with Docker (personal apocalypse)
Building Rails 6 and PostgreSQL environment with Docker
Build a PureScript development environment with Docker
Build a Wordpress development environment with Docker
[Docker] Build Jupyter Lab execution environment with Docker
Build an environment with Docker on AWS
Build TensorFlow operation check environment with Docker
Rails Docker environment construction
How to build Rails + Vue + MySQL environment with Docker [2020/09 latest version]
Build a development environment for Django + MySQL + nginx with Docker Compose
Build a development environment for Docker + Rails6 + Postgresql
Build a Laravel / Docker environment with VSCode devcontainer
Rails6 [API mode] + MySQL5.7 environment construction with Docker
Notes on building Rails6 / PostgreSQL with Docker Compose
Build a simple Docker Compose + Django development environment
Build mecab (NEologd dictionary) environment with Docker (ubuntu)
[First team development ②] Build an environment with Docker
Build debug environment on container --Build local development environment for Rails tutorial with Docker-
[Copy and paste] Build a Laravel development environment with Docker Compose Part 2
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
Build a local development environment for Rails tutorials with Docker (Rails 6 + PostgreSQL + Webpack)
[Copy and paste] Build a Laravel development environment with Docker Compose Participation
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
Template: Build a Ruby / Rails development environment with a Docker container (Ubuntu version)
Template: Build a Ruby / Rails development environment with a Docker container (Mac version)
Pytorch execution environment with Docker
Run Rails whenever with docker
React environment construction with Docker
Build DynamoDB local with Docker
[Docker] Use whenever with Docker + Rails
How to build docker environment with Gradle for intelliJ
Build an environment of Ruby2.7.x + Rails6.0.x + MySQL8.0.x with Docker
Easily build a Vue.js environment with Docker + Vue CLI
[Note] Build a Python3 environment with Docker in EC2
Build Java development environment with WSL2 Docker VS Code
Build WordPress environment with Docker (Local) and AWS (Production)
Create a Vue3 environment with Docker!
I made a development environment with rails6 + docker + postgreSQL + Materialize.
Build a Tomcat 8.5 environment with Pleiades 4.8
Environment construction with Docker for beginners
[Rails] [Docker] Copy and paste is OK! How to build a Rails development environment with Docker
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Express ~
Create SolrCloud verification environment with Docker
WordPress with Docker Compose on CentOS 8
Rails on Docker environment construction procedure
Create Laravel environment with Docker (docker-compose)