[RAILS] Docker environment construction

Docker environment construction

I had a hard time with meta meta, so I will leave it as a memo Also, docker's Official is the strongest There are many parts that I do not fully understand, so I would appreciate it if you could tell me any descriptions that should be corrected. rails 6 MySQL

procedure

Generate a directory with any name in any directory

As it is

Generation and description of Dockerfile

Move to the directory generated above in the terminal

Terminal


% touch Dockerfile

Dockerfile


FROM ruby:2.6.5
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 \
    && apt-get update -qq \
    && apt-get install -y nodejs yarn \
    && mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp

COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

CMD ["rails", "server", "-b", "0.0.0.0"]

Gemfile settings / description

Terminal


% touch Gemfile

Gemfile


source 'https://rubygems.org'
gem 'rails', '~>6'

Generating Gemfile.lock

Terminal


% touch Gemfile.lock

The contents can be empty

Generation / description of entrypoint.sh

Terminal


% touch entrypoint.sh

entrypoint.sh


#!/bin/bash
set -e

# Remove a potentially pre-existing server.pid for Rails.
rm -f /myapp/tmp/pids/server.pid

# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"

Generate and describe docker-compose.yml

Terminal


% touch docker-compose.yml

docker-compose.yml


version: '3'
services:
  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: password
    ports:
      - '3306:3306'
    command: --default-authentication-plugin=mysql_native_password
    volumes:
      - mysql-data:/var/lib/mysql
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db
    stdin_open: true
    tty: true
volumes:
  mysql-data:
    driver: local

Rails application generation

Terminal


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

Modify database.yml

config/database.yml


default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: 5
  username: root
  password: password
  host: db

development:
  <<: *default
  database: myapp_development

test:
  <<: *default
  database: myapp_test

Rebuild Docker

It is necessary to execute this command when writing in Gemfile and Dockerfile

Terminal


% docker-compose build

Creating a database

Terminal


% docker-compose run web rails db:create

Launch the app

Terminal


% docker-compose up

Complete if the welcome page is always displayed on localhost: 3000 You may be required to install webpacker etc. on the way

Recommended Posts

Docker environment construction
Rails Docker environment construction
MySQL 5.7 (Docker) environment construction memo
Redmine (Docker) environment construction memo
[Docker] Rails 5.2 environment construction with docker
Docker × Spring Boot environment construction
[Docker] postgres, pgadmin4 environment construction
React environment construction with Docker
Rails + MySQL environment construction with Docker
Node.js environment construction with Docker Compose
Environment construction with Docker for beginners
Laravel + Docker Laradock usage environment construction
[Java] Environment construction
Rails on Docker environment construction procedure
[Environment construction with Docker] Rails 6 & MySQL 8
[Spring] Environment construction
SQL statement learning ~ Environment construction ~ Docker + MySQL
Rails environment construction with Docker (personal apocalypse)
Laravel development environment construction with Docker (Mac)
Sapper × Go (echo) × Docker development environment construction
Environment construction with Docker (Ubuntu20.04) + Laravel + nginx
[Docker] Development environment construction Rails6 / Ruby2.7 / MySQL8
Spring Boot + Docker Java development environment construction
Laravel + MySQL + phpMyadmin environment construction with Docker
Penronse environment construction [Windows]
[Environment construction] Eclipse installation
[Flutter] Ubuntu 20.04 environment construction
Circle CI environment construction
java development environment construction
Spring Boot environment construction with Docker (January 2021 version)
Docker + DynamoDB local + C ++ environment construction and practice
Environment construction command memo with Docker on AWS
Kaggle environment construction using official Docker and vscode
Rails6 [API mode] + MySQL5.7 environment construction with Docker
A reminder of Docker and development environment construction
React + Django + Nginx + MySQL environment construction with Docker
Wordpress local environment construction & development procedure with Docker
Pytorch execution environment with Docker
Rails6 development environment construction [Mac]
[Spring Boot] Environment construction (macOS)
WSL2 + VSCode + Docker development environment
EC-CUBE4 environment construction (local edition)
[Docker] Create Elasticsearch, Kibana environment!
I tried node-jt400 (Environment construction)
Rails environment construction Rails5.2.1 ruby2.5.1 Catalina
BEAR application Docker development environment construction example (docker-sync, Mutagen)
virtulbox + vagrant + Docker + nginx + puma + MySQL Rails environment construction
Introduction to Metabase ~ Environment Construction ~
JavaFX environment construction in Java 13
AtCoder Challenge Environment Construction (Java 8)
Build docker environment with WSL
Offline environment construction Ruby edition
[Super easy] Ruby environment construction
Laravel environment construction (Ubuntu 18.04 LTS)
Database environment construction with Docker in Spring boot (IntellJ)
Offline environment construction CentOS edition
Construction of data analysis environment using Docker (personal memorandum)
docker
Easy environment construction of MySQL and Redis with Docker and Alfred
[Portfolio] Manage site with laravel APP implementation (Docker environment construction)
Rails application development environment construction with Docker [Docker, Rails, Puma, Nginx, MySQL]