A memo when building a Rails 5.2 development environment using Docker Desktop + WSL2 on Windows 10 Home

About operating environment

--Edition Windows 10 Home

About Docker Desktop

WSL2 must be installed before installing Docker Desktop.

If you have not installed WSL2, you can refer to the here document.

Install Docker Desktop from the official website below. https://www.docker.com/products/docker-desktop

After the installation is complete, launch Docker Desktop and click the Settings button in the upper right corner. image.png Make sure that General Use the WSL 2 based engine (Windows Home can only run the WSL 2 backend is checked. image.png Then open WSL INTEGRATION in Resources, check the installed WSL2 distribution (Ubuntu-20.04 in my case) and press Apply & Restart.

Reference: https://docs.microsoft.com/ja-jp/windows/wsl/tutorials/wsl-containers

About Rails environment construction

usage environment

Flow from rails new to executing rails s

In the conventional environment, executing the rails new command creates a new directory, but in the case of Docker, you need to create the directory manually in advance. For example, if you type the command rails new sample_app, the sample_app directory will be created, but in the case of Docker, you need to create the sample_app directory first, and the work from now on will be done under the directory created in advance. I will.

File preparation

Create four files, Dockerfile, Gemfile, Gemfile.lock, and docker-compose.yml, in any directory you created in advance. Dockerfile

Dockerfile


FROM ruby:2.7.2
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN curl -sL https://deb.nodesource.com/setup_10.x | bash - \
   && apt-get install -y nodejs
RUN mkdir /myapp
WORKDIR /myapp
ADD Gemfile /myapp/Gemfile
ADD Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
ADD . /myapp

Simply put, the Dockerfile is for fetching Docker images. The Docker image is necessary when creating a Docker container, and the virtual environment is executed based on this. The FROM command is for fetching the required image and should be written at the beginning of the Dockerfile. The RUN command is for executing commands. Set the working directory in the container to myapp with WORKDIR/myapp. With ADD Gemfile/myapp/Gemfile, copy the local Gemfile to myapp/Gemfile in the container.

Gemfile

Gemfile


source 'https://rubygems.org'
gem 'rails', '5.2.4.4'

Gemfile.lock Create an empty file with the name Gemfile.lock.

docker-compose.yml

docker-compose.yml


version: '3'
services:
  db:
    image: mysql:5.7.32
    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
    ports:
      - "3000:3000"
    depends_on:
      - db

This is also briefly explained. docker-compose is a tool for managing multiple containers. This time, we will launch two containers, db (mysql) and web (rails). You can run a container based on this file and set up a virtual environment by executing the docker-compose command only once.

version is the version of docker-compose. You can check the current latest version from the following site. https://docs.docker.com/compose/compose-file/

services is a description required when defining each element (db and web in this case) required to run the application as service. volumes is a description for synchronizing the local (host) and container. ports is for accessing the web server in the container, and this time it is described as" 3000: 3000 ", but this corresponds to port 3000 in the container and port 3000 in the host. It is attached.

Command execution

After creating Dockerfile, Gemfile, Gemfile.lock, docker-compose.yml, change to the directory where you created these files and execute the following command at the command prompt. At that time, you can run it locally without entering the wsl environment.

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

Running this command will create a lot of folders and files. Maybe I get an error with nokogiri related installation, but in my case it was solved by restarting the PC.

Modify config/database.yml

config/database.Partial excerpt from yml


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

Modify password: and host: to the contents described in docker-compose.yml. Then execute the following command.

Command to execute when modifying a local file

docker-compose build

Launch rails server

First, run the following command to create the database.

docker-compose run web rails db:create

Then run the command below.

docker-compose up

Executing this command will start the container as described in docker-compose.yml.

If you access http: // localhost: 3000 /, you will see the usual screen.

In future Rails development using Docker, add docker-compose run web before the rails command.

docker-compose run web rails

I will execute the command like this.

At the end

Since WSL2 can be used as a backend for Docker Desktop for Windows, it has become possible to easily build a Docker environment, so I introduced Docker, which I had been interested in for a long time. I used to develop rails with vagrant, but since I introduced Docker, development has become convenient.

Reference material

https://www.ccs1981.jp/blog/wsl2%E3%81%A8docker%E3%81%A7%E9%96%8B%E7%99%BA%E7%92%B0%E5%A2%83%E3%82%92%E4%BD%9C%E3%82%8B/ http://docs.docker.jp/v17.06/engine/reference/builder.html https://qiita.com/yuta-ushijima/items/d3d98177e1b28f736f04

Recommended Posts

A memo when building a Rails 5.2 development environment using Docker Desktop + WSL2 on Windows 10 Home
Using Docker on Windows10 Home WSL2
Building a haskell environment with Docker + VS Code on Windows 10 Home
I tried using Docker Desktop for Windows on Windows 10 Home
Build a Doker-based development environment on Windows 10 Home 2020 ver. Part 1 Until WSL2-based Docker build
Install Ubuntu 20.04 in virtual box on windows10 and build a development environment using docker
Difficulties in building a Ruby on Rails environment (Windows 10) (SQLite3)
Procedure for building a Rails application development environment with Docker [Rails, MySQL, Docker]
Build a development environment where Ruby on Rails breakpoints work on Windows
Building a Kotlin development environment using SDKMAN
Introducing Docker Desktop for Windows on WSL2
[Ruby] Building a Ruby development environment on Ubuntu
How to build a Ruby on Rails environment using Docker (for Docker beginners)
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)
Build a development environment for Docker + Rails6 + Postgresql
[Personal memo] Ruby on Rails environment construction (Windows)
Operate Docker Desktop for Windows on Linux (WSL)
Build a Docker-based development environment on Windows 10 Home 2020 ver. Part 2 VS Code should make the Docker development environment comfortable
[First environment construction] I tried to create a Rails 6 + MySQL 8.0 + Docker environment on Windows 10.
A memorandum when building an environment with Ruby3.0 x Rails6.1 x Docker x CentOS Stream
Create a Java development environment using jenv on Mac
I built an Ubuntu environment on Windows 10 using WSL2.
Docker the development environment of Ruby on Rails project
Easily build Redmine on Windows using WSL2 and Docker
Try to build a Java development environment using Docker
Create Chisel development environment with Windows10 + WSL2 + VScode + Docker
[For beginners] Until building a Web application development environment using Java on Mac OS
WSL2 + VSCode + Docker development environment
Docker × Java Building a development environment that is too simple
I made a development environment with rails6 + docker + postgreSQL + Materialize.
Measures for permissions when building MySQL with Docker on WSL2
Build Go development environment with WSL2 + Docker Desktop + VSCode (Remote --Containers)
Until you put Ubuntu 20 on Windows 10 Home and WSL2 and run Docker
Ruby on Rails --From environment construction to simple application development on WSL2
Ruby on Rails development environment construction with Docker + VSCode (Remote Container)
How to install Pry after building Rails development environment with Docker
Ruby on Rails 6.0 environment construction memo
Build a browser test environment using Capybara in the Docker development environment
Rails on Docker environment construction procedure
Try Docker on Windows Home (September 2020)
Create a development environment for Ruby 3.0.0 and Rails 6.1.0 on Ubuntu 20.04.1 LTS
Steps to build a Ruby on Rails development environment with Vagrant
Build Unity development environment on docker
The story of fighting ubuntu errors 0x80370114, 0x80000000 to enable docker desktop with wsl2 on windows10 home
What I checked when I installed Docker Hub in a Windows 10 home environment but it did not start
Build debug environment on container --Build local development environment for Rails tutorial with Docker-
Let's install Docker on Windows 10 and create a verification environment for CentOS 8!
Clone your own web app on GitLab when building a Docker image
Building a development environment for Flutter on Win10 --- Flutter SDK Install (2020 preservation version)
Build a local development environment for Rails tutorials with Docker (Rails 6 + PostgreSQL + Webpack)
Template: Build a Ruby / Rails development environment with a Docker container (Ubuntu version)
When building rails6 environment on Ubuntu, it gets stuck with bundle install
Installing Docker Desktop on Windows 10 Home was easy and easy (as of December 2020)
Template: Build a Ruby / Rails development environment with a Docker container (Mac version)
Create a web environment quickly using Docker
Windows Docker: Disk pressure on WSL files
Building Rails 6 and PostgreSQL environment with Docker
Building a Lambda development environment in Eclipse
Build a PureScript development environment with Docker
Using JupyterLab + Java with WSL on Windows 10