Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 2588

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T06:49:08+00:00 2024-11-26T06:49:08+00:00

Dockerizing a Rails app

  • 61k

Docker is a container solution developed by the Docker Inc and backed by the open source community. The idea of the docker solution is to provide a container based tool with an image-based deployment model. You can create a complete development/production ready environment with a simple set of configuration and wrap it into an image that can be distributed on your team or to the community.

In this article we are going to build the configuration of a development environment to a simple rails application using a PostgreSQL database.

Getting the hands dirty

First you need some application to work on created and Docker/Compose installed.

The folder structure that we will follow is based in creating a docker folder inside the root of the project containig the Dockerfile and the entrypoint.sh script. The docker-compose.yml file will be placed at the root of the project.

The Dockerfile

The Dockerfile configures the image with all dependencies of the project.

ARG RUBY_VERSION=2.7.2 FROM ruby:$RUBY_VERSION ARG DEBIAN_FRONTEND=noninteractive  RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list RUN curl -sL https://deb.nodesource.com/setup_12.x | bash -  RUN apt-get update && apt-get install -y        build-essential        nodejs        yarn        locales        netcat        sudo    && apt-get clean  ENV LANG C.UTF-8  RUN mkdir -p /app && chown $USER:$USER /app WORKDIR /app  RUN gem install bundler 
Enter fullscreen mode Exit fullscreen mode

The FROM statement defines the base image of our container which is the official ruby image of the 2.7.2 version of the language.
We are also installing the dependecies to run the application and preparing the system to receive the code.

The docker-compose.yml file

The compose file configures the services of the application using a YAML syntax.

version: '3.7'  services:   postgres:     image: postgres:11     environment:       - POSTGRES_HOST_AUTH_METHOD=trust     volumes:       - postgres:/var/lib/postgresql/data   web:     build:       context: ./docker/     environment:       - DATABASE_USERNAME=postgres       - DATABASE_PASSWORD=       - DATABASE_HOST=postgres       - DATABASE_PORT=5432     depends_on:       - postgres     entrypoint: docker/entrypoint.sh     ports:       - "3000:3000"     volumes:       - .:/app:cached     tty: true     stdin_open: true  volumes:     gems:     postgres:     rails_cache: 
Enter fullscreen mode Exit fullscreen mode

With this config file we are defining two services:

  • The database service, using the version 11 of PostgreSQL and with a volume mounted storing the database data.
  • The application service thats going to use our previouslly created Dockerfile to build its container. Here we can define some parameters of the application run:
    • The environment variables with the environment key.
    • The ports binding from the host machine with the container.
    • The entrypoint script, which will bootstrap the application during the services startup.

The entrypoint.sh script

Here we just install all rails/node dependencies, configure the database and start the application server process.

  #! /bin/bash   set -e    bundle install   yarn install    bundle exec rails db:prepare   bundle exec rails server -p 3000 -b 0.0.0.0 
Enter fullscreen mode Exit fullscreen mode

Adding new dependencies

At this point the application is configured to run the rails server connected to a PostgreSQL database.
If you need more dependencies, such Redis, ElasticSearch or MongoDB you can simply add a new service that uses an image that wraps the depedency.

The Docker Hub platform is a public respository used by many companies and individual developers to share Docker images. You can use it to search the service you need to attach to your application.

Wrapping up and running the application

To run the application you can use the following commands:

  • docker-compose up will raise all services described on the compose file.
  • docker-compose exec SERVICE_NAME COMMAND will run the given command on the command line of the indicated service.
  • docker-compose down is going to stop all services.

Now you have a fully functional and ready to use configuration to your project. I used a Ruby on Rails application as example, but the concepts can be applied to any stack you use, you just need to adjust the Dockerfile to your project dependencies and configure the services on the compose file.

dockerrailswebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.