We want to install gems in ./vendor/bundle because the gems will persist in ./vendor/bundle regardless of the lifecyle of the container. When we update the Gemfile and do bundle install again, it will only install the newly added gems, not everything again.
# Base imageFROMruby:2.5.1# Install pluginRUNapt-getupdate-qq&&apt-getinstall-ybuild-essentialvim# Install mysqlRUNapt-getinstall-ydefault-libmysqlclient-dev# Install nodejsRUNcurl-sLhttps://deb.nodesource.com/setup_11.x|bash-&&\apt-getinstall-ynodejs# Clears out the local repository of retrieved package filesRUNapt-get-qclean# Set an environment variable where the Rails app is installed to inside of Docker imageENVAPP_PATH/usr/src/appRUNmkdir-p$APP_PATH# Set working directoryWORKDIR$APP_PATH# Setting env upENVRAILS_ENVproductionENVRACK_ENVproduction# Setting localENVLC_ALLC.UTF-8# Setting timezoneENVTZAsia/TaipeiRUNcp/usr/share/zoneinfo/$TZ/etc/localtime&&echo$TZ>/etc/timezone# COPY Gemfile & Gemfil.lockCOPYGemfile*$APP_PATH/# Run bundleRUNbundleinstall--jobs20--retry5--withoutdevelopmenttest--pathvendor/bundle# Adding project filesCOPY.$APP_PATH/# Build Frond-EndRUNRAILS_ENV=$RAILS_ENVbundleexecrakeassets:precompileEXPOSE3000CMD["bundle","exec","puma","-C","config/puma.rb"]
# Base imageFROMnginx:1.15.8# Install dependenciesRUNapt-getupdate-qq&&apt-get-yinstallapache2-utilsvim# Establish where Nginx should look for filesENVRAILS_ROOT/usr/src/app# Setting localENVLC_ALLC.UTF-8# Setting timezoneENVTZAsia/TaipeiRUNcp/usr/share/zoneinfo/$TZ/etc/localtime&&echo$TZ>/etc/timezone# Set our working directory inside the imageWORKDIR$RAILS_ROOT# create log directoryRUNmkdirlog# copy over static assetsCOPYpublicpublic/# Copy Nginx config templateCOPYdocker/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 placeRUNenvsubst'$RAILS_ROOT'</tmp/docker.nginx>/etc/nginx/conf.d/default.confEXPOSE80# Use the "exec" form of CMD so Nginx shuts down gracefully on SIGTERM (i.e. `docker stop`)CMD["nginx","-g","daemon off;"]
# define our application serverupstreamrails_app{# The app service 3000 port that points to the docker-compose definitionserverapp:3000;}server{listen80;# define your domain or IPserver_namelocalhost;# define the public application rootroot$RAILS_ROOT/public;indexindex.html;# define where Nginx should write its logsaccess_log$RAILS_ROOT/log/nginx.access.log;error_log$RAILS_ROOT/log/nginx.error.log;# deny requests for files that should never be accessed# ~ regular 區分大小寫, .env / .gitlocation~/\. { deny all; } # ~* regular 不分大小寫, .rb /.loglocation~*^.+\.(rb|log)${denyall;}# serve static (compiled) assets directly if they exist (for rails production)location~^/(assets|images|javascripts|stylesheets|swfs|system)/{# $uri: localhost/404.html,則 $uri 為 `/404.html`# @rails: 後面定義的 location @rails# 如果 url 匹配進來,則先按 $uri 處理,若沒有找到,則交給 @rails 處理try_files$uri@rails;# close access logaccess_logoff;# to serve pre-gzipped version# 設定為 `on` ,在處理壓縮之前,先查找已經預壓縮的文件(.gz)# 避免每次對同一個文件進行重複的壓縮處理gzip_staticon;expiresmax;# public 對每個用戶有效; private 對當前用戶有效add_headerCache-Controlpublic;add_headerLast-Modified"";add_headerETag"";break;}# send non-static file requests to the app serverlocation/{try_files$uri@rails;}location@rails{internal;# 只能被內部的請求呼叫,外部的呼叫請求會返回 'Not found'proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;proxy_set_headerHost$http_host;proxy_redirectoff;proxy_passhttp://rails_app;# 導向到 upstream rails_app}}
database.yml
host name 必須對應到 docker-compose 所定義的 service name,並且透過環境變數所設定的 user 來登入