Welcome to the third post in the Ruby on Rails Developer Series. In this part, our goal is to add a Docker container to our application so we can deploy our packaged application on a server. This will enable us to create a continuous integration process while building our environment in a matter of seconds. We’ll cycle through learning about the benefits of Docker and how to build a basic Docker image. The series theme is to make you feel confident as an engineer in building a structured project with Ruby on Rails.
You can do this!
What is Docker?
Docker allows you to package up an application or service with all of its dependencies in an image. The Docker image contains code, system libraries and whatever else you may need to install on a server.
What makes Docker different from a virtual machine?
Regarding a virtual machine (VM), you tend to have an entire guest operating system for each application you want to isolate. The time to reboot a server is also a longer process than being able to spin up a server in a few seconds. I think that says everything – when you can test your application in minutes vs hours.
What are the benefits?
- Encapsulation of your Application
- Expand Development Teams
Well, it is safe to assume that we can spin-up another server in seconds .. if it was Pokemon Go .. we wouldn’t have to waste time dealing with non-trivial developer workarounds.
Docker Compose relies on Docker Engine and we will need to set up our machine with docker to begin:
Install Docker below with your given OS:
This will install Docker and the tools including Docker Compose and other supporting apps.
Once installed, it’s time to set up our Rails/PostgresSQL application in our Dockerfile.
Define the project
We will need to create a file named
Entry point or entrypoint?
entrypoint file is a script based file ran when a container is started. We will use an entry point to fix a rails-specific issue that prevents the server from restarting when
What do these commands do?
set -e – allows you to change the values of shell options and set the positional parameters, or to display the names and values of shell variables. And
-e is set to exit immediately if something returns a non-zero status.
rm -f – allows you to remove a file path that is set. And
-f is to force the removal even if it has dependencies or will cause issues.
exec "$@" – typically used to make the entry point a pass-through that then runs the Docker command. It will replace the currently running shell with the command that
$@ is pointed at
How does the magic happen for development locally?
docker-compose.yml is where we describe the services the application contains. One service we will include is the
postgres database and
sidekiq. Although, this file will configure and bind the services together to properly execute expose the correct ports, etc.
Build the project
docker-compose run web rails new . --force --no-deps --database=postgresql
Context: We’re building the image for the web service which utilizes the Dockerfile. Then it runs rails new inside the container and we generate a fresh app.
You should see a list of files like below:
If you add gems in your Gemfile, you will need to rebuild with the following command:
Modify Database Config
Create database and run application!
docker-compose run web rake db:create
You should be able to view
http://localhost:3000 on the web browser to see the rails application boot up.
Overall, it really was that simple to dockerize our rails application! It could be a scary area, in my opinion, to enter for newer developers, although it really does help scale companies quickly.
In the last part of this series, we’re going to explore how to secure your application with best practices and some cool gems that keep the community very strong!
- Spin up a JSON API with Ruby on Rails
- Read about the power of strong APIs
- What’s the difference between JSON and XML
Published on Web Code Geeks with permission by Evan Glazer, partner at our WCG program. See the original article here: Ruby on Rails Developer Series: Creating a Docker Container Around Your Application
Opinions expressed by Web Code Geeks contributors are their own.