[Environment construction] Rails + MySQL + Docker (Beginners can also use it in 30 minutes!)

Environment construction using Docker

I've heard that building an environment is more difficult than writing code.

Docker is easy!

And Why,,, ** Just copy and paste ** However, it may take some time to understand ... Let's start with a brief overview

Docker Tools for building virtual environments

container

Virtual environment itself (Docker runs a container on top of the Docker engine)

image

What you need to run a Docker container.

Environment construction procedure

① .``` Create a project (directory) and move it `` `

$ mkdir app name $ cd app name ② . Create Dockerfile, docker-compose.yml, Gemfile, Gemfile.lock

$ touch Dockerfile docker-compose.yml Gemfile Gemfile.lock

③. `Open the editor and copy and paste Dockerfile, docker-compose.yml, Gemfile from the following` Dockerfile A file that automatically generates a Docker image

FROM ruby:2.5
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs

RUN mkdir /myapp
WORKDIR /myapp

COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock

RUN bundle install
COPY . /myapp

FROM: Image and command to use RUN: Command execution WORKDIR: Working directory settings COPY: Specify the file or directory of the copy source (host side) and copy destination (virtual environment side)

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

docker-compose.yml version: '3'

services:
    db:
        image: mysql:5.7
        environment:
            MYSQL_USER: root
            MYSQL_ROOT_PASSWORD: password
        ports:
            - "3306:3306"
        volumes:
            - ./db/mysql/volumes:/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
            - gem_data:/usr/local/bundle
        ports:
            - 3000:3000
        depends_on:
            - db 
        tty: true
        stdin_open: true

volumes:
  gem_data:

version: The version of docker-compose. depends_on: Shows dependencies and allows you to control the boot order. Here, start to "db → web".

What is Docker compose?

Docker compose is a function that automates the procedure for building and executing a service consisting of multiple containers and facilitates management. With Docker compose, you can prepare a compose file and execute the command once to read the settings from that file and start all containers.

About service

In Docker compose, each element to run the application is called service. Usually named web (rails) and db (mysql).

About `` `rm -f tmp / pids / server.pid```

The pid is the process ID. The pid is written to tmp / pids / server.pid when you start the development web server and is deleted when you exit. If pid is written in server.pid, the server will be judged as ** starting **.

About ports

ports:
  - 3000:3000

The above means that port 3000 in the container is mapped to 3000 on the host. This will allow you to access the web server inside the container at `` `http: // localhost: 3000```.

About volume

volumes:
   - ./db/mysql/volumes:/var/lib/mysql

Means to mount ./db/mysql/volumes on the host to / var / lib / mysql inside the container. Simply put, the Docker container is ** synchronized ** locally.

volumes :
   - .:/myapp

Means all directories on the host with `.``` and mounts it on `myapp``` in the container.

④ .``` Execute the following command `` `

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

The run command will do everything from building an image to building and starting a container. Service must be specified in the argument. Since the volumes:-.:/Myapp part of docker-compose.yml is set to synchronize docker with the local directory, a similar file will be generated locally at the same time after executing this command. ..

⑤. ``` Change to (password: password, host: db) in config / database.yml`

default: &default
   adapter: mysql2
   encoding: utf8
   pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
   username: root

password: password Added #password host: db Changed from #localhost to db

⑥. `Execute the following command`

$ docker-compose build

Run `` `build``` when the Gemfile is updated

⑦ .``` Execute the following command `` `

$ docker-compose up -d

The container starts according to docker-compose.yml. Also executed when reflecting changes in docker-compose.yml. The `` `-d``` option starts in the background.

⑧. ``` Execute the following command

$ docker-compose run web rails db:create

Create a database. You can execute commands locally with docker-compose run web. (No need to enter the container)

⑨. ``` Execute the following command and check if the two containers are up. `` `(Completed!)

$ docker ps

A command to display the currently running container. When you access localhost: 3000, you will see the usual Yay! You're on Rails !.

`If you want to delete the container, do the following`

$ docker-compose down

Check if the container has been deleted with `docker ps```. To create and start a container, execute docker-compose up -d ``.

Summary

I think it was pretty easy! Let's develop the app with this! : thumbsup:

Recommended Posts

[Environment construction] Rails + MySQL + Docker (Beginners can also use it in 30 minutes!)
Rails + MySQL environment construction with Docker
[Environment construction with Docker] Rails 6 & MySQL 8
[Docker] Development environment construction Rails6 / Ruby2.7 / MySQL8
Rails Docker environment construction
Rails6 [API mode] + MySQL5.7 environment construction with Docker
virtulbox + vagrant + Docker + nginx + puma + MySQL Rails environment construction
Beginners use ubuntu in windows to prepare rails environment
MySQL 5.7 (Docker) environment construction memo
[Docker] Rails 5.2 environment construction with docker
[Rails / MySQL] Mac environment construction
For those who want to use MySQL for the database in the environment construction of Rails6 ~.
Rails application development environment construction with Docker [Docker, Rails, Puma, Nginx, MySQL]
Environment construction with Docker for beginners
Rails on Docker environment construction procedure
Check MySQL logs in Docker environment
"Rails 6 x MySQL 8" Docker environment construction procedure for sharing with teams
SQL statement learning ~ Environment construction ~ Docker + MySQL
[Docker] Use environment variables in Nginx conf
Rails environment construction with Docker (personal apocalypse)
Create Rails 6 + MySQL environment with Docker compose
Use docker in proxy environment on ubuntu 20.04.1
Edit Mysql with commands in Docker environment
How to use MySQL in Rails tutorial
Laravel + MySQL + phpMyadmin environment construction with Docker
Show Better Errors in Rails + Docker environment
Rails & React & Webpacker & MySQL Environment Construction Manual
Understand in 5 minutes !! How to use Docker
[Rails & Docker & MySQL environment construction] I started the container, but I can't find MySQL ...?
[Rails & Docker & MySQL environment construction] Could not find gem ‘mysql2 (> = 0.4.4, <0.6.0)’ in any of the gem sources listed in your Gemfile.
[First environment construction] I tried to create a Rails 6 + MySQL 8.0 + Docker environment on Windows 10.
(Basic authentication) environment variables in rails and Docker
React + Django + Nginx + MySQL environment construction with Docker
Docker environment construction
[Rails] How to use PostgreSQL in Vagrant environment
How to install Docker in the local environment of an existing Rails application [Rails 6 / MySQL 8]
[Rails] How to delete production environment MySQL data after putting it in the development environment
[Even beginners can do it! ] How to install Eclipse on Windows 10 (Java environment construction)
Migration error after Activerecord association in Rails5 + Docker environment (2)
Build Docker + Laravel PHP + Vue.js development environment in 5 minutes
Migration error after Activerecord association in Rails5 + Docker environment
Environment construction of Rails5 + MySQL8.0 + top-level volumes with docker-compose
SSL in the local environment of Docker / Rails / puma
Build Rails (API) x MySQL x Nuxt.js environment with Docker
Database environment construction with Docker in Spring boot (IntellJ)
Use images in Rails
Create a MySQL test environment (+ millions of test data) in 5 minutes
[Procedure 1 for beginners] Ruby on Rails: Construction of development environment
Easy environment construction of MySQL and Redis with Docker and Alfred
Exit code 1 occurs when Rails is stopped in Docker environment
Rails6 development environment construction [Mac]
Rails engineer environment construction ruby2.7.1
Rails6 (MySQL, Ubuntu environment, Cloud9)
Rails environment construction Rails5.2.1 ruby2.5.1 Catalina
JavaFX environment construction in Java 13
Redmine (Docker) environment construction memo
Docker × Spring Boot environment construction
Use multiple checkboxes in Rails6!
[Docker] postgres, pgadmin4 environment construction
React environment construction with Docker
[Docker] Use whenever with Docker + Rails