Deploying a Scrivito Application to Amazon ECS with Docker

Since November 2014, Amazon Web Services provide a new service for managing Docker-based infrastructure on your EC2 instances. Amazon says: "Amazon EC2 Container Service (ECS) allows you to easily run and manage Docker-enabled applications across a cluster of Amazon EC2 instances." We gave it a try with a basic Rails application into which Scrivito has been integrated. The following steps provide an overview of how to easily run a Scrivito application within a docker container using ECS. Before we continue, please note that this article does not result in a production-ready setup but is meant to be a short introduction to the topic to get you started. Before you start Our guide assumes that you've already integrated Scrivito into your application. If you do not have a Scrivito application yet, please register and follow the intructions in the documentation. You also need to install docker to your local machine (e.g. https://docs.docker.com/mac/started/) and create a new Dockerfile for building an image for your application. Here is the one we used for running the Scrivito-enabled application: FROM ruby:2.2.3 RUN apt-get update \ && apt-get install -y nodejs --no-install-recommends \ && rm -rf /var/lib/apt/lists/* RUN bundle config --global frozen 1 RUN mkdir -p /usr/src/app WORKDIR /usr/src/app COPY Gemfile /usr/src/app/ COPY Gemfile.lock /usr/src/app/ RUN bundle install --without development test COPY . /usr/src/app EXPOSE 3000 CMD bin/rails s -b 0.0.0.0 -p 3000 To be able to deploy it to ECS, the docker image must be publicly available in a docker registry (we used hub.docker.com). The image is fully self-contained and includes everything needed to run our Scrivito application. The last line starts the application using the default webrick server (again, this is no production-ready setup and the image should be extended with Unicorn/Nginx or Passenger/Apache for real world use). Before you can deploy your application to ECS, you need to build the image on your local machine. For this, a Docker hub account (or an account for any other docker registry) is required. Replace "your_user_name" in the following commands with your user name. $ docker build -t your_user_name/scrivito-ecs-test . This will take some minutes. After the image has been built, you can run it on your local machine to test it: $ docker run -it --rm -p 3000:3000 your_user_name/scrivito-ecs-test This should start up the rails application. If it starts up fine, you can push it to the registry: $ docker push your_user_name/scrivito-ecs-test:latest Deploying the container to ECS So far, everything we did was standard Docker stuff. In the next steps, we need to deploy our docker image to the ECS environment. For our example, we created a new AWS trial account which lets us use a limited number of AWS resources for a year, but you can also use any existing AWS account if you have one. Please log in to your account and make sure that you have an SSH key pair set up. It will be needed later in the process. After logging in to your account, click Services and select EC2 Container Service. The next screen offers a short introduction to ECS. To proceed, click the Get Started button. ECS provides a step-by-step guide for creating your first container environment. Please select Custom and move on to the next step. The following page represents the configuration interface for your Task Definition. We do not have any container yet and add a new one using Add Container Definition. The form for adding a new container includes a lot of different options. The ones important for us are shown in the image below. You should at least specify:  Container name - self explaining Image - this is the name of the image on docker hub you pushed earlier Memory - the free trial includes 1 GB, but 512 Mbyte should be enough Port Mappings - to be able to access your application from outside the container (choose the port used in your Dockerfile) Environment Variables - you need to add your Scrivito credentials here After filling in the necessary information, click Add and proceed to the next step. We would like to create a new service because our application should run permanently. If you want to run the service on more than one server, increase the Desired number of Tasks. We recommend running one Task per server. To access our application in a browser we also need to add a new Elastic Load Balancer. After choosing the automatically created one, an additional form opens. The default values are good enough for our test case, so we proceed to the next step. On the screen above, your cluster configuration is displayed. You may change the Number of Instances (preferably to the number of Tasks you chose earlier) and the Instance Type and select the Key pair you added in the beginning. In the AWS free trial, you may run one t2.micro instance for a whole month, but you can also spread the ~730 hours over multiple machines. In our example we will run two micro instances. You should also select an IAM instance and service role. If you do not have those set up yet, you can create them using Create IAM Roles. After clicking the Review & Launch button, you can check your configuration once again. Then click Launch Instances and Run Services to have everything you need set up. During the setup process, AWS displays a progress bar. Depending on the complexity of your docker configuration and the instance size, the setup can take a couple of minutes. When your service is marked as running (left image), you need to find out how to access your application. To do this, click the highlighted link. The screen shown on the right-hand side should appear. The DNS Name that can be seen is the publicly accessible domain of your application. As the final step, copy the DNS Name to the address bar of your browser. If everything has been set up correctly, you should see your Scrivito application on the screen. Further reading Please keep in mind that this is just an example that demonstrates how easy a Scrivito application can be set up using Docker. It does not make use of all the advantages of Docker or ECS. If you want to dive deeper into the Docker world, take a look at the docker user guide. In addition, the ECS introduction should also be helpful. If you would like to learn more about Scrivito, please have a look at its extensive documentation.

More great blog posts from Marcel