docker_practice/compose/rails.md

120 lines
3.8 KiB
Go
Raw Normal View History

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