28
Shipping Python Projects by Docker Wei-Ting Kuo

Shipping python project by docker

Embed Size (px)

Citation preview

Page 1: Shipping python project by docker

Shipping Python Projects by Docker

Wei-Ting Kuo

Page 2: Shipping python project by docker

Outline• Introduction to Docker

• define our sample web app

• Build/Run Docker Images

• Docker Index

• Deploy to AWS

Page 3: Shipping python project by docker

The Challenge

Page 4: Shipping python project by docker

The Matrix From Hell

Page 5: Shipping python project by docker

Cargo Transport Pre-1960

Page 6: Shipping python project by docker

The Matrix From Hell

Page 7: Shipping python project by docker

Solution: Intermodal Shipping Container

Page 8: Shipping python project by docker

Docker is a shipping container system for code

Page 9: Shipping python project by docker

Docker eliminates the matrix from hell

Page 10: Shipping python project by docker

VMs vs Containers

Page 11: Shipping python project by docker

Installation (Ubuntu14.04)

• $ sudo apt-get update

• $ sudo apt-get install docker.io

• $ sudo ln -sf /usr/bin/docker.io /usr/local/bin/docker

Page 12: Shipping python project by docker

Pull a Base Image

• docker pull ubuntu

Page 13: Shipping python project by docker

List Docker Images

• docker images

REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE ubuntu 13.10 5e019ab7bf6d 3 weeks ago 180 MB ubuntu saucy 5e019ab7bf6d 3 weeks ago 180 MB ubuntu 12.04 74fe38d11401 3 weeks ago 209.6 MB ubuntu precise 74fe38d11401 3 weeks ago 209.6 MB ubuntu 12.10 a7cf8ae4e998 3 weeks ago 171.3 MB ubuntu quantal a7cf8ae4e998 3 weeks ago 171.3 MB ubuntu 14.04 99ec81b80c55 3 weeks ago 266 MB ubuntu latest 99ec81b80c55 3 weeks ago 266 MB ubuntu trusty 99ec81b80c55 3 weeks ago 266 MB ubuntu raring 316b678ddf48 3 weeks ago 169.4 MB ubuntu 13.04 316b678ddf48 3 weeks ago 169.4 MB

Page 14: Shipping python project by docker

Run Something• docker run <image> <command … >

• docker run ubuntu ls -l /rootdrwxr-xr-x 2 root root 12288 Apr 16 20:36 bin drwxr-xr-x 2 root root 4096 Apr 10 22:12 games drwxr-xr-x 2 root root 4096 Apr 16 20:36 include drwxr-xr-x 26 root root 4096 Apr 16 20:36 lib drwxr-xr-x 10 root root 4096 Apr 16 20:35 local drwxr-xr-x 2 root root 4096 Apr 24 16:17 sbin drwxr-xr-x 52 root root 4096 Apr 16 20:36 share drwxr-xr-x 2 root root 4096 Apr 10 22:12 src

Page 15: Shipping python project by docker

Simple Flask App

• https://github.com/waitingkuo/flask-sample

• flask-sample - app.py - requirements.txt - Dockerfile

Page 16: Shipping python project by docker

app.pyfrom flask import Flask app = Flask(__name__) !

@app.route("/") def hello(): return "Hello World!" !

if __name__ == "__main__": app.run(‘0.0.0.0’, port=5000)

Page 17: Shipping python project by docker

Requirements.txt

flask

Page 18: Shipping python project by docker

Install and Run flask-sample 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 19: Shipping python project by docker

How to build a Docker Image

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

Page 20: Shipping python project by docker

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 21: Shipping python project by docker

Build a Image• docker build -t <image name> <Dockerfile PATH>

• docker build -t myapp .Uploading context 76.29 kB Uploading context Step 0 : FROM ubuntu ---> 99ec81b80c55 Step 1 : RUN apt-get install -y python-setuptools ---> Using cache ---> d651a6cb230c Step 2 : RUN easy_install pip ---> Using cache ---> ecf1474da849 Step 3 : ADD requirements.txt /src/requirements.txt ---> Using cache ---> 274c84b5415e !… (ignore) … !Successfully built b4c78de4abc1

Page 22: Shipping python project by docker

Check Your new Image• docker images

REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE myapp latest b4c78de4abc1 2 minutes ago 302.7 MB ubuntu 13.10 5e019ab7bf6d 3 weeks ago 180 MB ubuntu saucy 5e019ab7bf6d 3 weeks ago 180 MB ubuntu 12.04 74fe38d11401 3 weeks ago 209.6 MB ubuntu precise 74fe38d11401 3 weeks ago 209.6 MB ubuntu 12.10 a7cf8ae4e998 3 weeks ago 171.3 MB ubuntu quantal a7cf8ae4e998 3 weeks ago 171.3 MB ubuntu 14.04 99ec81b80c55 3 weeks ago 266 MB ubuntu latest 99ec81b80c55 3 weeks ago 266 MB ubuntu trusty 99ec81b80c55 3 weeks ago 266 MB ubuntu raring 316b678ddf48 3 weeks ago 169.4 MB ubuntu 13.04 316b678ddf48 3 weeks ago 169.4 MB

Page 23: Shipping python project by docker

Run It!

• docker run -d -p 5000:5000 myapp -d detached mode -p port forwarding

Page 24: Shipping python project by docker

How to check the Running Docker Process?

• docker ps

CONTAINER ID IMAGE COMMAND … PORTS 6294c5a5e9be myapp:latest /bin/sh -c 'python s … 0.0.0.0:5000->5000/tcp

• To kill the process, use docker kill <CONTAINER ID>

Page 25: Shipping python project by docker

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

• You might think it as the GitHub For Docker Image

• You can pull 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 26: Shipping python project by docker

The Repository for our flask-sample

• https://index.docker.io/u/waitingkuo/flask-sample/

Page 27: Shipping python project by docker

Deploy!

• Use whatever you want to login to your production server (ssh, fabric, capistrano, chef, puppet, ….)

• docker pull waitingkuo/flask-sample

• docker run -d -p 5000:5000 waitingkuo/flask-sample

Page 28: Shipping python project by docker

• Thank you :)