Configure the Rails development environment method under the Mac

Source: Internet
Author: User
Tags git clone docker for mac docker compose aliyun

The most troubling thing about rails development is environmental issues. With its own ideas and the help of a great defense project, the configuration of each environment takes a long time to solve, and there are many inconveniences in collaborating with others. So I've been trying to do a development environment that can be reused at any time.

1. Install Docker

The latest solution for Mac Docker is the Docker for Mac, which can be downloaded directly (currently in beta, but for the development environment).

2. Preparation of Dockerfile

To accomplish this, I made two Docker image, a base image, named Rails, which basically implements the basic configuration of the rails operating environment for later reuse and project-related image, which is configured primarily for specific projects.

Rails. Dockerfile (the key part is described in the note)

From ubuntu:16.10 # If the download is slow, this can be changed to a daocloud mirror image: Daocloud.io/library/ubuntu:trusty-xxxxxxx
Maintainer Tairy <tairyguo@gmail.com> # change to your own

# Run Update
# in order to speed up the update, modify the Ubuntu source for Aliyun (currently try the fastest, you can choose other domestic mirrors)
RUN sed-i ' s/archive.ubuntu.com/mirrors.aliyun.com/g '/etc/apt/sources.list \
&& apt-get update--fix-missing \
&& apt-get-y Upgrade

# Install Dependencies
RUN apt-get install-y git-core \
    Curl Zlib1g-dev build-essential \     libssl-dev libreadline-dev
Run apt-get update--fix-missing  
Run apt-get instal L-y libyaml-dev \
    libsqlite3-dev sqlite3 libxml2-dev \
    Libxslt1-dev libcur L4-openssl-dev \
    python-software-properties libffi-dev

# Install Rbenv
# Here clone may be a bit slow, you can first clone to local, the following clone operation to ADD rbenv/root/.rbenv operation can be.
RUN git clone git://github.com/sstephenson/rbenv.git/root/.rbenv \
&& echo ' Export path= ' $HOME/.rbenv/bin: $PATH "' >>/ROOT/.BASHRC \
&& Echo ' eval ' $ (rbenv init-) "' >>/ROOT/.BASHRC \
&& git clone git://github.com/sstephenson/ruby-build.git/root/.rbenv/plugins/ruby-build \
&& echo ' Export path= ' $HOME/.rbenv/plugins/ruby-build/bin: $PATH "' >>/ROOT/.BASHRC

# to speed up the use of Ruby Rbenv
RUN git clone https://github.com/andorchen/rbenv-china-mirror.git/root/.rbenv/plugins/rbenv-china-mirror

# Install Ruby
Run/root/.rbenv/bin/rbenv install-v 2.3.1 \
&&/root/.rbenv/bin/rbenv Global 2.3.1 \
&& echo "Gem:--no-document" >/ROOT/.GEMRC \
&&/root/.rbenv/shims/gem sources--add https://ruby.taobao.org/--remove https://rubygems.org/\
&&/root/.rbenv/shims/gem install bundler \
&&/root/.rbenv/shims/gem install rails \
&&/root/.rbenv/bin/rbenv Rehash
RUN apt-get install-y Software-properties-common python-software-properties
# Install Nodejs
RUN apt-get-y Install Nodejs

Run/root/.rbenv/shims/bundle Config--global frozen 1
Run/root/.rbenv/shims/bundle config--global silence_root_warning 1

# Run Project
RUN mkdir-p/working
Workdir/working
Onbuild COPY gemfile/working
Onbuild COPY gemfile.lock/working
Onbuild Run/root/.rbenv/shims/bundle Install--no-deployment
Onbuild COPY. /working

# Some Tools
RUN apt-get install-y Vim inetutils-ping
Build

Cd/path/to/dockerfile
Docker build Rails.
Above, this image will install the basic environment in which the rails application runs and set the command that Onbuild executes, and then your own rails can rely on the project to be created, for example:

Demo. Dockerfile

From Rails:latest # here to add dependencies
Maintainer Tairy <tairyguo@gmail.com>

# Todo:custom Env
Expose 3000
Place this dockerfile in the Rails project directory to build:

Cd/path/to/rails/app/path
Docker build Demo.
3. Use of Docker-compose

Using Docker-compose, you can better manage containers, and you can write docker-compose.yml files in the project directory (when you use them to delete the comments at the beginning of #):

# Compose version number, choose 2 can
Version: ' 2 '
Services
# Database Container
Db:
Image:mongodb
# Database Port Mappings
Ports
-"4,568:27,017"
Web:
# Build Path
Build:.
# The equivalent of CMD in Dockerfile
Command:/root/.rbenv/shims/bundle exec rails s-p 3000-b 0.0.0.0
Ports
-"3,000:3,000"
# shared Directory
Volumes:
- .:/ Working
# dependent containers
DEPENDS_ON:
-DB
In turn, the execution of the Docker-compose up command allows the container to be built, and when server startup is complete, it can be accessed via localhost:3000.

You can also add a parameter Docker-compose up-d to run it in the background.

4. RubyMine & Docker

Docker Plugin can be installed in RubyMine to build the container directly.

1. Installation of Docker plugin

Search for the installation in Preferences/plugins.


2. Configure Docker plugin

Open Build, Execution, Deployment/docker


Name:servername

API URL: [Docker API URL] ()

Certificates folder: [HTTPS] ()

Docker Compose executable: View with which docker-compose.

3. Configuration Construction method

Open the Run/debug Configurations window in the toolbar:

Server: Select server for second step configuration

Deployment: Choose Docker-compose.yml

At this point, you can build the project container directly in the IDE.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.