This is a guideline for how to dockerize your Rails application with Puma and NGINX from your application’s git link.
What this tutorial is trying to achieve is creating two seperate container -one for your app and another for its database-. The diagram below explains the interaction between them.
I created a Docker image builder repo which helps you to build your application’s own docker image. Just follow the steps below:
docker run --name DATABASE_CONTAINER_NAME -e POSTGRES_PASSWORD=DATABASE_PASSWORD -d postgres
You can feel free to use other databases as your container. However, do not forget them to link them to your application container.
git clone https://github.com/decauville/rails-puma-nginx.git .
docker build --build-arg DATABASE_USER=DATABASE_USERNAME --build-arg DATABASE_PASSWORD=DATABASE_PASSWORD --build-arg DATABASE_HOST=DATABASE_CONTAINER_NAME --build-arg APP_URL=YOUR_APP_GIT_URL -t decauville:latest .
docker run -d --name APPLICATION_CONTAINER_NAME -p 80:80 --link DATABASE_CONTAINER_NAME decauville:latest
If you want to share a folder from your docker host with your application execute this:
docker run -d --name APPLICATION_CONTAINER_NAME -v DOCKER_HOST_DIRECTORY_PATH:APPLICATION_CONTAINER_DIRECTORY_PATH -p 80:80 --link curtain-db decauville:latest
And that’s it. If you are lucky enough and your app does not have any uncommon package dependencies, your app is ready to rock.
If you still feel that you don’t understand the process entirely, you can check out build and run script for my application.
During image building, it is possible to encounter some problems. Docker will inform you about at which step in Dockerfile failure occurs. Do not hesitate to modify Dockerfile. Just comment out the lines below the last successful step and build your image. To open up a bash terminal in your pre-built container, execute the command below.
docker run -it decauville:latest /bin/bash
In your container, try to execute the failing Dockerfile command manually. Don’t be reluctant to ask me questions and do not forget: Google is your best friend.
If you can find the reason why your image fails to build, and you can solve it just by editing the existing Dockerfile, please fork the Decauville project on the github.
The Decauville project contains configuration files for puma and NGINX. You can configure them for your requirements before building your application image.
Moreover, at your container startup, docker-entrypoint.sh is called to run your web servers. You can add additional steps to the script to execute the commands which is required by your application.