Template: Build a Ruby / Rails development environment with a Docker container (Ubuntu version)

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.

  1. No need to install Ruby or Rails directly on the host OS
  2. It is also possible to run different versions of Ruby and Rails in separate containers as needed
  3. Rails source code can be edited with your favorite editor on the host OS
  4. Git management can be limited to the source code on the host OS
  5. (The following Dockerfile.dev should also be managed by Git)

Building and launching a container

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

Generate a Rails application inside a container

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

Template: Build a Ruby / Rails development environment with a Docker container (Ubuntu version)
Template: Build a Ruby / Rails development environment with a Docker container (Mac version)
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 PureScript development environment with Docker
Build a Wordpress development environment with Docker
Steps to build a Ruby on Rails development environment with Vagrant
Build a development environment for Docker + Rails6 + Postgresql
Build a WordPress development environment quickly with Docker
Build debug environment on container --Build local development environment for Rails tutorial with Docker-
Build a local development environment for Rails tutorials with Docker (Rails 6 + PostgreSQL + Webpack)
Build a Ruby on Rails development environment on AWS Cloud9
Build a development environment to create Ruby on Jets + React apps with Docker
Build a Node.js environment with Docker
Build environment with vue.js + rails + docker
Build Rails environment with Docker Compose
I made a development environment with rails6 + docker + postgreSQL + Materialize.
How to build Rails, Postgres, ElasticSearch development environment with Docker
[Rails] [Docker] Copy and paste is OK! How to build a Rails development environment with Docker
How to quit Docker for Mac and build a Docker development environment with Ubuntu + Vagrant
[Docker] Development environment construction Rails6 / Ruby2.7 / MySQL8
[Ruby] Building a Ruby development environment on Ubuntu
How to build Rails 6 environment with Docker
Build a simple Docker + Django development environment
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
How to build Rails + Vue + MySQL environment with Docker [2020/09 latest version]
Create a development environment for Ruby 3.0.0 and Rails 6.1.0 on Ubuntu 20.04.1 LTS
How to build a Ruby on Rails environment using Docker (for Docker beginners)
Build a Laravel / Docker environment with VSCode devcontainer
I tried to build a Firebase application development environment with Docker in 2020
[Copy and paste] Build a Laravel development environment with Docker Compose Part 2
Build a simple Docker Compose + Django development environment
[Copy and paste] Build a Laravel development environment with Docker Compose Participation
[Win10] Build a JSF development environment with NetBeans
Build a development environment for Docker, java, vscode
Build mecab (NEologd dictionary) environment with Docker (ubuntu)
[Rails] How to build an environment with Docker
[First team development ②] Build an environment with Docker
Create a Spring Boot development environment with docker
Build a Java development environment with VS Code
Easily build a Vue.js environment with Docker + Vue CLI
[Note] Build a Python3 environment with Docker in EC2
Build Java development environment with WSL2 Docker VS Code
Build Rails (API) x MySQL x Nuxt.js environment with Docker
[Environment construction] Build a Java development environment with VS Code!
Try to build a Java development environment using Docker
[Docker] Rails 5.2 environment construction with docker
Build docker environment with WSL
Ruby ① Build a Windows environment
Install Ubuntu 20.04 in virtual box on windows10 and build a development environment using docker
Until you build a Nuxt.js development environment with Docker and touch it with VS Code
Build a local development environment for Open Distro for Elasticsearch with multiple nodes using Docker
Docker command to create Rails project with a single blow in environment without Ruby
Build a "Spring Thorough Introduction" development environment with IntelliJ IDEA
Rails application development environment construction with Docker [Docker, Rails, Puma, Nginx, MySQL]
Build a Node-RED environment with Docker to move and understand
I tried to create a padrino development environment with Docker
Rails + MySQL environment construction with Docker
Create a Vue3 environment with Docker!
Build a Tomcat 8.5 environment with Pleiades 4.8