Content Introduction Prerequisites Virtual Host Redirect to HTTPS Conclusion Introduction To secure the data transfer redirecting the HTTP traffic to...
Read MoreRunning 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.
sudo apt-get update
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
apt-cache policy docker-ce
sudo apt-get install -y docker-ce
sudo service docker start
sudo service docker status
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
sudo docker info
docker
sudo docker images
sudo docker ps
sudo docker search ubuntu
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
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
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.
sudo mkdir /var/www/DemoApp
cd /var/www/DemoApp
sudo mkdir -p app/static app/temp
sudo nano app/__init__.py
from flask import Flask
app = Flask(__name__)
from app import views
/var/www/DemoApp/__init__.py
Save and close the file.
sudo nano app/views.py
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.
sudo nano uwsgi.ini
[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.
sudo nano main.py
from app import app
/var/www/DemoApp/main.py
sudo nano requirements.txt
Flask==1.0.2
/var/www/DemoApp/app/requirement.txt
Save and close the file.
We have successfully set up our Flask application.
First, we will create a Dockerfile which contains the commands used to assemble the image and create the docker deployment.
sudo nano Dockerfile
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
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”.
sudo bash start.sh
sudo dokcer ps
CONTAINER ID IMAGE COMMAND PORTS NAMES
58b05508f4dd zehntech.test “/entrypoint.sh /sta…” 0.0.0.0:5000->80/tcp zehntech.testOutput
http://ip_address:5000
Great! We have successfully deployed our Flask application on Docker.
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.
Content Introduction Prerequisites Virtual Host Redirect to HTTPS Conclusion Introduction To secure the data transfer redirecting the HTTP traffic to...
Read MoreContent Introduction Requirement Getting Started Conclusion Introduction Angular is an open-source web application framework. It is a TypeScript-based free and development...
Read More