2016/07/10 修正: docker-compose.yml
と nginx.conf
の一部を修正しました
とある経緯で Docker を使って Rails の環境をセットアップした手順です。
setting files
まずは用意した file から。
docker-compose.yml
# not use datastore container now
#datastore:
# build: Dockerfiles/datastore
mysql:
image: mysql:5.6.26
environment:
MYSQL_ROOT_PASSWORD: 'pass'
ports:
- '3306:3306'
# volumes_from:
# - datastore
nginx:
build: Dockerfiles/nginx
ports:
- '80:80'
# volumes_from:
# - datastore
links:
- web
web:
build: .
command: bundle exec unicorn -c config/unicorn.rb
# volumes_from:
# - datastore
volumes:
- .:/usr/src/app
ports:
- '3000:3000'
links:
- mysql
environment:
RAILS_ENV: development
MYSQL_ROOT_PASSWORD: 'pass'
DATABASE_URL: mysql2://root:pass@mysql:3306
SECRET_KEY_BASE: hogehoge
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Dockerfile
FROM rails:onbuild
1
Dockerfiles/nginx/Dockerfile
FROM nginx:1.7.9
COPY nginx.conf /etc/nginx/nginx.conf
1
2
3
2
3
Dockerfiles/nginx/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024; # increase if you have lots of clients
accept_mutex off; # "on" if nginx worker_processes > 1
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on; # off may be better for *some* Comet/long-poll stuff
tcp_nodelay off; # on may be better for some Comet/long-poll stuff
gzip on;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 500;
gzip_disable "MSIE [1-6]\.";
gzip_types text/plain text/html text/xml text/css
text/comma-separated-values
text/javascript application/x-javascript
application/atom+xml;
upstream app_server {
# for UNIX domain socket setups:
# server unix:/path/to/.unicorn.sock fail_timeout=0;
# for TCP setups, point these to your backend servers
# server 192.168.0.7:8080 fail_timeout=0;
server web:3000 fail_timeout=0;
}
server {
listen 80;
server_name localhost;
client_max_body_size 4G;
keepalive_timeout 5;
# path for static files
root /usr/your_app/home/system/public;
try_files $uri/index.html $uri.html $uri @app;
location @app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
# Rails error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
root /usr/your_app/home/system/public;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
Setup Tutorial
Download Tools
VirtualBox
https://www.virtualbox.org/wiki/Downloads
please install version >= 5.x
Homebrew
http://brew.sh/index_ja.html
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
1
Docker Tools
Docker
$ brew install docker
1
Docker Machine
$ brew install docker-machine
1
Docker Compose
$ brew install docker-compose
1
Download Application
$ cd ~/path/to/workspace
$ git clone git@github.com:your/App.git your_app
$ cd your_app
1
2
3
2
3
Setup Docker Machine
$ docker-machine create --driver virtualbox dev
...
$ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
dev - virtualbox Running tcp://192.168.99.100:2376 v1.10.0
1
2
3
4
5
6
2
3
4
5
6
$ eval "$(docker-machine env dev)"
$ echo 'eval "$(docker-machine env dev)"' >> ~/.bashrc
1
2
3
2
3
$ docker-machine start dev
...
$ docker-machine ip dev
192.168.99.100
1
2
3
4
5
2
3
4
5
Setup Application
Build Docker Images and Start Containers
# ~/path/to/workspace/your_app
$ docker-compose build
...
$ docker-compose up
...
1
2
3
4
5
6
2
3
4
5
6
please open another tab
Setup DB
$ docker-compose run web rake db:create
$ docker-compose run web rake db:migrate
1
2
2
Access Application
$ docker-machine ip dev
192.168.99.100
1
2
2
access to 192.168.99.100:3000