Docker Rails Bundle Install

2/13/2022by admin

Feb 12, 2019 Rails & Docker: configure bundler to run bundle install in a docker build for Rails.4.2.6? Ask Question Asked 2 years, 7 months ago. Active 2 years, 7 months ago. Running the elasticsearch-rails tests via bundle exec rake test:all will now pass. For a longer running development project that also requires a Rails server, you could add a more robust Elasticsearch service to your docker-compose.yml, similar to the one described by the elasticsearch docker docs.

Docker Rails Bundle Installer

This is a basic development environment setup for Ruby on Rails application with MySQL using Docker and Docker Compose.

You can find the instructions on how to install Docker from here. If you are using Docker for Mac/Windows, then Docker Compose is already installed as a part of the desktop installs. If not, you can get the installation instructions here.

Getting Started: Creating a new Rails App

The method we’re going to use for generating a new Rails app is going to be quite different. In this example, I’m not going to require you from installing Ruby on your local machine. We will be running Ruby on a docker container with the command below:

-itThis is actually 2 flags, -i and -t being combined. With these flags, we can run the image in interactive mode.
-v 'PWD:/app'This will share the current folder ($PWD) with the folder inside the container which is /app.
-w /appThis will set the default working directory to /app
--rmThis flag means to remove/destroy the container when we exit the container.
ruby:2.6This will tell docker to use the Ruby 2.6 official image.
bashThis will let us start a bash session.

The command above will automatically pull the official image from the Docker Hub registry if the image does not exist in your local machine.

After running the command above, you will now be inside a container with Ruby 2.6 installed.

Next, we’re going to install the rails gem inside the Ruby container.

Once the installation is complete, we can now generate new rails application inside the container.

We’ll be using mysql as our database so we added the flag -d mysql. And we also told it to skip the process for running bundle install. We’ll do that later.

InstallDocker rails bundle install path

Since we already have our rails application, we can now exit the container by running:

If you do a quick ls on your current directory, you can see the blog rails app that we just generated seconds ago.

Rails Docker Bundle Install Cache

Docker compose rails bundle install

Writing the Dockerfile

Now we can move into our blog app directory and create the Dockerfile.

Open the Dockerfile with the text editor of your choice and paste the following configuration.

Docker Compose configuration

Still inside the blog directory, we’ll now create the docker-compose.yml file.

And add the configuration below:

We have specified 2 services: db and app. These services will be running on their own separate containers. The db container will be running mysql, and the app container will be running the Rails app.

Let’s first look at the db service. We’ll create the .env file later, that’s going to be used on both services. We also specified volumes for the mysql data. This is to persist the mysql data, so that the data won’t disappear when we stop or destroy the containers.

As for the app service, note that we created a Dockerfile a while ago. With the build: . option, it will instruct it later to build the image using the Dockerfile we’ve created.

We also share the application source code using volumes, and set the ports. depends_on will instruct it to wait for the db service container to start first, before starting the app container.

Now let’s create the .env file which will contain the environment variables.

Don’t forget to include .env to your .gitignore file, as you may not want to commit this file. I would recommend creating a sample file .env.example to give a hint for other developers on what is needed to be inside the .env file, and commit that instead of the actual .env file.

Next, we’ll have to edit our development database configuration in config/database.yml. We’ll set the host to use the environment variable.

Build the image

Now we have the Dockerfile and the docker-compose.yml file. We can build the image using the command below:

Initial Setup

These commands are only required when running the app for the first time.

Install and Setup Webpacker

Create the database

Start up the containers

Now you can start up the containers simply by running the command below:

Note: When you update something in Dockerfile, it is recommended to add the build flag (--build) to rebuild the image. Eg., docker-compose up -d --build

Open up your browser and visit http://localhost:3000

Now if you want run some other rails commands, you just have to run docker-compose run --rm --no-deps app [command]:

To stop all the containers:

Docker Rails Bundle Installation


Comments are closed.