KEMBAR78
dockerizing web application | PDF
Walid Ashraf
Researcher , Software Developer, Instructor
about.me/WalidAshraf
DOCKERIZNG WEB
APPLICATIONS
Docker - Walid Ashraf
Table of Contents
Docker File Basics
Creating a NodeJS application
Creating a python application
Docker file acts as the source code for your
image where you specify all the needed tools,
data, code to run you image correctly in any
environment.
DOCKER FILE BASICS
Docker - Walid Ashraf
Docker file basics
From:
• Base Image
• Usage
• FROM <image>
Maintainer
• Who is the Image creator
• Usage
• MAINTAINER <Name>
WORKDIR
• Sets the start directory for the container where the run and the CMD instructions are executed.
• Usage
• WORKDIR /path/to/workdir
RUN
• The RUN instruction will execute any commands in a new layer on top of the current image and commit the results.The resulting committed image will be
used for the next step in the Dockerfile.
• Usage
• RUN <command> (the command is run in a shell - /bin/sh -c - shell form)
• RUN ["executable", "param1", "param2"] (exec form)
Docker - Walid Ashraf
Docker file basics
CMD
• The CMD Command is used as a default entry point to container.And if more than one is added the last one only will take effect
• Usage
• CMD ["executable","param1","param2"] (exec form, this is the preferred form)
• CMD ["param1","param2"] (as default parameters to ENTRYPOINT)
• CMD command param1 param2 (shell form)
COPY
• Copies files from the host file system to the image file system.
• Usage
• COPY <src>... <dest>
• COPY ["<src>",... "<dest>"] (this form is required for paths containing whitespace)
ADD
• The add file is similar to the copy but can handle both tar files and remote URLs
• Usage
• ADD <src>... <dest>
• ADD ["<src>",... "<dest>"] (this form is required for paths containing whitespace)
ENV
• Sets some environment variables for the container
• Usage
• ENV <key> <value> only one perline
• ENV <key>=<value> ... allows multiple per line
Docker - Walid Ashraf
Docker file basics
EXPOSE
• This allows the container ports to be accessed from the global ports. Note that Port
mapping has to be specified directly at creation time using the –p or –P commands
• Usage
• EXPOSE <port> [<port>...]
Vloume
• TheVOLUME instruction creates a mount point with the specified name and marks it
as holding externally mounted volumes from native host or other containers.
• Usage
• VOLUME ["/data"]
Docker - Walid Ashraf
Docker File Best Practices
Don’t add unnecessary files or libraries
Utilize the ecosystem and don't reinvent images
For more ideas and best practices check:
https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/
http://www.walidashraf.com/2015/10/docker-and-solid-princple.html
Create an application folder (node-app)
CREATING NODEJS
APPLICATION
Docker - Walid Ashraf
Step 1: Create package.json file
{
"name": "docker-ubuntu-hello",
"private": true,
"version": "0.0.1",
"description": "Node.js Hello world app using docker",
"author": "Yourname<Mail@Mail.com>",
"dependencies": {
"express": "3.2.4"
}
}
Docker - Walid Ashraf
Step 2: Create Index.JS File
var express = require('express');
// Constants
var PORT = 8080;
// App
var app = express();
app.get('/', function (req, res) {
res.send('Hello Docker Cairo Geeks :D n');
});
app.listen(PORT);
console.log('Running on http://localhost:' + PORT);
Docker - Walid Ashraf
Step 3: Create A DockerFile
# Aminimal alpine node image
FROM mhart/alpine-node
# Get The Code
COPY . /src
# Install app dependencies
RUN cd /src; npm install
#Expose a port
EXPOSE 8080
#The Container Start command
CMD ["node", "/src/index.js"]
Docker - Walid Ashraf
& the rest is history
Step 4: Build The Image from Docker File
$ sudo docker build -t washraf/nodeapp .
Step 5: Run the Docker Image
$ sudo docker run -p 1234:8080 –d --name nodeApplication washraf/nodeapp
Docker - Walid Ashraf
Other: Install form GitHub
The Repo:
https://github.com/washraf/docker-meetup
Install git:
apt-get install git
Get the Code:
git clone https://github.com/washraf/docker-meetup
Go to node-app
cd docker-meetup/node-app
Build the image
docker build - t name/app-name .
run the container
docker run -d -p 1234:8080 name/app-name
Docker - Walid Ashraf
The result
Create an application folder (flask-app)
CREATE A PYTHON
APPLICATION
Docker - Walid Ashraf
Step 1: Create File app.py
from flask import Flask, render_template
import random
app = Flask(__name__)
# list of cat images
images = [
"http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr05/15/9/anigif_enhanced-buzz-26388-1381844103-11.gif",
"http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr01/15/9/anigif_enhanced-buzz-31540-1381844535-8.gif",
"http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr05/15/9/anigif_enhanced-buzz-26390-1381844163-18.gif",
"http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr06/15/10/anigif_enhanced-buzz-1376-1381846217-0.gif",
"http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr03/15/9/anigif_enhanced-buzz-3391-1381844336-26.gif",
"http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr06/15/10/anigif_enhanced-buzz-29111-1381845968-0.gif",
"http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr03/15/9/anigif_enhanced-buzz-3409-1381844582-13.gif",
"http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr02/15/9/anigif_enhanced-buzz-19667-1381844937-10.gif",
"http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr05/15/9/anigif_enhanced-buzz-26358-1381845043-13.gif",
"http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr06/15/9/anigif_enhanced-buzz-18774-1381844645-6.gif",
"http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr06/15/9/anigif_enhanced-buzz-25158-1381844793-0.gif",
"http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr03/15/10/anigif_enhanced-buzz-11980-1381846269-1.gif"
]
@app.route('/')
def index():
url = random.choice(images)
return render_template('index.html', url=url)
if __name__ == "__main__":
app.run(host="0.0.0.0")
Docker - Walid Ashraf
Step 2: Create requirements.txt
Flask==0.10.1
Docker - Walid Ashraf
Step 3: Create templates/index.html
<html>
<head>
<style type="text/css">
body {
background: black; color: white;
}
div.container { max-width: 500px; margin: 100px auto; border: 20px solid white; padding: 10px; text-align: center; }
h4 {
text-transform: uppercase;
}
</style>
</head>
<body>
<div class="container">
<h4>Cat Gif of the day</h4>
<img src="{{url}}" />
<p>
<small>Courtesy: <a href="http://www.buzzfeed.com/copyranter/the-best-cat-gif-post-in-the-history-of-cat-gifs">Buzzfeed</a></small></p>
</div>
</body>
</html>
Docker - Walid Ashraf
Step 4: Create Docker file
# our base image
FROM alpine:latest
# Install python and pip
RUN apk add --update py-pip
# install Python modules needed by the Python app
COPY requirements.txt /usr/src/app/
RUN pip install --no-cache-dir -r /usr/src/app/requirements.txt
# copy files required for the app to run
COPY app.py /usr/src/app/
COPY templates/index.html /usr/src/app/templates/
# tell the port number the container should expose
EXPOSE 5000
# run the application
CMD ["python", "/usr/src/app/app.py"]
Docker - Walid Ashraf
& the rest is history
Step 5:
docker build -t washraf/flask-app .
Step 6:
docker run -p 8888:5000 -d --name flaskApplication washraf/flask-app
Docker - Walid Ashraf
The result
Docker - Walid Ashraf22

dockerizing web application

  • 1.
    Walid Ashraf Researcher ,Software Developer, Instructor about.me/WalidAshraf DOCKERIZNG WEB APPLICATIONS
  • 2.
    Docker - WalidAshraf Table of Contents Docker File Basics Creating a NodeJS application Creating a python application
  • 3.
    Docker file actsas the source code for your image where you specify all the needed tools, data, code to run you image correctly in any environment. DOCKER FILE BASICS
  • 4.
    Docker - WalidAshraf Docker file basics From: • Base Image • Usage • FROM <image> Maintainer • Who is the Image creator • Usage • MAINTAINER <Name> WORKDIR • Sets the start directory for the container where the run and the CMD instructions are executed. • Usage • WORKDIR /path/to/workdir RUN • The RUN instruction will execute any commands in a new layer on top of the current image and commit the results.The resulting committed image will be used for the next step in the Dockerfile. • Usage • RUN <command> (the command is run in a shell - /bin/sh -c - shell form) • RUN ["executable", "param1", "param2"] (exec form)
  • 5.
    Docker - WalidAshraf Docker file basics CMD • The CMD Command is used as a default entry point to container.And if more than one is added the last one only will take effect • Usage • CMD ["executable","param1","param2"] (exec form, this is the preferred form) • CMD ["param1","param2"] (as default parameters to ENTRYPOINT) • CMD command param1 param2 (shell form) COPY • Copies files from the host file system to the image file system. • Usage • COPY <src>... <dest> • COPY ["<src>",... "<dest>"] (this form is required for paths containing whitespace) ADD • The add file is similar to the copy but can handle both tar files and remote URLs • Usage • ADD <src>... <dest> • ADD ["<src>",... "<dest>"] (this form is required for paths containing whitespace) ENV • Sets some environment variables for the container • Usage • ENV <key> <value> only one perline • ENV <key>=<value> ... allows multiple per line
  • 6.
    Docker - WalidAshraf Docker file basics EXPOSE • This allows the container ports to be accessed from the global ports. Note that Port mapping has to be specified directly at creation time using the –p or –P commands • Usage • EXPOSE <port> [<port>...] Vloume • TheVOLUME instruction creates a mount point with the specified name and marks it as holding externally mounted volumes from native host or other containers. • Usage • VOLUME ["/data"]
  • 7.
    Docker - WalidAshraf Docker File Best Practices Don’t add unnecessary files or libraries Utilize the ecosystem and don't reinvent images For more ideas and best practices check: https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/ http://www.walidashraf.com/2015/10/docker-and-solid-princple.html
  • 8.
    Create an applicationfolder (node-app) CREATING NODEJS APPLICATION
  • 9.
    Docker - WalidAshraf Step 1: Create package.json file { "name": "docker-ubuntu-hello", "private": true, "version": "0.0.1", "description": "Node.js Hello world app using docker", "author": "Yourname<Mail@Mail.com>", "dependencies": { "express": "3.2.4" } }
  • 10.
    Docker - WalidAshraf Step 2: Create Index.JS File var express = require('express'); // Constants var PORT = 8080; // App var app = express(); app.get('/', function (req, res) { res.send('Hello Docker Cairo Geeks :D n'); }); app.listen(PORT); console.log('Running on http://localhost:' + PORT);
  • 11.
    Docker - WalidAshraf Step 3: Create A DockerFile # Aminimal alpine node image FROM mhart/alpine-node # Get The Code COPY . /src # Install app dependencies RUN cd /src; npm install #Expose a port EXPOSE 8080 #The Container Start command CMD ["node", "/src/index.js"]
  • 12.
    Docker - WalidAshraf & the rest is history Step 4: Build The Image from Docker File $ sudo docker build -t washraf/nodeapp . Step 5: Run the Docker Image $ sudo docker run -p 1234:8080 –d --name nodeApplication washraf/nodeapp
  • 13.
    Docker - WalidAshraf Other: Install form GitHub The Repo: https://github.com/washraf/docker-meetup Install git: apt-get install git Get the Code: git clone https://github.com/washraf/docker-meetup Go to node-app cd docker-meetup/node-app Build the image docker build - t name/app-name . run the container docker run -d -p 1234:8080 name/app-name
  • 14.
    Docker - WalidAshraf The result
  • 15.
    Create an applicationfolder (flask-app) CREATE A PYTHON APPLICATION
  • 16.
    Docker - WalidAshraf Step 1: Create File app.py from flask import Flask, render_template import random app = Flask(__name__) # list of cat images images = [ "http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr05/15/9/anigif_enhanced-buzz-26388-1381844103-11.gif", "http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr01/15/9/anigif_enhanced-buzz-31540-1381844535-8.gif", "http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr05/15/9/anigif_enhanced-buzz-26390-1381844163-18.gif", "http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr06/15/10/anigif_enhanced-buzz-1376-1381846217-0.gif", "http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr03/15/9/anigif_enhanced-buzz-3391-1381844336-26.gif", "http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr06/15/10/anigif_enhanced-buzz-29111-1381845968-0.gif", "http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr03/15/9/anigif_enhanced-buzz-3409-1381844582-13.gif", "http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr02/15/9/anigif_enhanced-buzz-19667-1381844937-10.gif", "http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr05/15/9/anigif_enhanced-buzz-26358-1381845043-13.gif", "http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr06/15/9/anigif_enhanced-buzz-18774-1381844645-6.gif", "http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr06/15/9/anigif_enhanced-buzz-25158-1381844793-0.gif", "http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr03/15/10/anigif_enhanced-buzz-11980-1381846269-1.gif" ] @app.route('/') def index(): url = random.choice(images) return render_template('index.html', url=url) if __name__ == "__main__": app.run(host="0.0.0.0")
  • 17.
    Docker - WalidAshraf Step 2: Create requirements.txt Flask==0.10.1
  • 18.
    Docker - WalidAshraf Step 3: Create templates/index.html <html> <head> <style type="text/css"> body { background: black; color: white; } div.container { max-width: 500px; margin: 100px auto; border: 20px solid white; padding: 10px; text-align: center; } h4 { text-transform: uppercase; } </style> </head> <body> <div class="container"> <h4>Cat Gif of the day</h4> <img src="{{url}}" /> <p> <small>Courtesy: <a href="http://www.buzzfeed.com/copyranter/the-best-cat-gif-post-in-the-history-of-cat-gifs">Buzzfeed</a></small></p> </div> </body> </html>
  • 19.
    Docker - WalidAshraf Step 4: Create Docker file # our base image FROM alpine:latest # Install python and pip RUN apk add --update py-pip # install Python modules needed by the Python app COPY requirements.txt /usr/src/app/ RUN pip install --no-cache-dir -r /usr/src/app/requirements.txt # copy files required for the app to run COPY app.py /usr/src/app/ COPY templates/index.html /usr/src/app/templates/ # tell the port number the container should expose EXPOSE 5000 # run the application CMD ["python", "/usr/src/app/app.py"]
  • 20.
    Docker - WalidAshraf & the rest is history Step 5: docker build -t washraf/flask-app . Step 6: docker run -p 8888:5000 -d --name flaskApplication washraf/flask-app
  • 21.
    Docker - WalidAshraf The result
  • 22.