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

If you want to easily build a Ruby / Rails development environment, Docker is the best choice. Here, Mac OS is used as the host OS. (See here for Ubuntu 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

#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 -f Dockerfile.dev .

Start the container

$ export REPO=`pwd`
$ docker run -d --name $YOURAPP -v $REPO/:/$YOURAPP/ -p 4000:3000 -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 (Mac version)
Template: Build a Ruby / Rails development environment with a Docker container (Ubuntu 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 Wordpress development environment with Docker
Ruby on Rails development environment construction with Docker + VSCode (Remote Container)
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
Build a Java development environment on Mac
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]
Build a development environment for Django + MySQL + nginx with Docker Compose
How to build a Ruby on Rails environment using Docker (for Docker beginners)
Creating a java web application development environment with docker for mac part1
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
[Ruby on Rails] Let's build an environment on mac
Ruby on Rails development environment construction on M1 Mac
Build a development environment for Docker, java, vscode
[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
A memorandum when building an environment with Ruby3.0 x Rails6.1 x Docker x CentOS Stream
Build Java development environment with VS Code on Mac
Docker the development environment of Ruby on Rails project
Easily build a Vue.js environment with Docker + Vue CLI
Build Rails (API) x MySQL x Nuxt.js environment with Docker
[Docker] Build a Wordpress environment locally (Win, Mac) quickly
[Environment construction] Build a Java development environment with VS Code!
Try to build a Java development environment using Docker
Rails6 development environment construction [Mac]
[Docker] Rails 5.2 environment construction with docker
Build docker environment with WSL
Ruby ① Build a Windows environment
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
Build a "Spring Thorough Introduction" development environment with IntelliJ IDEA
Rails application development environment construction with Docker [Docker, Rails, Puma, Nginx, MySQL]
Build a CentOS 8 virtual environment on your Mac with VirtualBox
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!