From 890d5d26e9e3d5507f8d809d7ea6d4a54ab67295 Mon Sep 17 00:00:00 2001 From: Eric Boehs Date: Thu, 10 Dec 2020 19:25:48 -0600 Subject: [PATCH 1/6] Dockerize app to move away from Heroku --- .env | 2 +- .gitignore | 1 + Dockerfile | 18 +++++++++ docker-compose.yml | 92 ++++++++++++++++++++++++++++++++++++++++++++++ init.sql | 1 + models/item.rb | 2 +- 6 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 init.sql diff --git a/.env b/.env index 1ea3c76..01ac8a6 100644 --- a/.env +++ b/.env @@ -1 +1 @@ -DATABASE_URL="postgres://localhost/hackinews_development?sslmode=disable" +DATABASE_URL="postgres://hackinews@database/hackinews?sslmode=disable" diff --git a/.gitignore b/.gitignore index 677c465..684c21b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .bundle +.env.local diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f369d94 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +# Use the Ruby 2.7.1 image from Docker Hub +# as the base image (https://hub.docker.com/_/ruby) +FROM ruby:2.7.2 + +# Use a directory called /code in which to store +# this application's files. (The directory name +# is arbitrary and could have been anything.) +WORKDIR /code + +# Copy all the application's files into the /code +# directory. +COPY . /code + +# Run bundle install to install the Ruby dependencies. +RUN bundle install + +# Commmand to run when this container starts. +CMD ["bundle", "exec", "puma", "-t", "5:5", "-p", "${PORT:-3000}", "-e", "${RACK_ENV:-development}"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..3374d4c --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,92 @@ +# Use the file format compatible with Docker Compose 3.8 +version: "3.8" + +# Each thing that Docker Compose runs is referred to as +# a "service". In our case, our Ruby application is one +# service ("web") and our PostgreSQL database instance +# is another service ("database"). +services: + database: + # Use the postgres 13.1 base image for this container. + image: postgres:13.1 + + volumes: + # We need to tell Docker where on the PostgreSQL + # container we want to keep the PostgreSQL data. + # In this case we're telling it to use a directory + # called /var/lib/postgresql/data, although it + # conceivably could have been something else. + # + # We're associating this directory with something + # called a volume. (You can see all your Docker + # volumes by running +docker volume ls+.) The name + # of our volume is db_data. + - db_data:/var/lib/postgresql/data + + # This copies our init.sql into our container, to + # a special file called + # /docker-entrypoint-initdb.d/init.sql. Anything + # at this location will get executed one per + # container, i.e. it will get executed the first + # time the container is created but not again. + # + # The init.sql file is a one-line that creates a + # user called (arbitrarily) hackinews. + - ./init.sql:/docker-entrypoint-initdb.d/init.sql + + environment: + POSTGRES_HOST_AUTH_METHOD: trust + + web: + # The root directory from which we're building. + build: . + + # This makes it so any code changes inside the project + # directory get synced with Docker. Without this line, + # we'd have to restart the container every time we + # changed a file. + volumes: + - .:/code:cached + + # The command to be run when we run "docker-compose up". + command: bash -c "bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development}" + + # Expose port 3000. + ports: + - "3000:3000" + + # Specify that this container depends on the other + # container which we've called "database". + depends_on: + - database + + # Specify the values of the environment variables + # used in this container. + environment: + RACK_ENV: development + DATABASE_NAME: hackinews + DATABASE_USER: hackinews + DATABASE_PASSWORD: + DATABASE_HOST: database + + worker: + build: . + + volumes: + - .:/code:cached + + command: bash -c "bundle exec ruby -r ./worker.rb -e \"loop { Worker.run }\"" + + depends_on: + - database + + environment: + RACK_ENV: development + DATABASE_NAME: hackinews + DATABASE_USER: hackinews + DATABASE_PASSWORD: + DATABASE_HOST: database + +# Declare the volumes that our application uses. +volumes: + db_data: diff --git a/init.sql b/init.sql new file mode 100644 index 0000000..3d2c469 --- /dev/null +++ b/init.sql @@ -0,0 +1 @@ +CREATE USER hackinews SUPERUSER; diff --git a/models/item.rb b/models/item.rb index 6abb471..ecef878 100644 --- a/models/item.rb +++ b/models/item.rb @@ -57,7 +57,7 @@ def prefetch_children def truncated_url return unless data['url'] - uri = URI.parse data['url'].delete('#%').encode(Encoding.find('ASCII'), { replace: '' }) + uri = URI.parse data['url'].delete('#%').encode(Encoding.find('ASCII'), **{ replace: '' }) uri.host.tap do |host| return host unless EXTENDED_TRUNCATION_DOMAINS.include? host From 9a139ad1a204bdc373af0264080ecd730731eddd Mon Sep 17 00:00:00 2001 From: Eric Boehs Date: Thu, 10 Dec 2020 19:28:36 -0600 Subject: [PATCH 2/6] Invert .env and .env.local loading --- lib/dotenv.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/dotenv.rb b/lib/dotenv.rb index 8bf80d6..e2402fa 100644 --- a/lib/dotenv.rb +++ b/lib/dotenv.rb @@ -46,5 +46,5 @@ def replace_newline_character(string) end if ENV['RACK_ENV'] == 'development' || ENV['RACK_ENV'] == 'test' - Dotenv.new.load %w[.env .env.local] + Dotenv.new.load %w[.env.local .env] end From c49559573dbd84fb987cc1ebb92fcd14bec0d505 Mon Sep 17 00:00:00 2001 From: Eric Boehs Date: Thu, 10 Dec 2020 19:29:55 -0600 Subject: [PATCH 3/6] Load .env in all envs --- lib/dotenv.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/dotenv.rb b/lib/dotenv.rb index e2402fa..e9fdb50 100644 --- a/lib/dotenv.rb +++ b/lib/dotenv.rb @@ -45,6 +45,4 @@ def replace_newline_character(string) end end -if ENV['RACK_ENV'] == 'development' || ENV['RACK_ENV'] == 'test' - Dotenv.new.load %w[.env.local .env] -end +Dotenv.new.load %w[.env.local .env] From 7ddaf0c71c051e0d4efb395a91d78ca622185b3b Mon Sep 17 00:00:00 2001 From: Eric Boehs Date: Thu, 10 Dec 2020 19:35:27 -0600 Subject: [PATCH 4/6] Move database_url to compose file --- .env | 2 +- docker-compose.yml | 10 ++-------- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/.env b/.env index 01ac8a6..1ea3c76 100644 --- a/.env +++ b/.env @@ -1 +1 @@ -DATABASE_URL="postgres://hackinews@database/hackinews?sslmode=disable" +DATABASE_URL="postgres://localhost/hackinews_development?sslmode=disable" diff --git a/docker-compose.yml b/docker-compose.yml index 3374d4c..0cd68db 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -64,10 +64,7 @@ services: # used in this container. environment: RACK_ENV: development - DATABASE_NAME: hackinews - DATABASE_USER: hackinews - DATABASE_PASSWORD: - DATABASE_HOST: database + DATABASE_URL: postgres://hackinews@database/hackinews worker: build: . @@ -82,10 +79,7 @@ services: environment: RACK_ENV: development - DATABASE_NAME: hackinews - DATABASE_USER: hackinews - DATABASE_PASSWORD: - DATABASE_HOST: database + DATABASE_URL: postgres://hackinews@database/hackinews # Declare the volumes that our application uses. volumes: From e21db45ee5fc9f3df5602218e2e6901ef80333bc Mon Sep 17 00:00:00 2001 From: Eric Boehs Date: Mon, 14 Dec 2020 10:14:41 -0600 Subject: [PATCH 5/6] Add docker containers to rev_proxy network --- docker-compose.yml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/docker-compose.yml b/docker-compose.yml index 0cd68db..96574d3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,6 +10,9 @@ services: # Use the postgres 13.1 base image for this container. image: postgres:13.1 + networks: + - rev_proxy + volumes: # We need to tell Docker where on the PostgreSQL # container we want to keep the PostgreSQL data. @@ -41,6 +44,9 @@ services: # The root directory from which we're building. build: . + networks: + - rev_proxy + # This makes it so any code changes inside the project # directory get synced with Docker. Without this line, # we'd have to restart the container every time we @@ -69,6 +75,9 @@ services: worker: build: . + networks: + - rev_proxy + volumes: - .:/code:cached @@ -84,3 +93,7 @@ services: # Declare the volumes that our application uses. volumes: db_data: + +networks: + rev_proxy: + name: home_rev_proxy_default From 710dc57794ba8c7cd83cf8e465d0a77cc371fe07 Mon Sep 17 00:00:00 2001 From: Eric Boehs Date: Mon, 6 Sep 2021 22:38:14 -0500 Subject: [PATCH 6/6] wip --- README.md | 4 ++++ docker-compose.yml | 12 ++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..fed9cac --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +To run migrations in docker: +``` +docker run --rm -it --network=home_rev_proxy_default -v "$(pwd)/db:/db" --env "DATABASE_URL=postgres://hackinews@database/hackinews?sslmode=disable" amacneil/dbmate up +``` diff --git a/docker-compose.yml b/docker-compose.yml index 96574d3..6fa582c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -37,6 +37,10 @@ services: # user called (arbitrarily) hackinews. - ./init.sql:/docker-entrypoint-initdb.d/init.sql + deploy: + restart_policy: + condition: on-failure + environment: POSTGRES_HOST_AUTH_METHOD: trust @@ -66,6 +70,10 @@ services: depends_on: - database + deploy: + restart_policy: + condition: on-failure + # Specify the values of the environment variables # used in this container. environment: @@ -86,6 +94,10 @@ services: depends_on: - database + deploy: + restart_policy: + condition: on-failure + environment: RACK_ENV: development DATABASE_URL: postgres://hackinews@database/hackinews