[RUBY] I tried migrating the portfolio created on Vagrant to the Docker development environment

Introduction

Nice to meet you, this is my first post, so I would like to write about myself and the reason for the article.

--The trigger for the article I don't know if other programming schools are the same, but in order to make it a common development environment for all school students, I built a virtual environment on vagrant and created a curriculum and portfolio. Therefore, if you have never built an environment on your own from scratch and are conscious of practical work, I wanted to put in Docker on my own and started learning. I'm still studying, so honestly, I think there are many suspicious points, but I'd be happy if it could be a reference for those who are studying in the same way!

What to do in this article

--Building a development environment using Docker It is an image that makes it possible to do what was done on Vagrant on Docker

--Notation such as Dockerfile, docker-compose, etc. For reference, the development environment when using Vagrant is as follows

スクリーンショット 2020-09-13 12.40.05.png

—— Possible errors in building a development environment from 0 and how to resolve them As mentioned in the beginning of the article, there were some errors before docker because it was the first time to build a local environment. I would like to mainly introduce the articles that I referred to for the solution.

Not covered in this article

--Basic knowledge of Dockerfile, docker-compose, etc. There are many articles and youtube about Docker, but most of them can be copied and pasted, so at first I thought it would be easier to understand by reading the document and moving my hand to grasp the whole mechanism first. For reference, I started studying at the following site. It is recommended because you can get an idea of Docker and what you can do in half a day to two days while looking up each term.

-Introduction Docker: Basic knowledge and operation of docker -Quickstart: Compose and Rails: How to start docker specifically with Ruby on Rails ――For the selection of learning method, I referred to youtube of KENTA / omnivorous engineer TV. Learning order and teaching materials for incorporating AWS, Docker and CircleCI into your portfolio

Advance preparation

--Installing Docker Let's do it from this official website https://hub.docker.com/editions/community/docker-ce-desktop-mac
It's okay if you can check the version in the terminal


$ docker version
Client: Docker Engine - Community
 Cloud integration  0.1.18
 Version:           19.03.13
 API version:       1.40

--Git clone the portfolio (the project you want to use as the Docker development environment) As you can see in the article, let's bring what you want to build the environment locally from github because you are already in the stage of creating a portfolio.

$ mkdir docker
 #Execute from Desktop or in the work directory when using vagrant
$ git clone [email protected]:username/Repository name.git

Preparation of each file

Now that Docker and the project are ready, we will create the files necessary for building the environment.

①Dockerfile


#On the directory you just cloned
$ touch Dockerfile

Let's fill in the following contents

#Specify the original docker image. Match with the ruby version of your portfolio
FROM ruby:2.5.7

RUN apt-get update -qq && apt-get install -y nodejs postgresql-client

RUN apt-get update && apt-get install -y nodejs --no-install-recommends && rm -rf /var/lib/apt/lists/*
#Specify sqlite3
RUN apt-get update && apt-get install -y sqlite3 --no-install-recommends && rm -rf /var/lib/apt/lists/*
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs

#Create myapp directory in the container when you start the container
RUN mkdir /myapp

#Specifying the working directory in the container(Directory created by RUN above)
WORKDIR /myapp

#Copy the gemfile of the host machine and paste it into a directory inside the container
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock

#Read the pasted gemfile. This is required if the bundler version is 2 or later
ENV BUNDLER_VERSION 2.1.4
RUN gem install bundler
RUN bundle install

#application(Current directory)And paste it into a directory inside the container
COPY . /myapp

#Publish port at 3000
EXPOSE 3000

#Execute command in container
CMD ["rails", "server", "-b", "0.0.0.0", "-p", "3000"]

--About ENV BUNDLER_VERSION 2.1.4 I think there is such a notation at the bottom of Gefile.lock, so please check it.

Gemfile.lock


BUNDLED WITH
   2.1.4

②docker-compose.yml


#On the same directory
$ touch docker-compose.yml

Let's fill in the following contents

docker-compose.yml


version: '3'
services:
  web:
    build:
      context: ./
      dockerfile: Dockerfile 
    #Specify the file to save your changes
    volumes:
      - .:/myapp
    #Specify port
    ports:
      - "3000:3000"
    #Keep the container running
    tty: true
    stdin_open: true

If you are using MySQL or PostgreSQL, you need to write web + DB, but in the case of SQLite, this is all you need. I don't have much knowledge about this area yet, so I'd like to change to MySQL eventually ...

Let's start the container

#Create docker image based on Dockerfile
$ docker-compose build 

#Launch docker container in the background
$ dokcer-compose up -d

#Update migration in container
docker-compose run web rails db:migrate

Now, if you can display it at the usual [localhost: 3000](http: // localhost: 3000 /), it's a paragraph!

Caught error and how to resolve it

In general, I will list them in chronological order when they are caught. As mentioned at the beginning, the content of the error and the consideration of the probable cause are sub, and the main article will be the reference for the solution. Thank you very much

①ActiveRecord::PendingMigrationError Migrations are pending "It works with docker-compose up -d ...!"

$ docker-compose up -d                                                  
Creating network "~~~_default" with the default driver
Creating ~~~_web_1 ... done

Error when accessing the page of localhost: 3000 After starting the container, let's do docker-compose run web rails db: migrate, I feel like there is Reference: ActiveRecord :: PendingMigrationError Migrations are pending with Rails launched with docker-compose up

②To install the missing version, run 'gem install bundler:2.1.4' "I can't do commands that start with bundle (deploy, Rspec, etc.) ...!"

Traceback (most recent call last):
    2: from 
    1: from 
~
Could not find 'bundler' (2.1.4) required by your /Users/username/Directory name/Gemfile.lock. (Gem::GemNotFoundException)
To update to the latest version installed on your system, run `bundle update --bundler`.
To install the missing version, run `gem install bundler:2.1.4`

It is an image that "BUNDLED WITH 2.1.4" of Gemfile.lock, which was confirmed when the Dockerfile was created, cannot be found locally. Reference: [What to do when To install the missing version, run gem install bundler: 2.1.4 appears

Try installing according to the error statement, if it fails, go to ③

gem install bundler -v 2.1.4

③ERROR: While executing gem ... (Gem::FilePermissionError)

ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory.

"You don't have permission to install gems ...!" There were many articles about doing it with sudo in mind, but I think it's better to think about the future by using a version control tool called "rbenv" rather than installing it as it is.

Reference 1: What to do when a permission error occurs in gem install Reference 2: From Ruby installation by rbenv to Hello, World!

When rbenv is ready, let's try again

gem install bundler -v 2.1.4

④ I can't bundle install

"Bundle ... isn't good yet ...!"

When gem install is done, when I enter "bundle ~" system such as deploy, an error message still appears

Install missing gem executables with `bundle install`

If you try bundle install according to the error statement

#Because it is long, only the last part is excerpted
An error occurred while installing mysql2 (0.5.3), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/'` succeeds before bundling.

In Gemfile:
  mysql2

It's an error around mysql, but I didn't know the cause by myself. I was able to solve it according to the article that I referred to below, I am really grateful.

Reference: [Rails] What to do if you can't bundle install MySQL2

I think you can now deploy.

⑤Could not find a JavaScript runtime ~

"It's working ...!"

This is an error when running Rspec

$ bundle exec rspec spec/ --format documentation

An error occurred while loading ./spec/models/cart_item_spec.rb.
Failure/Error: require File.expand_path('../config/environment', __dir__)

ExecJS::RuntimeUnavailable:
  Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes.

When I fly to github, I recommend downloading one of ExecJS. There was a way to insert a gem, but if you select Node.js and proceed to download, it was solved smoothly.

Now you can also do Rspec.

At the end

I introduced the procedure and error of migration from vagrant to docker. There are still many places where I haven't studied enough, so I thought again that there are many points where I haven't fully utilized docker. Since there were many articles that I referred to, I was able to introduce it in a shorter time than I initially thought, so I feel that it was good to try it.

Now that we can do the same thing as the previous vagrant environment, we would like to continue learning to improve the development environment with the introduction of CircleCI.

Since it was my first post, I think it was difficult to understand, but thank you for watching until the end!

Recommended Posts

I tried migrating the portfolio created on Vagrant to the Docker development environment
I tried to create React.js × TypeScript × Material-UI on docker environment
I tried to build the environment little by little using docker
I tried to build the environment of WSL2 + Docker + VSCode
I tried to create a padrino development environment with Docker
I tried to build the environment of PlantUML Server with Docker
I tried to create a Spring MVC development environment on Mac
[Rails 6.0, Docker] I tried to summarize the Docker environment construction and commands necessary to create a portfolio
[First environment construction] I tried to create a Rails 6 + MySQL 8.0 + Docker environment on Windows 10.
I tried to summarize the state transition of docker
Docker the development environment of Ruby on Rails project
[First team development ③] Share the development environment created with Docker
I tried to build an environment using Docker (beginner)
I tried the Docker tutorial!
How to check the WEB application created in the PC development environment on your smartphone
I tried to create a java8 development environment with Chocolatey
I tried to explain the method
Build Unity development environment on docker
Special Lecture on Multi-Scale Simulation: I tried to summarize the 5th
I tried to build an http2 development environment with Eclipse + Tomcat
Notes on building Kotlin development environment and migrating from Java to Kotlin
Special Lecture on Multi-Scale Simulation: I tried to summarize the 8th
I tried to display the calendar on the Eclipse console using Java.
Steps to build a Ruby on Rails development environment with Vagrant
I tried to build a laravel operating environment while remembering Docker
What I was addicted to when updating the PHP version of the development environment (Docker) from 7.2.11 to 7.4.x
I tried to take a look at the flow of Android development environment construction with Android Studio
Create Spring Boot development environment on Vagrant
I tried to summarize the methods used
I tried migrating Processing to VS Code
I tried running Docker on Windows Server 2019
I tried to implement the Iterator pattern
Microservices 101-I tried putting Docker on Ubuntu-
I tried to summarize the Stream API
I tried to build AdoptOpenjdk 11 on CentOS 7
What is Docker? I tried to summarize
Try the Docker environment on AWS ECS
Build an Ultra96v2 development environment on Docker 1
Lightweight PHP 7.4 development environment created with Docker
Problems I was addicted to when building the digdag environment with docker
I tried to summarize the key points of gRPC design and development
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)
I had to lower the PHP version in Docker + Composer + Laravel environment
I tried to publish the reflex measurement application on the Google Play store
I tried using Docker for the first time
I tried to create a portfolio with AWS, Docker, CircleCI, Laravel [with reference link]
I tried to verify AdoptOpenJDK 11 (11.0.2) with Docker image
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I tried to organize the session in Rails
Improve the performance of your Docker development environment
I want to create the strongest local development environment using VSCode Remote Containers
I tried running Ansible on a Docker container
Build a development environment to create Ruby on Jets + React apps with Docker
I tried to set tomcat to run the Servlet.
When I tried to reproduce Progate's Rails app on my PC (local environment), I got stuck at the image display
Bad Gateway came out when I tried to connect Grafana and InfluxDB on Docker
The story that did not disappear when I tried to delete mysql on ubuntu
How to quit Docker for Mac and build a Docker development environment with Ubuntu + Vagrant
Introduction to Slay the Spire Mod Development (2) Development Environment Construction
I tried to organize the cases used in programming