If you want to easily build a Ruby / Rails development environment, Docker is the best choice. Here, Ubuntu is used as the host OS. (See here for Mac hosts)
In the Docker container, the base OS is Ubuntu 20.04, and the libraries and Ruby itself required to run Rails applications will be installed. The Rails application itself can be referenced from a local directory on the host OS. This is for the Docker container to handle the execution environment of the Rails application, and to realize the editing of the Rails application and the source code management by the editor on the host OS.
It has the following merits.
Please install the Docker service on the host OS in advance.
First, place the following files in any directory.
Dockerfile.dev
#Ruby with Docker/Build Rails development environment
#
#Install any version of Ruby on an Ubuntu-based container
#Also node.install js
#Rails applications are generated in the host OS's local directory, not in the container
From ubuntu:20.04
ENV DEBIAN_FRONTEND=noninteractive
#Create the same user as local
ARG uid=unknown
ARG user=unknown
RUN useradd -m -u ${uid} ${user}
#Update the software installed on Ubuntu
RUN apt-get update -y
RUN apt-get upgrade -y
#Apt the packages needed to build Ruby-Install via get
RUN apt-get install -y build-essential
RUN apt-get install -y libssl-dev libreadline-dev zlib1g-dev
RUN apt-get install -y git wget
#Library required when using sqlite3
RUN apt-get install libsqlite3-dev
#Library required when using MySQL or MariaDB
RUN apt-get install -y libmysqlclient-dev
#Other useful tools
RUN apt-get install -y nano
# ruby-Install any Ruby version using build
RUN git clone --depth=1 https://github.com/rbenv/ruby-build
RUN PREFIX=/usr/local ./ruby-build/install.sh
RUN rm -rf ruby-build
RUN ruby-build 2.7.2 /usr/local
# node.js,install npm
RUN apt-get install -y nodejs npm
#n package installed
RUN npm install n -g
#Install node using n package
RUN n stable
#The old node you put in first.js,Remove npm
RUN apt-get purge -y nodejs npm
#Install yarn package
RUN npm install yarn -g
The following is how to create a container (Change your_app_name to your application name)
$ export YOURAPP=your_app_name
$ docker build -t $YOURAPP --build-arg uid=$(id -u $USER) --build-arg user=$USER -f Dockerfile.dev .
Start the container
$ export REPO=`pwd`
$ docker run -d --name $YOURAPP -v $REPO/:/$YOURAPP/ -p 4000:3000 -v /etc/group:/etc/group:ro -v /etc/passwd:/etc/passwd:ro -u $(id -u $USER):$(id -g $USER) -ti $YOURAPP
Start bash on the container and build a Rails application.
$ docker exec -ti $YOURAPP bash
Go to your application directory (Change your_app_name to your application name)
$ cd /your_app_name
Run bundle init to create a Gemfile file
$ bundle init
Edit Gemfile (use nano editor below)
$ nano Gemfile
Uncomment # gem "rails" in Gemfile (Change # gem "rails" to gem "rails")
Install Rails related Gem on vendor / bundle with bundle install
$ bundle config set path 'vendor/bundle'
$ bundle install
rails new creates a new Rails application in this directory Overwrite / example / Gemfile? (Enter "h" for help) Answer Y when asked [Ynaqdhm]
$ bundle exec rails new .
Launch a Rails application Set the IP address to be bound to 0.0.0.0 so that it can be accessed from the host OS.
$ bundle exec rails s -b 0.0.0.0
Now that the work on the container is complete, access http: // localhost: 4000 again with the browser of the host OS. If you see Yay! You ’re on Rails! On your browser, you ’re successful.
Recommended Posts