Skip to content

Instantly share code, notes, and snippets.

@hamadu
Last active February 23, 2017 16:11
Show Gist options
  • Save hamadu/9784cd4024bb09bf9154522271b64eb1 to your computer and use it in GitHub Desktop.
Save hamadu/9784cd4024bb09bf9154522271b64eb1 to your computer and use it in GitHub Desktop.
Learn Docker

1. prepare files

  • Dockerfile
FROM ruby:2.3.3

# install necessary packages
RUN apt-get update -qq && apt-get install -y build-essential nodejs npm sqlite3 --no-install-recommends

# remove caches
RUN rm -rf /var/lib/apt/lists/*

ENV INSTALL_PATH /app
ENV LANG C.UTF-8
RUN mkdir $INSTALL_PATH
WORKDIR $INSTALL_PATH

COPY Gemfile ./
COPY Gemfile.lock ./
RUN bundle install
COPY . ./
  • Gemfile
source 'https://rubygems.org'

gem 'rails', '~> 5.0.1'
  • Gemfile.lock
$ touch Gemfile.lock # <- create empty file

2. run 'docker build' to create image

$ docker build -t app .

3. run 'docker run' to run command in container

$ docker run --rm -v `pwd`:/app app bundle install
$ docker build -t app . # Copy current Gemfile.lock
$ docker run --rm -v `pwd`:/app app rails new . --force --skip-test
$ docker build -t app . # Copy current Gemfile.lock again

4. run 'rails s'

$ docker run --rm -v `pwd`:/app -p 3000:3000 app rails s -b '0.0.0.0'

5. scaffold

$ docker run --rm -v `pwd`:/app app rails g scaffold post title:string body:text published:boolean
$ docker run --rm -v `pwd`:/app app rake db:migrate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment