Skip to content

Commit

Permalink
Dockerize the application
Browse files Browse the repository at this point in the history
Base Dockerfile, entrypoint.sh, and docker-compose.yml on https://docs.docker.com/samples/rails/
Ignore all temporary folders, .git, and non-necessary files on .dockerignore based on https://codefresh.io/docker-tutorial/not-ignore-dockerignore-2/

Create .env file with POSTGRES_USER and POSTGRES_PASSWORD used for postgres on docker-compose.yml
I need to spend some time seeing what solution I can use for environment variables in docker locally and production
Since at the moment I have both .env and environment_variables.rb

Add host:db to database.yml in order for the postgres port to be available on docker
  • Loading branch information
Exellin committed Apr 24, 2021
1 parent dbecec5 commit 305f045
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 5 deletions.
14 changes: 14 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.git
/tmp
/log

.byebug_history
.gitignore
.dockerignore
.rubocop.yml
.ruby-version
README.rdoc

/config/secrets.yml
/config/environment_variables.rb
.env
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Used for docker-compose.yml.
# POSTGRES_USER and POSTGRES_PASSWORD need to match ones in database.yml

POSTGRES_USER=
POSTGRES_PASSWORD=
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@
#ignore config secrets and environment variables
/config/secrets.yml
/config/environment_variables.rb
.env
18 changes: 18 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Based on https://docs.docker.com/samples/rails/

# syntax=docker/dockerfile:1
FROM ruby:3.0.1
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install

# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

# Configure the main process to run when running the image
CMD ["rails", "server", "-b", "0.0.0.0"]
9 changes: 4 additions & 5 deletions config/database.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
defaults: &default
default: &default
adapter: postgresql
encoding: unicode
host: db
username: <%= ENV["POSTGRES_USERNAME"] %>
password: <%= ENV["POSTGRES_PASSWORD"] %>
database: <%= ENV["POSTGRES_DB"] %>
pool: 5

test:
adapter: postgresql
encoding: unicode
username: <%= ENV["POSTGRES_USERNAME"] %>
password: <%= ENV["POSTGRES_PASSWORD"] %>
<<: *default
database: <%= ENV["POSTGRES_TEST_DB"] %>

development:
Expand Down
19 changes: 19 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Based on https://docs.docker.com/samples/rails/

version: "3.9"
services:
db:
image: postgres
volumes:
- ./tmp/db:/var/lib/postgresql/data
env_file:
- .env
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- db
10 changes: 10 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
set -e

# Remove a potentially pre-existing server.pid for Rails.
rm -f /myapp/tmp/pids/server.pid

# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"

# Based on https://docs.docker.com/samples/rails/

0 comments on commit 305f045

Please sign in to comment.