Skip to content

Instantly share code, notes, and snippets.

@leo424y
Forked from satendra02/app.DockerFile
Created May 2, 2018 12:34
Show Gist options
  • Save leo424y/79f2664fbcbb372cf21bf0c4849f74ab to your computer and use it in GitHub Desktop.
Save leo424y/79f2664fbcbb372cf21bf0c4849f74ab to your computer and use it in GitHub Desktop.
docker+rails+puma+nginx+postgres (Production ready)
Create docker folder in app root
-app_name
-app
-db
-config
-database.yml
...
-docker
-app
-DockerFile_app
-web
-DockerFile_web
-nginx.conf
-docker-compose.yml
default: &default
adapter: postgresql
encoding: unicode
username: postgres
password:
pool: 5
host: db
production:
<<: *default
database: app_name_production
version: '3'
volumes:
postgres_data: {}
services:
app:
build:
context: .
dockerfile: ./docker/app/Dockerfile_app
depends_on:
- db
db:
image: postgres
volumes:
- postgres_data:/var/lib/postgresql/data
web:
build:
context: .
dockerfile: ./docker/web/Dockerfile_web
depends_on:
- app
ports:
- 80:80
FROM ruby:2.3.1
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
# Configuring main directory
RUN mkdir -p /app_name
WORKDIR /app_name
# Setting env up
ENV RAILS_ENV='production'
ENV RAKE_ENV='production'
# Adding gems
COPY Gemfile /app_name/Gemfile
COPY Gemfile.lock /app_name/Gemfile.lock
RUN bundle install --jobs 20 --retry 5 --without development test
# Adding project files
COPY . /app_name
RUN bundle exec rake assets:precompile
EXPOSE 3000
CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"]
# Base image:
FROM nginx
# Install dependencies
RUN apt-get update -qq && apt-get -y install apache2-utils
# establish where Nginx should look for files
ENV RAILS_ROOT /var/www/app_name
# Set our working directory inside the image
WORKDIR $RAILS_ROOT
# create log directory
RUN mkdir log
# copy over static assets
COPY public public/
# Copy Nginx config template
COPY docker/web/nginx.conf /tmp/docker.nginx
# substitute variable references in the Nginx config template for real values from the environment
# put the final config in its place
RUN envsubst '$RAILS_ROOT' < /tmp/docker.nginx > /etc/nginx/conf.d/default.conf
EXPOSE 80
# Use the "exec" form of CMD so Nginx shuts down gracefully on SIGTERM (i.e. `docker stop`)
CMD [ "nginx", "-g", "daemon off;" ]
upstream rails_app {
server app:3000;
}
server {
# define your domain
server_name www.example.com;
# define the public application root
root /app_name/public;
index index.html;
# define where Nginx should write its logs
access_log /var/www/app_name/log/nginx.access.log;
error_log /var/www/app_name/log/nginx.error.log;
# deny requests for files that should never be accessed
location ~ /\. {
deny all;
}
location ~* ^.+\.(rb|log)$ {
deny all;
}
# serve static (compiled) assets directly if they exist (for rails production)
location ~ ^/(assets|images|javascripts|stylesheets|swfs|system)/ {
try_files $uri @rails;
access_log off;
gzip_static on; # to serve pre-gzipped version
expires max;
add_header Cache-Control public;
# Some browsers still send conditional-GET requests if there's a
# Last-Modified header or an ETag header even if they haven't
# reached the expiry date sent in the Expires header.
add_header Last-Modified "";
add_header ETag "";
break;
}
# send non-static file requests to the app server
location / {
try_files $uri @rails;
}
location @rails {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://rails_app;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment