Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Create basic Rails App with Docker
- ## Generating App Skeleton
- inside the container, install rails and generate application with some predefined configuration
- ```sh
- $ docker run --rm -v $PWD:/usr/src -w /usr/src ruby:2.3.3 bash
- $ gem install rails
- $ rails new "AppName" --database postgresql --skip-bundle
- ```
- ### Configuration files for the docker environment
- - now, exit the container and enter the project directory
- - create your Dockerfile, docker-compose.yml, .dockerignore and .env files
- ## Dockerfile
- ```Dockerfile
- FROM ruby:2.3.3
- # Set TERM env to avoid error message "TERM environment variable not set"
- ENV TERM=xterm
- RUN apt-get update -yqq \
- && apt-get install -yqq --no-install-recommends \
- postgresql-client \
- nodejs \
- && apt-get -q clean \
- && rm -rf /var/lib/apt/lists
- # Pre-install gems with native extensions
- RUN gem install nokogiri -v "1.6.8.1"
- WORKDIR /usr/src/app
- COPY Gemfile* ./
- RUN bundle install
- COPY . .
- CMD rails server -b 0.0.0.0
- ```
- ## docker-compose.yml
- ```yml
- version: "2"
- volumes:
- db-data:
- external: false
- services:
- db:
- image: postgres
- env_file: .env
- volumes:
- - db-data:/var/lib/postgresql/db-data
- app:
- build: .
- env_file: .env
- volumes:
- - .:/usr/src/app
- ports:
- - "3000:3000"
- depends_on:
- - db
- ```
- ## .dockerignore
- ```.dockerignore
- .git
- .gitignore
- README.md
- Dockerfile
- docker-compose.yml
- .dockerignore
- log/*
- tmp/*
- .rake_tasks*
- ```
- ## .env
- ```dotfile
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=pass1234
- ```
- ## Gemfile
- - add Nokogiri in Gemfile, that will speed up image build time
- ```Gemfile
- gem 'nokogiri', '~> 1.6', '>= 1.6.8.1'
- ```
- ## database.yml
- ```yml
- default: &default
- adapter: postgresql
- encoding: unicode
- host: db
- username: <%= ENV["POSTGRES_USER"] %>
- password: <%= ENV["POSTGRES_PASSWORD"] %>
- pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
- development:
- <<: *default
- database: "name_db"_development
- test:
- <<: *default
- database: "name_db"_test
- production:
- <<: *default
- host: <%= ENV["POSTGRES_HOST"] %>
- database: "name_db"_production
- ```
- ### build image and generate database
- ```sh
- $ docker-compose build
- $ docker-compose run --rm app rake db:create db:migrate
- ```
- ### run containers in the background
- ```sh
- $ docker-compose up -d
- ```
- ### run the bundle command on a container that is running
- ```sh
- $ docker-compose exec app <rails command>
- ```
- #### tips
- - after adding a gem in Gemfile, run the build again
- ```sh
- $ docker-compose build
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement