dockerizing web application

22
Walid Ashraf Researcher , Software Developer, Instructor about.me/WalidAshraf DOCKERIZNG WEB APPLICATIONS

Transcript of dockerizing web application

Page 1: dockerizing web application

Walid AshrafResearcher , Software Developer, Instructor

about.me/WalidAshraf

DOCKERIZNG WEB APPLICATIONS

Page 2: dockerizing web application

Docker - Walid Ashraf

Table of ContentsDocker File Basics

Creating a NodeJS application

Creating a python application

Page 3: dockerizing web 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

Page 4: dockerizing web application

Docker - Walid Ashraf

Docker file basicsFrom:

• 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)

Page 5: dockerizing web application

Docker - Walid Ashraf

Docker file basicsCMD

• 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

Page 6: dockerizing web application

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

• The VOLUME 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"]

Page 7: dockerizing web application

Docker - Walid Ashraf

Docker File Best PracticesDon’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

Page 8: dockerizing web application

Create an application folder (node-app)

CREATING NODEJS

APPLICATION

Page 9: dockerizing web 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<[email protected]>",

"dependencies": {

"express": "3.2.4"

}

}

Page 10: dockerizing web application

Docker - Walid Ashraf

Step 2: Create Index.JS Filevar 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);

Page 11: dockerizing web application

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"]

Page 12: dockerizing web application

Docker - Walid Ashraf

& the rest is historyStep 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

Page 13: dockerizing web application

Docker - Walid Ashraf

Other: Install form GitHubThe 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-appcd docker-meetup/node-app

Build the imagedocker build - t name/app-name .

run the containerdocker run -d -p 1234:8080 name/app-name

Page 14: dockerizing web application

Docker - Walid Ashraf

The result

Page 15: dockerizing web application

Create an application folder (flask-app)

CREATE A PYTHON

APPLICATION

Page 16: dockerizing web application

Docker - Walid Ashraf

Step 1: Create File app.pyfrom flask import Flask, render_templateimport randomapp = Flask(__name__)# list of cat imagesimages = [

"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")

Page 17: dockerizing web application

Docker - Walid Ashraf

Step 2: Create requirements.txtFlask==0.10.1

Page 18: dockerizing web application

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>

Page 19: dockerizing web application

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"]

Page 20: dockerizing web application

Docker - Walid Ashraf

& the rest is historyStep 5:

docker build -t washraf/flask-app .

Step 6:

docker run -p 8888:5000 -d --name flaskApplication washraf/flask-app

Page 21: dockerizing web application

Docker - Walid Ashraf

The result

Page 22: dockerizing web application

Docker - Walid Ashraf22