Deploying to AWS Part I: Docker Rails Deployment
What's the 2018 approach to deploying a Rails app to AWS? We've partnered with DailyDrip on a series of videos to guide you through the process. We'll be covering how to Dockerize a Rails app, AWS Fargate, logging, monitoring, and CDN support.
Today, we will be working through a few steps to get our app Dockerized and our image pushed to a hosted Docker registry.
Introduction to Docker
Docker is a tool that allows you to package your application code and any supporting libraries so you can ship and run that code anywhere. If you're familiar with virtual machines, it's similar, but avoids costly side effects, since it doesn't need an entire operating system to run. A container runs on top of the hosts and will use the hosts' kernels and allotted resources.
This makes Docker containers much smaller and easier to ship to colleagues, staging, and production. I'm sure you've heard or used the old adage "It works on my box." Using Docker, you can be assured that your servers are running the exact same code that you were running locally, so you shouldn't experience those problems.
Dockerizing our App
When Dockerizing an application, there are a few things that need to be done. First, you need to add a
Dockerfile. This file is the most important and really, the only requirement. This file defines the Docker image and instructs Docker on how to build it. When you
docker build .
Next, there's a
.dockerignore file that is used to specify certain files or folders that you don't want to include in the image being built. If you are familiar with Git you can think of this file as being equivalent to
Last, we're adding a
docker-compose.yml file that we will mainly be using to simplify local development. This can be configured for your dependencies such as Redis, Rabbitmq, Postgres, Mysql, along with many others. This will allow you to spin up containers for those dependencies, without the manual management that's typically associated with installing those pieces of software. This comes in very handy if you have many services. Simply by
Below is what we should have for our files.
See the Dockerfile reference for more information.
.dockerignore .git logs/ tmp/
Running our app
Now, we should be able to spin up the
First, we'll need to take care of setting up the database in the new container.
docker-compose run web rake db:create
- Run migrations:
docker-compose run web rake db:migrate
- Run seeds:
docker-compose run web rake db:seed
Now, we should be able to run our containers via
Now, if we switch over to our browser, we should be able to point to
localhost:3000 and see our app running.
Lastly, if we switch back to our terminal, we should see that our logs are actually working correctly.
Using Docker and Docker-Compose, we Dockerized a Rails app and its dependencies to allow us to conveniently spin up a Rails app and allow others to pull down the service and run it, without having to know about all of its dependencies and have them pre-configured on their box.