46
www.aurorasolutions.io Ship Python Apps with Docker!

Ship python apps with docker!

Embed Size (px)

Citation preview

Page 1: Ship python apps with docker!

www.aurorasolutions.io

Ship Python Apps with Docker!

Page 2: Ship python apps with docker!

www.aurorasolutions.io

Raise your hands if you have...

❖ Tried Docker (online tutorial)❖ Tried real Docker (e.g. deployed on remote VM)❖ Installed Docker locally (e.g. with boot2docker)❖ Written a Dockerfile (and built it!)❖ An image on Docker hub (pushed or autobuilt)❖ Deployed Docker images for dev/QA/test/prod… etc.

Page 3: Ship python apps with docker!

www.aurorasolutions.io

About: Rasheed

Full Stack Developer & Co-Founder @ Aurora Solutions - Provides REMOTE Teams specializing in JVM languages and Angular + Ember

My team at Aurora specializes in:► Web/Backend Apps ◄ Java, Groovy, Grails, C# and AngularJS/EmberJS based single or multi page web apps► Mobile Apps ◄ Android & iOS

Business domains we specialize in:¤ Media Streaming¤ Automated/Algorithmic Trading¤ Bitcoin / Crypto Currency

LinkedIn: https://se.linkedin.com/in/rasheedwaraich

Email: [email protected]

Page 4: Ship python apps with docker!

www.aurorasolutions.io

Agenda● Background● Container vs VM● Define Sample App● Build/Run Docker Images● Docker Index● Deploy Locally● Deploy AWS● Deploy GCE

Page 5: Ship python apps with docker!

www.aurorasolutions.io

Background

Page 9: Ship python apps with docker!

www.aurorasolutions.io

A useful analogy...

Page 16: Ship python apps with docker!

www.aurorasolutions.io

Say again?

❏ Build: package your application in a container❏ Ship: move that container from a machine to another❏ Run: execute that container❏ Any application: anything that runs on Linux❏ Anywhere: local VM, cloud instance, bare metal…

Page 17: Ship python apps with docker!

www.aurorasolutions.io

Container vs. VM

Page 20: Ship python apps with docker!

www.aurorasolutions.io

Basic Docker Concepts

Page 21: Ship python apps with docker!

www.aurorasolutions.io

Main Docker Parts

● docker daemon○ used to manage docker (LXC) containers on the host it runs

● docker CLI○ used to command and communicate with the docker daemon

● docker image index○ a repository (public or private) for docker images

Page 22: Ship python apps with docker!

www.aurorasolutions.io

Main Docker Elements

● Dockerfiles○ scripts automating the building process of images

● docker images○ snapshots of containers or base OS (e.g. Ubuntu) images

● docker containers○ directories containing everything-your-application

Page 23: Ship python apps with docker!

www.aurorasolutions.io

Install Docker (Ubuntu 14.04)

Step 1: add docker repository key to apt-key for package verification

sudo sh -c "wget -qO- https://get.docker.io/gpg | apt-key add -"

Step 2: add the docker repository to your list of repositories:sudo sh -c "echo deb http://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list"

Step 3: finally install docker with an apt-get combo:sudo apt-get updatesudo apt-get install lxc-docker

Page 24: Ship python apps with docker!

www.aurorasolutions.io

Sample python flask app!

Page 25: Ship python apps with docker!

www.aurorasolutions.io

Sample flask app

❏ repo❏ https://github.com/rasheedamir/flask-sample

❏ flask-sample app structure❏ app.py❏ requirements.txt❏ Dockerfile

Page 26: Ship python apps with docker!

www.aurorasolutions.io

app.pyfrom flask import Flaskapp = Flask(__name__)

@app.route("/")def hello():

return "Welcome To PyCon Sweden 2015!"

if __name__ == "__main__":app.run('0.0.0.0')

Page 27: Ship python apps with docker!

www.aurorasolutions.io

requirements.txt

flask

Page 28: Ship python apps with docker!

www.aurorasolutions.io

Install & Run locally!

❏ sudo apt-get install -y python-setuptools

❏ sudo easy_install pip

❏ sudo pip install -r requirements.txt

❏ python app.py

* Running on http://0.0.0.0:5000/

Page 29: Ship python apps with docker!

www.aurorasolutions.io

How to build a Docker Image?

Dockerfile: http://docs.docker.io/reference/builder/

Page 30: Ship python apps with docker!

www.aurorasolutions.io

DockerfileFROM ubuntu

# Install Python Setuptools

RUN apt-get install -y python-setuptools

# Install pip

RUN easy_install pip

# Install requirements.txt

ADD requirements.txt /src/requirements.txt

RUN cd /src; pip install -r requirements.txt

# Add the Flask App

ADD . /src

# EXPOSE PORT

EXPOSE 5000

# Run the Flask APP

CMD python src/app.py

Page 31: Ship python apps with docker!

www.aurorasolutions.io

Build an Image

❏ docker build -t <image name> <Dockerfile PATH>

❏ docker build -t pycon-app .Step 0 : FROM ubuntu

---> 07f8e8c5e660

Step 1 : RUN apt-get install -y python-setuptools

---> Using cache

---> f2a1db95b4b8

Step 2 : RUN easy_install pip

---> Using cache

---> 643c89e6188d

…(ignore)...

Successfully built 403ed7ef1455

Page 32: Ship python apps with docker!

www.aurorasolutions.io

List an Image

❏ docker images

Page 33: Ship python apps with docker!

www.aurorasolutions.io

Run it!

❏ docker run -d -p 5000:5000 pycon-app❏ -d : detached mode❏ -p : port forwarding

Page 34: Ship python apps with docker!

www.aurorasolutions.io

List Container(s)

❏ docker ps -a❏ -a: list stopped containers as well

Page 35: Ship python apps with docker!

www.aurorasolutions.io

Stop/Kill Container(s)

❏ docker kill <CONTAINER ID>

Page 36: Ship python apps with docker!

www.aurorasolutions.io

Remove Container!

❏ docker rm <CONTAINER ID>❏ removes the container

Page 37: Ship python apps with docker!

www.aurorasolutions.io

Remove Image!

❏ docker rmi <IMAGE ID>❏ removes the image!

Page 38: Ship python apps with docker!

www.aurorasolutions.io

Deploy Locally!

Page 39: Ship python apps with docker!

www.aurorasolutions.io

Docker Index

Page 40: Ship python apps with docker!

www.aurorasolutions.io

Docker Index❏ https://index.docker.io/

❏ It’s GitHub for Docker images!

❏ You can pull activemq, mysql, mongodb, hadoop, … etc.

❏ You can hook your docker index repository on to your own repository on GitHub!!!❏ Build images automatically once you push something to Github

Page 41: Ship python apps with docker!

www.aurorasolutions.io

flask-sample repository❏ https://registry.hub.docker.com/u/rasheedamir/flask-sample/

Page 42: Ship python apps with docker!

www.aurorasolutions.io

Deploy on EC2!

Page 43: Ship python apps with docker!

www.aurorasolutions.io

Deploy on GCE!

Page 44: Ship python apps with docker!

www.aurorasolutions.io

Docker Linking

Page 46: Ship python apps with docker!

www.aurorasolutions.io

Thank you!Questions...