Dockerization of Rails 6 app

2 Min. Read
Jan 15, 2019

 Docker Containerization Rails Docker Compose Config

Things you need to Learn

Please visit my blog post to learn about Docker and its installation from Containarization and Docker.

Using docker-compose to dockerize the app

Steps:-

  1. Chdir to the project directory

  2. Create an entry point file called docker-entrypoint.sh

    1
    2
    3
    4
    5
    6
    7
    8
    
    #!/bin/bash
    
    set -e
    
    bundle check || bundle install --binstubs="$BUNDLE_BIN"
    
    echo "command executing $@"
    exec "$@"
    
  3. Create a Dockerfile with the content like below:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    
    FROM ruby:2.6.3
    # Needed to compile nodejs
    RUN apt-get update -qq && apt-get install -y build-essential
    RUN apt-get install -y libpq-dev
    RUN apt-get install -y libxml2-dev libxslt1-dev
    RUN apt-get install -y nodejs
    
    RUN mkdir /app
    WORKDIR /app
    ADD . /app
    
    EXPOSE 3000
    
    COPY ./docker-entrypoint.sh /
    RUN chmod +x /docker-entrypoint.sh
    ENTRYPOINT ["/docker-entrypoint.sh"]
    
    ENV BUNDLE_PATH=/bundle \
        BUNDLE_BIN=/bundle/bin \
        GEM_HOME=/bundle
    ENV PATH="${BUNDLE_BIN}:${PATH}"
    RUN gem install bundler
    
    
  4. Create docker-compose-yml file

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    
    version: '3'
    
    services:
      postgres:
        image: postgres:9.6
        ports:
          - '5432:5432'
        volumes:
          - postgres:/var/lib/postgresql/data
    
      web:
        build: .
        command: puma # we are using puma
        volumes:
          - bundle:/bundle
          - .:/app
        ports:
          - '3000:3000'
        links:
          - postgres
    
    volumes:
      bundle:
      postgres:
    
  5. database.yml file need to change

    1
    2
    3
    4
    5
    6
    7
    8
    
    development:
      <<: *default
      host: postgres # service name in docker-compose.yml
      username: postgres # has to be this way
      password:
      database: my_app_development
    
    

    postgres is the name of the service which will be your host.

  6. Everytime you change Dockerfile, you need to rebuild the image using docker-compose build

  7. You need to create the database the first time

    1
    
    docker-compose run web rails db:create && rails db:migrate && rails db:seed
    

    or you can run

    1
    2
    3
    
    docker-compose run web rails db:create
    docker-compose run web rails db:migrate
    docker-compose run web rails db:seed
    

Then, your application will be available via localhost:3000 in browsers.

Summary

In this way you can move your existing Rails 5/6 projects to Docker using Docker Compose.

References

https://bundler.io/v2.0/guides/bundlerdockerguide.html

You might also like

Learn to move your existing Rails 5/6 projects to Docker using Docker Compose.
Read More..

Summary of the post.
Read More..

In Computer Science, Containerization is a concept of running a process in a small container isolated from other processes running in the system with its own operating environment. Docker facilitates developers to build systems in modular fashion that can run in a separate container so that the behaviour does'nt vary by machines and environments.
Read More..


Write To Me

Hire me shiva bhusal
We'll never share your email with anyone else.
I'll never share your email with anyone else.