Docker 实践(三):Mac 下构建 Rails 开发环境

rails 开发,最让人头疼的就是环境问题。其自己的理念加上某伟大防护工程的帮助,使得每次环境的配置都的花费很长的时间来解决;同时,与人协做也有诸多不便。因此一直在尝试作一个能够随时复用的开发环境来。node

1. 安装 Docker

关于 Mac 下 docker 有了最新的解决方案,就是 Docker for Mac,直接下载安装就能够了(目前尚在 beta 版本,可是对于开发环境使用足矣)。python

2. 编写 Dockerfile

为了实现目的,我作了两个 docker image,一个 base image,命名 rails,主要实现 rails 运行环境的基础配置,为的是之后方便复用,另外一个是项目相关的 image,主要针对特定的项目作一些配置。git

rails.Dockerfile(关键部分在注释中有说明)github

FROM ubuntu:16.10 # 若是下载的很慢,这里能够改为 Daocloud 的镜像:daocloud.io/library/ubuntu:trusty-XXXXXXX
MAINTAINER Tairy <tairyguo@gmail.com> # 改为你本身的

# Run update
# 为了加快 update 的速度,修改 ubuntu 源为阿里云(目前尝试的最快的,也能够自行选择其余国内的镜像)
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 install -y libyaml-dev \
    libsqlite3-dev sqlite3 libxml2-dev \
    libxslt1-dev libcurl4-openssl-dev \
    python-software-properties libffi-dev

# Install rbenv
# 这里 clone 的时候可能会有点慢,能够先 clone 到本地,把下面的 clone 操做改为 ADD rbenv /root/.rbenv 操做便可。
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

# 为了加速 rbenv 使用 ruby china 的加速插件
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

buildweb

cd /path/to/Dockerfile
docker build rails .

以上,这个image 将会安装 rails 应用运行的基础环境,而且设置了 onbuild 执行的命令,以后本身的 rails 即可依赖该项目建立,例如:sql

demo.Dockerfilemongodb

FROM rails:latest # 这里添加依赖
MAINTAINER Tairy <tairyguo@gmail.com>

# TODO: custom env
EXPOSE 3000

将此 Dockerfile 置于 rails 的项目目录下,便可进行 build:docker

cd /path/to/rails/app/path
docker build demo .

3. 使用 docker-compose

使用 docker-compose 能够更好的管理容器,可在项目目录下编写 docker-compose.yml 文件(使用时删除#开头的注释内容):数据库

# compose 版本号,选择 2 便可
version: '2'
services:
  # 数据库容器
  db:
    image: mongodb
    # 数据库端口映射
    ports:
      - "4568:27017"
  web:
       # build 路径
    build: .
    # 至关于 Dockerfile 中的 CMD
    command: /root/.rbenv/shims/bundle exec rails s -p 3000 -b 0.0.0.0
    ports:
      - "3000:3000"
    # 共享目录
    volumes:
      - .:/working
    # 依赖容器
    depends_on:
      - db

进而,执行 docker-compose up 命令便可实现容器的构建,等 server 启动完成后,就能够经过 localhost:3000 来访问了。ubuntu

也能够加参数 docker-compose up -d 让其在后台运行。

4. RubyMine & Docker

能够在 RubyMine 中安装 Docker Plugin 来直接构建容器。

1. 安装 docker plugin

Preferences/Plugins 中搜索安装。

docker

2. 配置 docker plugin

打开 Build, Execution, Deployment/Docker

docker

  • Name: ServerName

  • API URL: [Docker API Url]()

  • Certificates folder: [HTTPS]()

  • Docker Compose executable: 使用 which docker-compose 查看。

3. 配置构建方式

在工具栏中打开 Run/Debug Configurations 窗口:

Run/Debug Configurations

Run/Debug Configurations

  • Server: 选择第二步配置的 server

  • Deployment: 选择 docker-compose.yml

至此,即可在 IDE 中直接构建项目容器。

相关文章
相关标签/搜索