Back to docker and Rails, we’ve talked some time ago about how to dockerise your rails app, using docker and docker-compose to share a development environment configuration.
To do that, we’ll use ECS service, that is the Elastic Container Service, this will allow us to build a docker container image, upload it to an ECS registry, and run a one time command or start the container as a service in AWS.
To start lets create one small RDS instance, since we’ll need a database for our application, we could use a mysql container, but they already have a perfect service for that.
The first step in the ECS configuration, is to create a Repository, click on “Repositories” on the left and click on “Get Started”, then select any name you want for the repository, I’ll use “rails_docker_sample”.
The next screen will show the commands you need to push a docker image to that repository.
For this demo, we’ll use the same code from the previous post, with a small change in the Dockerfile, the change is already commited to github, you can just clone the repository from here: http://github.com:urubatan/rails_docker_sample
git clone git@github.com:urubatan/rails_docker_sample.git
In the app directory, we’ll run the commands from the amazon instructions:
$(aws ecr get-login --no-include-email --region us-east-1)
docker build -t rails_docker_sample .
docker tag rails_docker_sample:latest 896401241805.dkr.ecr.us-east-1.amazonaws.com/rails_docker_sample:latest
docker push 896401241805.dkr.ecr.us-east-1.amazonaws.com/rails_docker_sample:latest
These are the exact commands I used, you need to adjust according to the hosts and exact values amazon will present after you create your repository.
After this, we’ll click on the Task Definitions on the ECS menu, there we’ll create one Task Definition, I’ll call it sample_app, and I’ll use the Fargate launch type.
Fargate means AWS will take care of docker clusters for you, you can also make it deploy one entire configuration in ECS machines for you, but I’ll use the simpler approach.
For task memory I selected 0.5 and task vCPU 0.25, you can adjust it according to your application.
Then we’ll click to add a Container, I’ll use the name “app”, the “Image” in my case will be “896401241805.dkr.ecr.us-east-1.amazonaws.com/rails_docker_sample:latest” that is the same name I used in my “docker push” command.
The command is what will be executed in the container when you start it, it needs to be comma separated.
But for now, use this command: /myapp/bin/rails, s, -b, 0.0.0.0
The command is not needed if you added it in the Docker file, but this way it is a lot more flexible.
With this command, you cannot forget to access the database from your machine and setup the correct database name and run the migration.
In the “Port mappings” section, just add 3000, that will be the port we are exposing from our container.
It is not needed if we add an EXPOSE clause in our docker file, but as in the command, configuring it here is a lot more flexible.
Now to configure the database, you need to go to the bottom of the Environment section, the sample application uses environment variables to configure the database, you need to add the env vars:
- DATABASE_HOST
- DATABASE_USER
- DATABASE_PASSWORD
to point to the RDS instance you created
Now click add for the container, and create for the task definition and we are done with this step.
You can now click “View Task Definition” and in the “Actions” menu, select “Run Task”, and we’ll setup the first test run for our container.
In the next screen we need to select the cluster (I used the default, if you do not have one, create one in the clusters section), a VPC and a network inside that VPC as you can see in the image bellow.
Click on Edit besides the security group and add a “Custom TCP” rule to open the port 3000, or change the startup command to run your rails app in the port 80 that is open by default;
Click on “Run Task” in the bottom right of the screen, and you’ll see the task provisioning in the page, and if you are like me, you’ll keep some seconds clicking in the refresh button until you see the task status “Running”.
Once you see it running, you can click on the task ID and get the public IP of your task, and access your application.
Now that you know how to deploy your rails app on ECS you can just click on create service and make your task permanent, add a front proxy, load balancing, …
I can go deeper in the options in another post, please ask in the comments if you think that will be useful.
Or if you prefer, next week I can post how to deploy the dockerized rails app on Google Cloud, Oracle Cloud or Azure, just leave a comment so I know what will be more interesting for you!
Hi,
i would love to read more about this topic.
I would like to see how to put a front proxy, load balancer, etc. in front and maybe have multiple applications running with different domains assigend to each docker container ?
Thx and greetings
Holger