Skip to content

Commit a151c06

Browse files
author
Keith Thompson
authored
Merge pull request #12 from coderjourney/improve-deployment
Set up Nginx for production
2 parents 24c83f8 + 4fd2572 commit a151c06

File tree

5 files changed

+75
-2
lines changed

5 files changed

+75
-2
lines changed

Dockerfile.prod

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
FROM ruby:2.3.1
2+
3+
RUN apt-get update -yqq \
4+
&& apt-get install -yqq --no-install-recommends \
5+
postgresql-client \
6+
nodejs \
7+
&& apt-get -q clean \
8+
&& rm -rf /var/lib/apt/lists
9+
10+
# Pre-install gems with native extensions
11+
RUN gem install nokogiri -v "1.6.8.1"
12+
13+
WORKDIR /usr/src/app
14+
COPY Gemfile* ./
15+
RUN bundle install
16+
COPY . .
17+
18+
# Pre-compile assets
19+
ENV RAILS_ENV production
20+
RUN rails assets:precompile
21+
22+
CMD script/start

docker-compose.prod.yml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,40 @@
11
version: "2"
22

33
volumes:
4+
assets:
5+
external: false
6+
configs:
7+
external: false
48
db-data:
59
external: false
610

711
services:
12+
webserver:
13+
image: "nginx:1.11.8"
14+
env_file: .env.prod
15+
ports:
16+
- "80:80"
17+
- "443:443"
18+
volumes:
19+
- assets:/usr/share/nginx/html
20+
- configs:/etc/nginx/conf.d
21+
822
prod_db:
923
image: postgres
1024
env_file: .env.prod
1125
volumes:
1226
- db-data:/var/lib/postgresql/db-data
1327

1428
prod_app:
15-
build: .
29+
build:
30+
context: .
31+
dockerfile: Dockerfile.prod
1632
env_file: .env.prod
1733
ports:
1834
- "3000:3000"
35+
volumes:
36+
- assets:/usr/share/nginx/html
37+
- configs:/etc/nginx/conf.d
1938
depends_on:
2039
- prod_db
40+
- webserver

script/prod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash -e
2+
3+
docker-compose -f docker-compose.prod.yml "$@"

script/start

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
#!/bin/sh
1+
#!/bin/bash -e
22

33
if [[ -a /tmp/puma.pid ]]; then
44
rm /tmp/puma.pid
55
fi
66

7+
if [[ $RAILS_ENV == "production" ]]; then
8+
rake assets:precompile
9+
mkdir -p /usr/share/nginx/html
10+
cp -R public/* /usr/share/nginx/html
11+
mkdir -p /etc/nginx/conf.d/
12+
cp site.conf /etc/nginx/conf.d/default.conf
13+
fi
14+
715
rails server -b 0.0.0.0 -P /tmp/puma.pid

site.conf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
server {
2+
listen 80;
3+
4+
# Properly server assets
5+
location ~ ^/(assets)/ {
6+
root /usr/share/nginx/html;
7+
gzip_static on;
8+
expires max;
9+
add_header Cache-Control public;
10+
add_header ETag "";
11+
}
12+
13+
# Proxy request to rails app
14+
location / {
15+
proxy_set_header Host $host;
16+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
17+
proxy_pass_header Set-Cookie;
18+
proxy_pass http://prod_app:3000;
19+
}
20+
}

0 commit comments

Comments
 (0)