Day 12: Creating your own Docker image and deploying it on AWS Beanstalk — Random Funny GIFs app
Yesterday we explored Docker and how to get setup to run a test app. If you haven’t read that yet, go here first to get things setup.
Today we will be creating our own docker image that you can share/ship to your friends or colleagues.
Project: Create a docker image for a distributed web app created in python for the use case of generating random funny gifs on demand. This app will be deployed using the docker image on AWS Elastic Beanstalk.
All the code for this project can be found on my github
Docker container used in this project can be found on my registry
Step 1: Create your python webapp
Open up your docker terminal app and create a directory for your webapp, for this project I am going to create a python app that generates random funny gifs using giphy API. Code for this can be found in my github profile and you can download it for testing.
You will see that we are going to require “flask” and “requests” packages for python, so go ahead add them to “requirements.txt” and install them as follows. You can prefix your command with sudo in case you run into any perms issue.
pip install -r requirements.txt
Now you can run your python app locally to test if everything is working fine.
If you headover to “localhost:5000” in your browser you should be able to see your webapp.
Step 2: Creating docker image
Now to update this project as a docker image that you can easily distribute.
- Create a file in your project directory called “Dockerfile” as below:
- Now build the docker file using the following command, in case you run into any connection issue the use the eval command as shown below. Note you need to replace the username below with your own docker repo name.
On successful build you will be seeing a bunch of python library build notifications on the terminal followed by “Successfully built”.
Ensure this docker built version is working fine by running this command and opening the address in a browser.
Step 3: Publishing your docker image
Publish this container image to your docker repo using this command.
Step 4: Deploying docker image on AWS Elastic Beanstalk
AWS Elastic Beanstalk is a PaaS (Platform as a Service) offered from Amazon Web Services that allows users to create applications and push them. In our case we simply point it to our docket image and and Beanstalk automatically handles the details of capacity provisioning, load balancing, scaling, and application health monitoring.
Setup AWS account if you don’t already have one and create a new app environment in beanstalk. The steps should be easy to follow along only things to note would be:
- Select “Predefined Configuration” as docker.
- For source use “upload your own” option to point to “Dockerrun.aws.json” file in your project (see sample on github). The image in this file should be your docker image.
You can breeze through all of other steps to finish creation of the app. It should get installed and published at your URL on beanstalk.
Head over the URL mentioned there, in my case: http://gifme.us-west-2.elasticbeanstalk.com/ to get continuous funny gifs on refresh to get you cracking!
Congrats you have successully deployed a docker image using AWS beanstalk, now you can go ahead and share this URL with your family/friends to put a smile on their face!
Day 12 of #100DaysOfCode DONE
If you enjoyed this, please click 👏 so that others can enjoy it as well. Follow me on Twitter @HariniLabs to get the latest updates or just to say Hi :)