docker_practice/compose/rails.md

118 lines
3.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# 使 Rails
> `Ruby`
使 `Compose` `Rails/PostgreSQL`
Docker `Dockerfile` Docker
```docker
FROM ruby
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev
RUN mkdir /myapp
WORKDIR /myapp
ADD Gemfile /myapp/Gemfile
RUN bundle install
ADD . /myapp
```
使 RubyBundler Dockerfile [Dockerfile 使](../image/dockerfile/README.md)
Rails `Gemfile` `rails new`
```bash
source 'https://rubygems.org'
gem 'rails', '4.0.2'
```
`docker-compose.yml` `docker-compose.yml` 西 web 使 PostgreSQL web
```yaml
version: "3"
services:
db:
image: postgres
ports:
- "5432"
web:
build: .
command: bundle exec rackup -p 3000
volumes:
- .:/myapp
ports:
- "3000:3000"
```
使 `docker-compose run`
```bash
$ docker-compose run web rails new . --force --database=postgresql --skip-bundle
```
`Compose` 使 `Dockerfile` web 使 `rails new `
```bash
$ ls
Dockerfile app docker-compose.yml tmp
Gemfile bin lib vendor
Gemfile.lock condocker-compose log
README.rdoc condocker-compose.ru public
Rakefile db test
```
`Gemfile` `therubyracer` 便使 Javascript
```bash
gem 'therubyracer', platforms: :ruby
```
`Gemfile` Dockerfile
```bash
$ docker-compose build
```
Rails `localhost` `db` postgres
`database.yml`
```bash
development: &default
adapter: postgresql
encoding: unicode
database: postgres
pool: 5
username: postgres
password:
host: db
test:
<<: *default
database: myapp_test
```
```bash
$ docker-compose up
```
PostgreSQL
```bash
myapp_web_1 | [2014-01-17 17:16:29] INFO WEBrick 1.3.1
myapp_web_1 | [2014-01-17 17:16:29] INFO ruby 2.0.0 (2013-11-22) [x86_64-linux-gnu]
myapp_web_1 | [2014-01-17 17:16:29] INFO WEBrick::HTTPServer#start: pid=1 port=3000
```
```bash
$ docker-compose run web rake db:create
```
web docker 3000