--Edition Windows 10 Home
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. Make sure that General Use the WSL 2 based engine (Windows Home can only run the WSL 2 backend is checked. 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
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.
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.
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.
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.
docker-compose build
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.
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.
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