Rails & PostgreSQL with CapRover on DigitalOcean Droplet (Part III)

FROM ruby:2.6.5-alpine AS gemENV RAILS_ENV productionWORKDIR /myappRUN apk add --update --no-cache nodejs yarn postgresql-client postgresql-dev tzdata build-base# install gems
COPY Gemfile .
COPY Gemfile.lock .
RUN bundle install --deployment --without development test
# install npm packages
COPY package.json .
COPY yarn.lock .
RUN yarn install --frozen-lockfile
# compile assets
COPY Rakefile .
COPY bin bin
COPY .browserslistrc .
COPY postcss.config.js .
COPY babel.config.js .
COPY config config
COPY app/assets app/assets
COPY app/javascript app/javascript
# Assets, to fix missing secret key issue during building
RUN SECRET_KEY_BASE=dumb bundle exec rails assets:precompile
FROM ruby:2.6.5-alpineENV RAILS_ENV production
ENV RAILS_LOG_TO_STDOUT 1
ENV RAILS_SERVE_STATIC_FILES 1
WORKDIR /myappRUN apk add --update --no-cache postgresql-client postgresql-dev tzdataCOPY . /myapp
COPY --from=gem /usr/local/bundle /usr/local/bundle
COPY --from=gem /myapp/vendor/bundle /myapp/vendor/bundle
COPY --from=gem /myapp/public/assets /myapp/public/assets
COPY --from=gem /myapp/public/packs /myapp/public/packs
# 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 80
# Start the main process.
WORKDIR /myapp
CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0"]
.bundle
.dockerignore
.git
.gitignore
Dockerfile
log
node_modules
public/assets
public/packs
README.md
storage
test
tmp
RUN SECRET_KEY_BASE=dumb bundle exec rails assets:precompile \
&& find vendor/bundle -name "*.c" -delete \
&& find vendor/bundle -name "*.o" -delete
docker_build:
<<: *default
database: docker_build
Rails.application.configure do
config.eager_load = true
end
# Ensures that a master key has been made available in either ENV["RAILS_MASTER_KEY"]
# or in config/master.key. This key is used to decrypt credentials (and other encrypted files).
config.require_master_key = true
FROM ruby:2.6.5-alpine AS gemENV RAILS_ENV productionWORKDIR /myappRUN apk add --update --no-cache nodejs yarn postgresql-client postgresql-dev tzdata build-base# install gems
COPY Gemfile .
COPY Gemfile.lock .
RUN bundle install --deployment --without development test
# install npm packages
COPY package.json .
COPY yarn.lock .
RUN yarn install --frozen-lockfile
# compile assets
ENV RAILS_ENV docker_build
COPY . /myapp
# Assets, to fix missing secret key issue during building
RUN SECRET_KEY_BASE=dumb bundle exec rails assets:precompile \
&& find vendor/bundle -name "*.c" -delete \
&& find vendor/bundle -name "*.o" -delete
FROM ruby:2.6.5-alpineENV RAILS_ENV production
ENV RAILS_LOG_TO_STDOUT 1
ENV RAILS_SERVE_STATIC_FILES 1
WORKDIR /myappRUN apk add --update --no-cache postgresql-client postgresql-dev tzdataCOPY . /myapp
COPY --from=gem /usr/local/bundle /usr/local/bundle
COPY --from=gem /myapp/vendor/bundle /myapp/vendor/bundle
COPY --from=gem /myapp/public/assets /myapp/public/assets
COPY --from=gem /myapp/public/packs /myapp/public/packs
# For some reasion, dockerignore does not work properly
RUN rm -rf test \
&& rm -rf vendor/bundle/ruby/2.6.0/cache \
&& rm -rf README.md
# 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
# Start the main process.
WORKDIR /myapp
CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0"]
namespace :db do
desc "Checks to see if the database exists"
task :exists do
begin
Rake::Task['environment'].invoke
ActiveRecord::Base.connection
rescue
exit 1
else
exit 0
end
end
end
#!/bin/sh
set -e
# Remove a potentially pre-existing server.pid for Rails.
rm -f /myapp/tmp/pids/server.pid
bundle exec rake db:exists || bundle exec rake db:create# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"
scp -o "IdentitiesOnly=yes" -i .ssh/another_rsa latest.dump root@your_host_ip:/root
docker cp latest.dump container_id:/tmp/latest.dump
$ docker exec -it container_id /bin/sh# once inside the postgresql container
$ pg_restore --verbose --clean --no-acl --no-owner -U username -d mydb /tmp/latest.dump

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store