Dockerizing a Ruby on Rails APIManny ShapirBlockedUnblockFollowFollowingJun 16Containers provide a way to package your application, in an environment that is completely isolated from the one in which they actually run.
As a standard unit of software, a container packages up the code, and all of its dependencies, so that it runs efficiently from one environment to another.
This abstraction separates the development process from the deployment process — offering a flexible, portable, scalable, and lightweight alternative to deploying your applications in a private data center, the public cloud, or even locally on a personal machine.
Docker is a platform that enables developers to containerize their applications, where they can deploy, build, and run them in a Linux container.
How do we go about taking our locally running Ruby on Rails application and converting it to a Dockerized application, running in a container?Step 1: Environment SetupInstall Docker Desktop by choosing the desired operating system and following the onscreen instructions.
Once installed, ensure proper installation by opening up your terminal and running the following command:docker –versionExpect to see the Docker version you just installed.
An error stating that docker is not a recognized command, indicates that Docker wasn’t installed correctly or didn’t complete its installation.
docker run hello-worldThis command will run a simple Hello, World application.
You might notice a message that Docker couldn’t find an image called hello-world, but the correct message printed to the screen.
This is because Docker has a set of repositories, hosted on Docker Hub, to give new and experienced users alike a starting point for Dockerizing their applications, as well as exemplify best practices through well documented examples.
In ours, we’ll be using Docker Compose to run an application called ParkMe, running Ruby on a Node.
js server, with a PostgreSQL database.
The source code for ParkMe can be found below.
Feel free to fork and clone this repository, and check out the docker branch.
mshapir/ParkMe-Final-Backend-RailsBackend api using rails for parking app .
Contribute to mshapir/ParkMe-Final-Backend-Rails development by creating an…github.
comNow we can begin Dockerizing!Step 2: Create a DockerfileA Dockerfile defines exactly what happens inside of your container environment.
In the root directory of your project, create a new file named Dockerfile, and paste the following into the contents:We can quickly see the tools our application is using through the command flow defined in the Dockerfile — Ruby version 2.
4, a Node.
js server, and a PostgreSQL database — all of which can be easily tweaked to match your application stack.
Once you run your Docker image, Docker runs through this file line by line to install the latest versions of your tools, create the necessary directories and files for dependency management based on your tools.
Ours is Ruby, so we are using a Gemfile.
We can then run the application through the entrypoint.
sh script on port 3000.
Step 3: Create Entrypoint.
shIn the same directory as your Dockerfile, create a file called entrypoint.
sh and paste the following into its contents:Step 4: Create Docker-compose.
ymlDocker Compose is a tool for defining and running multi-container Docker applications.
We will use this tool to set ourselves up for the potential of running other related Docker applications together — the front end of our application, for example.
This file also defines how our application should behave in production.
In the same directory as your Dockerfile and entrypoint.
sh files, create a file called docker-compose.
yml and paste the following into its contents:This file defines the services that make up our application.
Be sure to replace YOUR_API_KEY with your Docker API key!Step 5: RunIn your terminal, run the following command to read your docker-compose.
yml and bring up application services inside of the container:docker-compose upIf you pulled a sample Dockerfile from a Docker guide, with the last line along the lines of: CMD [“rails”, “server”, “-b”, “0.
0”], you may run into some errors.
A clash between this command and the commands passed to Docker Compose, prevent the application from coming up, since both files specify a command to run every time the container comes up.
We comment this line out in the Dockerfile to allow the command bash -c “rm -f tmp/pids/server.
pid && bundle exec rails s -p 3000 -b ‘0.
0’” to start the application instead.
Step 6: Create Database InstancesBefore actually being able to use a back end service like ParkMe, you need to configure your database inside of your container, even if it was done on your local machine.
Remember that you are running your application in an isolated environment!Run the following command in your terminal:docker-compose run web rake db:createanddocker-compose run web rake db:migrateStep 7: TestTo verify that the service is up and running, navigate to an entry point in our REST API.
If you’re using ParkMe as an example, open up any web browser and paste in the following: http://localhost:3000/api/v1/users/.
You should see a list of users returned in JSON format (if you added some new users to the database in step six).
Our application is up and running as a service in a Docker container!Step 8: DeployDeploying your application can be done by making a few tweaks to your docker-compose.
yml file and running the following,docker swarm initto prevent errors relating to your Node not being a Swarm manager, and:docker stack deploy -c docker-compose.
yml nameofyourappSummaryContainerization is a wonderful way to abstract the infrastructure layer of your application, to ship code as efficiently as possible.
With just a few extra steps, you can have your application running in a Docker container as well!.