Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. # Create basic Rails App with Docker
  2.  
  3. ## Generating App Skeleton
  4.  
  5. inside the container, install rails and generate application with some predefined configuration
  6.  
  7. ```sh
  8. $ docker run --rm -v $PWD:/usr/src -w /usr/src ruby:2.3.3 bash
  9. $ gem install rails
  10. $ rails new "AppName" --database postgresql --skip-bundle
  11. ```
  12.  
  13. ### Configuration files for the docker environment
  14. - now, exit the container and enter the project directory
  15. - create your Dockerfile, docker-compose.yml, .dockerignore and .env files
  16.  
  17. ## Dockerfile
  18. ```Dockerfile
  19. FROM ruby:2.3.3
  20.  
  21. # Set TERM env to avoid error message "TERM environment variable not set"
  22. ENV TERM=xterm
  23.  
  24. RUN apt-get update -yqq \
  25. && apt-get install -yqq --no-install-recommends \
  26. postgresql-client \
  27. nodejs \
  28. && apt-get -q clean \
  29. && rm -rf /var/lib/apt/lists
  30.  
  31. # Pre-install gems with native extensions
  32. RUN gem install nokogiri -v "1.6.8.1"
  33.  
  34. WORKDIR /usr/src/app
  35. COPY Gemfile* ./
  36. RUN bundle install
  37. COPY . .
  38.  
  39. CMD rails server -b 0.0.0.0
  40. ```
  41.  
  42. ## docker-compose.yml
  43. ```yml
  44. version: "2"
  45.  
  46. volumes:
  47. db-data:
  48. external: false
  49.  
  50. services:
  51. db:
  52. image: postgres
  53. env_file: .env
  54. volumes:
  55. - db-data:/var/lib/postgresql/db-data
  56.  
  57. app:
  58. build: .
  59. env_file: .env
  60. volumes:
  61. - .:/usr/src/app
  62. ports:
  63. - "3000:3000"
  64. depends_on:
  65. - db
  66. ```
  67.  
  68. ## .dockerignore
  69. ```.dockerignore
  70. .git
  71. .gitignore
  72. README.md
  73.  
  74. Dockerfile
  75. docker-compose.yml
  76. .dockerignore
  77.  
  78. log/*
  79. tmp/*
  80. .rake_tasks*
  81. ```
  82.  
  83. ## .env
  84. ```dotfile
  85. POSTGRES_USER=postgres
  86. POSTGRES_PASSWORD=pass1234
  87. ```
  88.  
  89. ## Gemfile
  90. - add Nokogiri in Gemfile, that will speed up image build time
  91.  
  92. ```Gemfile
  93. gem 'nokogiri', '~> 1.6', '>= 1.6.8.1'
  94. ```
  95.  
  96. ## database.yml
  97. ```yml
  98. default: &default
  99. adapter: postgresql
  100. encoding: unicode
  101. host: db
  102. username: <%= ENV["POSTGRES_USER"] %>
  103. password: <%= ENV["POSTGRES_PASSWORD"] %>
  104. pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  105.  
  106. development:
  107. <<: *default
  108. database: "name_db"_development
  109.  
  110. test:
  111. <<: *default
  112. database: "name_db"_test
  113.  
  114. production:
  115. <<: *default
  116. host: <%= ENV["POSTGRES_HOST"] %>
  117. database: "name_db"_production
  118. ```
  119.  
  120. ### build image and generate database
  121. ```sh
  122. $ docker-compose build
  123. $ docker-compose run --rm app rake db:create db:migrate
  124. ```
  125.  
  126. ### run containers in the background
  127. ```sh
  128. $ docker-compose up -d
  129. ```
  130. ### run the bundle command on a container that is running
  131. ```sh
  132. $ docker-compose exec app <rails command>
  133. ```
  134.  
  135. #### tips
  136. - after adding a gem in Gemfile, run the build again
  137.  
  138. ```sh
  139. $ docker-compose build
  140. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement