├── Dockerfile ├── Gemfile ├── Gemfile.lock ├── README.md └── docker-compose.yml /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ruby:2.4.2 2 | 3 | ENV APP_ROOT /app 4 | ENV BUNDLE_PATH /usr/local/bundle 5 | 6 | RUN apt-get update -qq && \ 7 | apt-get install -y build-essential libpq-dev nodejs 8 | 9 | WORKDIR $APP_ROOT 10 | ADD . $APP_ROOT 11 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gem 'rails', '~> 5' 4 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucatironi/rails-docker/4f8ed8c72854c360424c4edb700dd9dfdf628e87/Gemfile.lock -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Setup, Build and Run a Rails 5 web application with Docker and Docker Compose 2 | 3 | ## Setup the project 4 | 5 | In order to kickstart the application we need to install the rails gem and run the 6 | `rails new ` command. We will do this inside the same Docker container 7 | that will be used to run the application itself. To do so we need to create a 8 | `Dockerfile` to create the Docker image with the necessary dependencies, a 9 | `docker-compose.yml` file to provision the other services needed (a postgres 10 | database and a volume store) and a `Gemfile` (with an empty `Gemfile.lock`) to 11 | install the `rails` gem and bundle install its dependencies. 12 | 13 | Create a new directory and some empty files: 14 | 15 | ``` 16 | $ mkdir rails-docker 17 | $ cd rails-docker 18 | $ touch Dockerfile docker-compose.yml Gemfile Gemfile.lock 19 | ``` 20 | 21 | Copy and paste the following content in the respective files: 22 | 23 | File: `Dockerfile` 24 | 25 | ``` 26 | FROM ruby:2.4.2 27 | 28 | ENV APP_ROOT /app 29 | ENV BUNDLE_PATH /usr/local/bundle 30 | 31 | RUN apt-get update -qq && \ 32 | apt-get install -y build-essential libpq-dev nodejs 33 | 34 | WORKDIR $APP_ROOT 35 | ADD . $APP_ROOT 36 | ``` 37 | 38 | File: `docker-compose.yml` 39 | 40 | ``` 41 | version: '3' 42 | 43 | volumes: 44 | store: 45 | driver: local 46 | bundle: 47 | driver: local 48 | 49 | services: 50 | web: 51 | build: . 52 | ports: 53 | - 3000:3000 54 | volumes: 55 | - .:/app 56 | - bundle:/usr/local/bundle 57 | links: 58 | - db 59 | # Keep the stdin open, so we can attach to our app container's process and do things such as 60 | # byebug, etc: 61 | stdin_open: true 62 | # Enable sending signals (CTRL+C, CTRL+P + CTRL+Q) into the container: 63 | tty: true 64 | command: ./bin/start.sh 65 | environment: &app_env 66 | PORT: 3000 67 | DB_HOST: db 68 | DB_PORT: 5432 69 | DB_USER: postgres 70 | DB_PSWD: postgres 71 | DB_NAME: development_database 72 | db: 73 | image: postgres:latest 74 | ports: 75 | - 5432:5432 76 | volumes: 77 | - store:/var/lib/postgresql/data 78 | environment: 79 | POSTGRES_USER: postgres 80 | POSTGRES_PASSWORD: postgres 81 | POSTGRES_DB: development_database 82 | ``` 83 | 84 | File: `Gemfile` 85 | 86 | ``` 87 | source 'https://rubygems.org' 88 | 89 | gem 'rails', '~> 5' 90 | ``` 91 | 92 | ## Build the project 93 | 94 | First we need to bundle the rails 5 dependencies: 95 | 96 | ``` 97 | $ docker-compose run --rm web bundle --jobs=10 --retry=5 98 | ``` 99 | 100 | And then use the `rails new` command to create the new application: 101 | 102 | ``` 103 | $ docker-compose run --rm web bundle exec rails new . --force --database=postgresql --skip-bundle 104 | ``` 105 | 106 | ## Configure the database 107 | 108 | We need to change slightly the database configuration to use the environment 109 | variables set in the docker-compose file: 110 | 111 | File: `config/database.yml` 112 | 113 | ``` 114 | default: &default 115 | adapter: postgresql 116 | encoding: unicode 117 | username: <%= ENV['DB_USER'] %> 118 | password: <%= ENV['DB_PSWD'] %> 119 | host: <%= ENV['DB_HOST'] %> 120 | # For details on connection pooling, see rails configuration guide 121 | # http://guides.rubyonrails.org/configuring.html#database-pooling 122 | pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> 123 | 124 | development: 125 | <<: *default 126 | database: <%= ENV['DB_NAME'] %> 127 | 128 | test: 129 | <<: *default 130 | database: test_database 131 | ``` 132 | 133 | ## Setup the app 134 | 135 | ``` 136 | $ docker-compose run --rm web bin/setup 137 | ``` 138 | 139 | Create a `start.sh` file in the `bin` dir: 140 | 141 | File: `bin/start.sh` 142 | 143 | ``` 144 | #!/bin/bash 145 | 146 | bundle check || bundle install 147 | 148 | if [ -f tmp/pids/server.pid ]; then 149 | rm -f tmp/pids/server.pid 150 | fi 151 | 152 | bundle exec rails s -p $PORT -b 0.0.0.0 153 | ``` 154 | 155 | It automatically removes the `server.pid` that will create problems when you stop 156 | and restart the app. 157 | Make the file executable with the `chmod` command: 158 | 159 | ``` 160 | $ chmod +x bin/start.sh 161 | ``` 162 | 163 | Finally start your newly created Rails application and visit [localhost:3000](http://localhost:3000): 164 | 165 | ``` 166 | $ docker-compose up 167 | ``` 168 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | 3 | volumes: 4 | store: 5 | driver: local 6 | bundle: 7 | driver: local 8 | 9 | services: 10 | web: 11 | build: . 12 | ports: 13 | - 3000:3000 14 | volumes: 15 | - .:/app 16 | - bundle:/usr/local/bundle 17 | links: 18 | - db 19 | # Keep the stdin open, so we can attach to our app container's process and do things such as 20 | # byebug, etc: 21 | stdin_open: true 22 | # Enable sending signals (CTRL+C, CTRL+P + CTRL+Q) into the container: 23 | tty: true 24 | command: ./bin/start.sh 25 | environment: &app_env 26 | PORT: 3000 27 | DB_HOST: db 28 | DB_PORT: 5432 29 | DB_USER: postgres 30 | DB_PSWD: postgres 31 | DB_NAME: development_database 32 | db: 33 | image: postgres:latest 34 | ports: 35 | - 5432:5432 36 | volumes: 37 | - store:/var/lib/postgresql/data 38 | environment: 39 | POSTGRES_USER: postgres 40 | POSTGRES_PASSWORD: postgres 41 | POSTGRES_DB: development_database 42 | --------------------------------------------------------------------------------