Rails Application with Docker

Creating Rails App

We are creating a rails application without installing rails on our system

  • Create a folder on machine where we want to create Rails app

    • Linux 

      • mkdir ~/docker_rails

      • cd  ~/docker_rails

    • Windows 

      • mkdir docker_rails

      • cd docker_rails

  • Create docker container based on image ruby:2.6 

    • docker run -i -t --rm -v ${PWD}:/usr/src/app ruby:2.6 bash 

      • Where docker run creates a container

      • -i -t OR  -it : 

      •  --rm : It's a throwaway container 

      • -v Mounts volume to share local system with container 

      • ${PWD} : 

        • Unix environment variable pointed to current directory

        • Windows user should give current directory path as below

        • docker run -i -t --rm -v C:/Users/amrut/OneDrive/Desktop/Amruta/docker_rails:/usr/src/app ruby:2.6 bash

      •  ${PWD}:/usr/src/app : Mount the current directory inside the container at /usr/src/app

      • Ruby:2.6 Reference docker image

      • bash 

      • After running this command , we can see 

        • root@05d3878962ce:/#

        • This shows bash shell running inside container

  • Move into the container folder

    • cd /usr/src/app

  • Install Rails

    • As our Ruby image is of version 2.6 , I selected Rails version 6

    • Latest version of Rails require latest ruby version

    • gem install rails -v 6

  • Create new Rails application

    • rails new myapp --skip-test --skip-bundle

    • exit

  • List  Rails application files

    • Cd myapp

    •  Linux

      • ls

    • Windows

      • dir OR tree /

  • Go to root path of rails application and create this Dockerfile 


Building a Dockerfile image

It's simply creating a docker image with the help of the Dockerfile we have just written in our Rails application.

Be in the directory of Rails app files and Dockerfile. Run below command

docker build .

Here dot (.) represents the current directory.


 Running a Rails server with docker image

docker run -p 3000:3000 <docker_image_id> bin/rails s -b 0.0.0.0

docker run -p 3000:3000 459338e4742f bin/rails s -b 0.0.0.0



Comments

Popular posts from this blog

PERSONAL ACCESS TOKEN ON GITHUB

Docker Tutorial

Software Installation on Ubuntu