docker_practice/compose/rails.md

118 lines
3.7 KiB
Go
Raw Permalink Normal View History

# 使 Rails
2015-01-13 18:04:53 +08:00
2019-03-08 12:21:07 +08:00
> `Ruby`
2015-01-13 18:04:53 +08:00
2017-11-26 09:54:04 +08:00
使 `Compose` `Rails/PostgreSQL`
2015-01-14 13:46:21 +08:00
2017-11-01 00:20:30 +08:00
Docker `Dockerfile` Docker
2015-01-13 18:04:53 +08:00
```docker
2015-01-13 18:04:53 +08:00
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)
2015-01-14 21:52:19 +08:00
Rails `Gemfile` `rails new`
2015-01-13 18:04:53 +08:00
2017-11-22 11:13:23 +08:00
```bash
2015-01-13 18:04:53 +08:00
source 'https://rubygems.org'
gem 'rails', '4.0.2'
```
2016-11-25 22:24:48 +08:00
`docker-compose.yml` `docker-compose.yml` 西 web 使 PostgreSQL web
2015-01-14 13:46:21 +08:00
2017-11-01 00:20:30 +08:00
```yaml
version: "3"
services:
db:
image: postgres
ports:
- "5432"
2017-11-26 09:54:04 +08:00
2017-11-01 00:20:30 +08:00
web:
build: .
command: bundle exec rackup -p 3000
volumes:
- .:/myapp
ports:
- "3000:3000"
```
2017-11-01 00:20:30 +08:00
使 `docker-compose run`
2017-11-22 11:13:23 +08:00
```bash
2016-11-25 22:24:48 +08:00
$ docker-compose run web rails new . --force --database=postgresql --skip-bundle
2015-01-14 13:46:21 +08:00
```
2017-11-26 09:54:04 +08:00
`Compose` 使 `Dockerfile` web 使 `rails new `
2015-01-14 13:46:21 +08:00
2017-11-22 11:13:23 +08:00
```bash
2015-01-14 13:46:21 +08:00
$ ls
2016-11-25 22:24:48 +08:00
Dockerfile app docker-compose.yml tmp
2015-01-14 13:46:21 +08:00
Gemfile bin lib vendor
2016-11-25 22:24:48 +08:00
Gemfile.lock condocker-compose log
README.rdoc condocker-compose.ru public
2015-01-14 13:46:21 +08:00
Rakefile db test
```
2015-01-14 21:52:19 +08:00
`Gemfile` `therubyracer` 便使 Javascript
2015-01-14 13:46:21 +08:00
2017-11-22 11:13:23 +08:00
```bash
2015-01-14 13:46:21 +08:00
gem 'therubyracer', platforms: :ruby
```
2018-03-30 20:55:02 +08:00
`Gemfile` Dockerfile
2015-01-14 13:46:21 +08:00
2017-11-22 11:13:23 +08:00
```bash
2016-11-25 22:24:48 +08:00
$ docker-compose build
2015-01-14 13:46:21 +08:00
```
2015-01-14 21:52:19 +08:00
Rails `localhost` `db` postgres
2015-01-14 13:46:21 +08:00
`database.yml`
2017-11-22 11:13:23 +08:00
```bash
2015-01-14 13:46:21 +08:00
development: &default
adapter: postgresql
encoding: unicode
database: postgres
pool: 5
username: postgres
password:
host: db
test:
<<: *default
database: myapp_test
```
2015-01-14 13:46:21 +08:00
2017-11-22 11:13:23 +08:00
```bash
2016-11-25 22:24:48 +08:00
$ docker-compose up
2015-01-14 13:46:21 +08:00
```
2015-01-14 13:46:21 +08:00
PostgreSQL
2017-11-22 11:13:23 +08:00
```bash
2015-01-14 13:46:21 +08:00
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
```
2015-01-14 13:46:21 +08:00
2017-11-22 11:13:23 +08:00
```bash
2016-11-25 22:24:48 +08:00
$ docker-compose run web rake db:create
2015-01-14 13:46:21 +08:00
```
2017-11-01 00:20:30 +08:00
web docker 3000