Docker Compose is docker’s native tool for defining and running multi-container applications defined in a single file with Docker. Moreover is a great tool for development environments, staging servers, and CI. Here are some easy steps on how we can run ruby on rail web applications on Docker containers using Docker Compose.
1. Installing Docker Engine
First of all, we’ll need to install docker engine in our host operating system. As we are running Ubuntu 18.04 LTS in our host machine, we’ll first update our local repository index of an apt package manager.
$ sudo apt-get update
After updating the local repository index, we’ll need to run the following command to install docker engine.
$ sudo wget -qO- https://get.docker.com/ | sh
Now, we’ll need to add our user to the group docker so that we can access docker without sudo or root permission. To do so, we’ll need to run the following.
$ sudo usermod -aG docker arun
Note: Here, *arun is the username of the account we are currently logged in and wanna add to the group docker. So, we extremely recommend replacing arun with your own username.
2. Installing Docker Compose
After we have successfully installed docker engine, we’ll now need to install docker compose that we’ll be featuring in this tutorial. As docker compose is not available in the official repository of Ubuntu, so we’ll gonna install it from the official GitHub page.
$ wget https://github.com/docker/compose/releases/download/1.4.2/docker-compose-Linux-x86_64 $ mv docker-compose-Linux-x86_64 /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose
3. Configuring Dockerfile
Next, after the docker engine is installed in our machine, we’ll gonna create the most common file for docker ie Dockerfile. We can create the file using a text editor inside our working directory. To do so, we’ll need to run the following command.
$ nano Dockerfile
Now, we’ll gonna configure our Dockerfile configurations.
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev
RUN mkdir /myapp
ADD Gemfile /myapp/Gemfile
RUN bundle install
ADD . /myapp
4. Defining Compose Configuration
Finally, we’ll now create our main compose file where the real thing happens. Here, we’ll create a docker-compose.yml file in which we’ll define the configurations for our web app and database required by it to store data. In this file, we define the configurations that are necessary to link both the web app and the database and expose their ports. To do so, we’ll create the file using our favorite text editor.
$ nano docker-compose.yml
Then, we’ll gonna copy and paste the following lines of configuration into the file.
command: bundle exec rails s -p 80 -b ‘0.0.0.0’
5. Generating Rails Skeleton App
Before we for generating the rails skeleton, we’ll need to create a file named Gemfile. We can create it using a text editor.
$ nano Gemfile
Then, we’ll need to append the file with the following lines of configurations.
Official Ubuntu Book
The free Ubuntu operating system has won the hearts and minds of millions of users worldwide. It combines a strong technical platform, impressive quality, and an enthusiastic global community of users who relentlessly support, document, and test it.
gem ‘rails’, ‘4.2.2’
Now, we’ll gonna build an image for web service using Dockerfile and then we’ll generate a new rails skeleton app inside the container built from the newly created image. To do so, we’ll simply need to run the following command.
$ docker-compose run web rails new . --force --database=postgresql --skip-bundle
Now, we’ll uncomment the line in our newly generated Gemfile which loads therubyracer. To do so, we’ll gonna open Gemfile using our favorite text editor.
$ nano Gemfile
Then, we’ll gonna find the following line and uncomment it as shown below.
gem ‘therubyracer’, platforms: :ruby
Now that we’ve got a new Gemfile, we need to build the image again.
$ docker-compose build
6. Configuring Database
Now we’ll edit our newly generated database.yml using a simple text editor.
$ nano database.yml
Next, we’ll need to make changes to the file database.yml to look as the lines of a configuration shown below.
7. Running Rails Container
Finally, after our hard work, we’ll now gonna run our rails container using docker compose. To run the application container. We’ll need to run the following command.
$ docker-compose up
At last, we’ll gonna create the database by running the following command in a new terminal.
$ docker-compose run web rake db:create
Now, we are done. We can now access our Ruby app by browsing our web browser to our server on port 80 by default as our docker web container is exposed on port 80 ie http://ip-address or http://domain.com
Hurray!! Docker Compose is very easy to configure and makes our job pretty fast if we need run multiple Docker containers instantly. In this tutorial, we learned how we can run multiple containers in order to run our ruby on rails application using docker compose. Similarly, we can use docker compose to run WordPress, lamp stack and other various applications and servers in our Linux based servers.