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!
--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
—— 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.
--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
--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
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 ...
#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!
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
"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.
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