Getting Started with Docker on AWS EC2

Docker and AWS EC2

Docker on Amazon EC2

Running Docker on AWS provides admins and developers a highly reliable, low-cost way to build, ship, and run distributed applications. Docker can be installed on many different operating systems, including Linux distributions, like Ubuntu, and even Mac OSX and Windows.

Prerequisites

  • AWS EC2 instance should be up and running.
  • Connect with your instance (e.g. via putty).
  • A non-root user with sudo privileges.
  • Python should be installed to run flask application.

Steps to Install Docker on an Amazon EC2 Instance

  • Update the installed packages on your instance with the below command:
sudo apt-get update
  • Add the GPG key to your instance for the official Docker repository:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  • Add the Docker repository to the APT sources:
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
  • Update the packages from the newly added repo:
sudo apt-get update
  • Install it from Docker repo instead of the default Ubuntu 16.04 repo:
apt-cache policy docker-ce
  • Install Docker:
sudo apt-get install -y docker-ce
  • To review the docker service status:
sudo service docker start
sudo service docker status
  • The output should display the service is active and running:

docker.service – Docker Application Container Engine

Loaded: loaded (/lib/systemd/zehntech/docker.service; enabled; vendor preset: enabled)

Active: active (running) since Fri 2019-09-13 05:47:06 UTC; 25s ago

Docs: https://docs.docker.com

Main PID: 1339 (dockerd)

Tasks: 10

Memory: 123.5M

CPU: 340ms

CGroup: /system.slice/docker.service

└─1339 /usr/bin/dockerd -H fd:// –containerd=/run/containerd/containerd.sock

Output

  • Installing Docker will provide you the Docker service (daemon) and also the Docker command-line utility.

Basic Commands for Docker

  • To view system-wide information about Docker, use below command:
sudo docker info
  • To list all the docker sub commands:
docker
  • To list all the available images:
sudo docker images
  • To list all the containers:
sudo docker ps
  • To search for images on Docker Hub, type docker search with image name:
sudo docker search ubuntu

Run Docker Image:

By default, docker pulls the images from a Docker registry called Docker Hub managed by Docker company.

To confirm whether you can download the images from Docker Hub:

sudo docker run hello-world
  • The below output indicates that Docker is working correctly:

Hello from Docker!

This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:

1. The Docker client contacted the Docker daemon.

2. The Docker daemon pulled the “hello-world” image from the Docker Hub. (amd64)

3. The Docker daemon created a new container from that image which runs the

executable that produces the output you are currently reading.

4. The Docker daemon streamed that output to the Docker client, which sent it

to your terminal.

Output

Build a Flask Application using Docker

Flask is known as a web micro-framework because it does not require any specific plugins to run, and it is built on Python. The Flask framework is very flexible and lightweight.

In this tutorial, we will create a Flask application then deploy it with Docker.

Setting up a Flask Application:

  • First, create a directory structure for the Flask Application. Run the below command to create a “DemoApp” directory in “/var/www/”.
sudo mkdir /var/www/DemoApp
  • Switch to the newly created directory:
cd /var/www/DemoApp
  • Create the basic folder structure:
sudo mkdir -p app/static app/temp
  • Create a file, required to run the Flask application:
sudo nano app/__init__.py
  • The above file indicates the python interpreter that the app directory is a package.
  • Add the following code to the above file, which will create a Flask instance and will import the logic from the views.py file (which we will create further).

from flask import Flask

app = Flask(__name__)

from app import views

/var/www/DemoApp/__init__.py

Save and close the file.

  • Create the “views.py” file, which will contain application logic.
sudo nano app/views.py
  • Add the code into views.py, which will return the “Hello Zehntech!” when we visit the web page.

from app import app

@app.route(‘/’)

def home():

return “Hello Zehntech!”

/var/www/DemoApp/app/views.py

The “@app.route” in the code block called “Decorator,” from where we define on which URL the home() method trigger.

Now, you’re ready to create the uwsgi.ini file with views.py file, which will contain the uWSGI configurations for our application. uWSGI is an application server and a protocol of deployment for the Nginx. There are multiple application servers available that can serve FastCGI, uWSGI, and HTTP protocols.

  • To create the uwsgi.ini file:
sudo nano uwsgi.ini
  • Add the below configuration into uwsgi.ini file:

[uwsgi]

module = main

callable = app

master = true

/var/www/DemoApp/uwsgi.ini

The main referred to as main.py, which instructs uWSGI to use the app instance exported by the main application. The application will keep running with the “master” option.

  • Next, create the entry point to the application, which is the “main.py” file. It will instruct uWSGI how to interact with the application.
sudo nano main.py
  • Import the previously create package named as “app” into the main.py:

from app import app

/var/www/DemoApp/main.py

  • Create a file named “requirement.txt” which contains the dependencies installed by pip package manager:
sudo nano requirements.txt
  • Write below line into the newly created file “requirement.txt” to specify the Flask version to be installed:

Flask==1.0.2

/var/www/DemoApp/app/requirement.txt

Save and close the file.
We have successfully set up our Flask application.

Creating a Dockerfile and Setting up Docker

First, we will create a Dockerfile which contains the commands used to assemble the image and create the docker deployment.

  • To create a Dockerfile:
sudo nano Dockerfile
  • Add the configuration commands into the Dockerfile which will specify the requirements and how the image will be built.

    NOTE: There are many tags available in Docker, but we are using the tag with alpine version e.g., FROM tiangolo/uwsgi-nginx-flask:python3.6-alpine3.7.
    As the flask is based on WSGI, therefore we use the Docker image with uWSGI and Nginx for creating Flask web applications in Python 3.6 version using Alpine Linux in a single container. It is the most common way to deploy web applications in Python for decent performance.

FROM tiangolo/uwsgi-nginx-flask:python3.6-alpine3.7

RUN apk –update add bash nano

ENV STATIC_URL /static

ENV STATIC_PATH /var/www/app/static

COPY ./requirement.txt /var/www/requirements.txt

RUN pip3 install -r /var/www/requirements.txt

/var/www/DemoApp/Dockerfile

  • Save and close the configuration file.
  • Create a “start.sh” script which builds the Docker container with below command:
sudo nano start.sh

This is a shell script that builds an image from the Dockerfile and creates a container from the docker image. Add your commands into the “start.sh” script like below:

#!/bin/bash

app=”zehntech.test”

docker build -t ${app} .

docker run –name=${app} \ -v $PWD:/app ${app}

/var/www/DemoApp/start.sh

This will create an image named “zehntech.test” which is saved as a variable named “app”.

  • Execute the script “start.sh” for creating the Docker image and then build a container from the image:
sudo bash start.sh
  • After executing the above script, run the command to list all the running containers:
sudo dokcer ps

CONTAINER ID IMAGE COMMAND PORTS NAMES
58b05508f4dd zehntech.test “/entrypoint.sh /sta…” 0.0.0.0:5000->80/tcp zehntech.test

Output

  • To check that the docker container is running or not, visit the IP address with the specified port e.g. 5000 in your browser like below:
http://ip_address:5000
  • You will see the output as shown below:

Great! We have successfully deployed our Flask application on Docker.

NoteIf the docker container is running, still the application is not loading, please check if the required ports are allowed in the firewall of your server or in the Inbound rules in AWS for EC2 instance.

Conclusion

In the above document, we have installed Docker in an AWS EC2 Instance (Ubuntu 16.04) and run some basic commands of Docker. After that, we have created a Flask application using Docker and created a Dockerfile to create the Docker image.
Finally, we have created the Docker container by running the docker image.

Related Posts

ELK stack setup on centos
AWS
Chanchal Khatri

ELK Stack Setup on CentOS

Contents Introduction Elasticsearch Kibana Logstash Introduction ELK stack is formerly known as Elastic Stack. It consists of Elasticsearch, Logstash, and Kibana. It helps you to store all your logs in one place and analyze the issues

Read More »
amazonsqs
AWS
Anuj Mandloi

Why Amazon SQS?

Contents Introduction Difference between SQS and MQ Benefits of SQS Getting started with AWS SQS Conclusion Introduction Amazon Web Services provides many services to make life easier. “Amazon SQS” is another service provided by Amazon

Read More »