Dockerize New Rails5 Application

FROM ruby:2.5
MAINTAINER ayesha306
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp
# 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.
CMD ["rails", "server", "-b", "0.0.0.0"]
touch Gemfile
touch Gemfile.lock
source 'https://rubygems.org'
gem 'rails', '~>5'
#!/bin/bashset -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 “$@”
version: '3'
services:
db:
image: postgres
volumes:
- ./tmp/db:/var/lib/postgresql/data
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:
- "3001:3000"
depends_on:
- db

Build the project

docker-compose run web rails new . --force --no-deps --database=postgresql
$ ls -l
docker-compose build

Connect the database

default: &default
adapter: postgresql
encoding: unicode
host: db
username: postgres
password:
pool: 5
development:
<<: *default
database: myapp_development
test:
<<: *default
database: myapp_test
docker-compose up
docker-compose run web rake db:create

View the Rails welcome page!

docker ps #this command will show the running container
docker exec -it <container id> bash
rails console
 ctrl+q
docker-compose down

Restart the application

Rebuild the application

ports: - "3001:3000"

finally

docker login
docker push ayesha306/myapp:latest

Github

--

--

--

Software Engineer — Openshift CoreOS

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Linus / Unix Stuff

LatticeX Foundation Reached Strategic Partnership with xNFT Protocol

Deploy a simple flask application in Google Cloud Run

Appvn apk for android mobiles and drugs: Download appvn Apk is the first-class app keep to download…

@Charles eugene hill | 11 - 17 - 1983

Handling Screen Orientation Changes in Capacitor Apps

10 New Languages for Perspective API

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
Ayesha Kaleem

Ayesha Kaleem

Software Engineer — Openshift CoreOS

More from Medium

Getting Started with ScyllaDB Cloud Using Node.js Part 1

Connect MongoDB Docker Container With Robo 3T and Mongo Express

A Container illustration from Docker

Intelligent Systems with Internet of Things (IoT) -5 (Database)

A Node.js configuration provider reading files, environment and arguments