Skip to content

Commit bc9632d

Browse files
committed
demo updates
1 parent 350b2ee commit bc9632d

File tree

7 files changed

+149
-33
lines changed

7 files changed

+149
-33
lines changed

README.md

+48-28
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
# Containerization: From 0 to a vizualization microservice
1+
# Containerization: From 0 to a visualization micro-service
22
## (and why you shouldn't do it this way)
33

44
This is a simple tutorial on how get started with containers by deploying a visualization web application.
55

66
## Terminology
7-
- *Image*: Executable package that contains the application
7+
- *Image*: Executable package that contains the application
88
- *Container*: Running (or terminated) instance of an image, containers survive after finish. Need to delete them when done
99
``` docker rm $(docker ps -a -q -f status=exited) ```
1010
- *Dockerfile*: Configuration file with the instructions to build an image
11-
- *Docker deamon*: Background service which runs the deamon and controls the containers and images
12-
- *Docker client*: CLI tool to access the docker service to build, deploy and delete containers
11+
- *Docker daemon*: Background service which runs the daemon and controls the containers and images
12+
- *Docker client*: CLI tool to access the docker service to build, deploy and delete containers
1313
- *Docker Hub*: Docker official registry service
1414

1515
## Let's start with the very basic
@@ -21,12 +21,12 @@ Busybox is just a shell, nothing else:
2121
Build a very simple html server:
2222

2323
docker build -t demo1 demo1/.
24-
25-
To run it in deamon mode use `-d` and to expose the port use `-p <PORT HOST>:<PORT INSIDE CONTAINER TO BE EXPOSED>` since this will be running nginx, by default is using port 80
2624

27-
docker run -d -p 8080:80 --name my-first-demo demo1
28-
29-
`--name` is optional. Let's see if the container based on image `demo1` is running,
25+
To run it in daemon mode use `-d` and to expose the port use `-p <PORT HOST>:<PORT INSIDE CONTAINER TO BE EXPOSED>` since this will be running nginx, by default is using port 80
26+
27+
docker run -d -p 8080:80 --name my-first-demo demo1
28+
29+
`--name` is optional. Let's see if the container based on image `demo1` is running,
3030

3131
docker ps
3232

@@ -35,38 +35,57 @@ You should see something like this:
3535
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3636
cabe55ffa71f demo1 "nginx -g 'daemon ..." 2 seconds ago Up 1 second 0.0.0.0:8080->80/tcp my-first-demo
3737
```
38+
Then you can open your browser and go to [http://localhost:8080](http://localhost:8080)
39+
3840
Let's stop and delete the container
3941

4042
docker stop my-first-demo
4143
docker rm my-first-demo
4244

43-
or you can use: `docker rm my-first-demo --force`.
45+
or you can use: `docker rm my-first-demo --force`
46+
47+
You can also use jupyterlab images and run jupyter from inside a container
48+
49+
docker run -p 8888:8888 jupyter/base-notebook
50+
51+
and open your browser at [http://localhost:8888](http://localhost:8888) and copy/paste the token printed on your screen
4452

53+
To save the work done on the notebooks you need to mount a local volume so when the container is deleted you can still have your notebooks saved.
4554

46-
## Simple deployement of the application
55+
docker run --rm -p 8888:8888 -e JUPYTER_LAB_ENABLE=yes -v $PWD/jhub:/home/jovyan/work jupyter/base-notebook
56+
57+
There are some interesting images in Docker Hub you can run directly, **BUT** there is also malicious images runing as root that can mess up your system. Don't copy/paste or run any images you are not sure of or you haven't seen the Dockerfile
58+
59+
60+
## Simple deployment of the application
4761

4862
Two containers will be created, one contains a simple MySQL DB and another one a simple interactive user interface written in python, they will be linked internally.
4963

5064
### Deploy MySQL
5165

52-
To deplot locally, replace the variables in `<>`:
66+
To deploy locally, replace the variables in `<>`:
5367

54-
docker run --name <NAME_MYSQL> -p <PORT>:3306 -e MYSQL_ROOT_PASSWORD=<PASSWORD> -d mysql:5.6
68+
docker run --rm --name <NAME_MYSQL> -p <PORT>:3306 -e MYSQL_ROOT_PASSWORD=<PASSWORD> -d mysql:5.6
69+
docker stop <NAME_MYSQL>
5570

56-
To add a volume to make it psersistent after deletion:
71+
To add a volume to make it persistent after deletion:
72+
73+
docker run --rm --name <NAME_MYSQL> -v $PWD/data/:/var/lib/mysql -p <PORT>:3306 -e MYSQL_ROOT_PASSWORD=<PASSWORD> -d mysql:5.6
5774

58-
docker run --name <NAME_MYSQL> -v $PWD/data/:/var/lib/mysql -p <PORT>:3306 -e MYSQL_ROOT_PASSWORD=<PASSWORD> -d mysql:5.6
59-
6075
You can actually access the DB using (need to have mysql client installed):
6176

6277
'mysql -h 127.0.0.1 -P <PORT> -u root -p<PASSWORD>'
63-
78+
79+
To delete the container:
80+
81+
docker stop <NAME_MYSQL>
82+
6483
Since we will use links internally, there is no need to expose the port (its more secure):
6584

66-
docker run --name demo-mysql -v $PWD/data/:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=<PASSWD> -d mysql:5.6
67-
68-
69-
> Note that by default the containers **run as root** which is a terrible idea. :grimacing:
85+
docker run --rm --name demo-mysql -v $PWD/data/:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=<PASSWD> -d mysql:5.6
86+
87+
88+
> Note that by default the containers **run as root** which is a terrible idea. :grimacing:
7089
7190
### Deploy the front-end
7291

@@ -75,20 +94,22 @@ Let's build the front end:
7594
docker build -t webapp webpage/.
7695

7796
To deploy locally, we need to link to `demo-mysql` and add environmental variables with `-e`:
78-
97+
7998
docker run --name my-first-app -d -p 8080:8080 --link demo-mysql:remote-mysql -e MYSQL_USER=root -e MYSQL_PASS=<PASSWORD> webapp
8099

100+
Now you can access in your browser at [http://localhost:8080](http://localhost:8080)
101+
81102
Let's get inside the container:
82103

83104
docker exec -it my-first-app bash
84-
105+
85106
Exercise: Let's modify the `website/template/main.html` and re-deploy.
86107

87108

88109

89110
## Deployment on AWS
90111

91-
Using [AWS EC2](https://github.com/mgckind/container_demo.git) services
112+
Using [AWS EC2](https://github.com/mgckind/container_demo.git) services
92113

93114
After deploying an EC2 instance, install docker (There are other pre-installed options, or even [Beanstalk](https://aws.amazon.com/elasticbeanstalk/)):
94115

@@ -102,15 +123,15 @@ You can either pull a repository and build the images or use the DockeHub regist
102123
We need to push the tag and push the images, (only webapp):
103124

104125
docker tag webapp mgckind/my-demo-app:1.0.0
105-
126+
106127
Then we need to login to the Hub:
107128

108129
docker login
109-
130+
110131
and push the image:
111132

112133
docker push mgckind/my-demo-app:1.0.0
113-
134+
114135
Login to AWS, launch an instance, install docker, pull the images and repeat the steps above
115136

116137
In this case you need to expose 8080 to 80 and use the correct image name
@@ -121,4 +142,3 @@ In this case you need to expose 8080 to 80 and use the correct image name
121142
## Deployment -- the less simple way but still easy
122143

123144
Using [Kubernetes](https://kubernetes.io/), note that GKE and AWS provide Kubernetes cluster as a Service.
124-

kubernetes/demo-app-ing.yaml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apiVersion: extensions/v1beta1
2+
kind: Ingress
3+
metadata:
4+
name: demo-app
5+
annotations:
6+
#ingress.kubernetes.io/ssl-redirect: "false"
7+
kubernetes.io/ingress.class: "nginx"
8+
spec:
9+
rules:
10+
- http:
11+
paths:
12+
- path: /demo
13+
backend:
14+
serviceName: demo-app
15+
servicePort: 8080
16+

kubernetes/mysql-secret.sh

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
echo -n Please enter mysql root password for upload to k8s secret:
3+
read rootpw
4+
echo
5+
kubectl create secret generic mysql-secret --from-literal=passwd=$rootpw

kubernetes/mysql.yaml

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: remote-mysql
5+
spec:
6+
type: NodePort
7+
ports:
8+
- port: 3306
9+
selector:
10+
app: mysql
11+
---
12+
apiVersion: apps/v1beta1
13+
kind: Deployment
14+
metadata:
15+
name: mysql
16+
spec:
17+
strategy:
18+
type: Recreate
19+
template:
20+
metadata:
21+
labels:
22+
app: mysql
23+
spec:
24+
containers:
25+
- image: mysql:5.6
26+
imagePullPolicy: IfNotPresent
27+
name: mysql
28+
env:
29+
- name: MYSQL_ROOT_PASSWORD
30+
valueFrom:
31+
secretKeyRef:
32+
name: mysql-secret
33+
key: passwd
34+
ports:
35+
- containerPort: 3306
36+
name: mysql

kubernetes/website.yaml

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: demo-app
5+
spec:
6+
type: NodePort
7+
ports:
8+
- port: 8080
9+
selector:
10+
app: demo-app
11+
---
12+
apiVersion: apps/v1beta1
13+
kind: Deployment
14+
metadata:
15+
name: demo-app
16+
spec:
17+
strategy:
18+
type: Recreate
19+
template:
20+
metadata:
21+
labels:
22+
app: demo-app
23+
spec:
24+
containers:
25+
- image: mgckind/my-demo-app:kube-1.0
26+
imagePullPolicy: IfNotPresent
27+
name: demo-app
28+
env:
29+
- name: MYSQL_PASS
30+
valueFrom:
31+
secretKeyRef:
32+
name: mysql-secret
33+
key: passwd
34+
- name: MYSQL_USER
35+
value: "root" ## <----
36+
ports:
37+
- containerPort: 8080
38+
name: demo-app

webpage/main.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def get(self):
9898
add_topic(topic)
9999
if topictxt is not '':
100100
add_topic(topictxt.lower(), new=True)
101-
self.redirect('/')
101+
self.redirect('/demo/')
102102

103103

104104
class Application(tornado.web.Application):
@@ -109,8 +109,9 @@ class Application(tornado.web.Application):
109109
def __init__(self):
110110
handlers = [
111111
(r"/", MainHandler),
112-
(r"/getdata", GetDataHandler),
113-
(r"/add", AddDataHandler),
112+
(r"/demo/?", MainHandler),
113+
(r"/demo/getdata", GetDataHandler),
114+
(r"/demo/add", AddDataHandler),
114115
]
115116
settings = {
116117
"template_path": Settings.TEMPLATE_PATH,

webpage/templates/main.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
function drawChart() {
1010
var jsonData = $.ajax({
11-
url: "/getdata",
11+
url: "/demo/getdata",
1212
dataType: "json",
1313
async: false
1414
}).responseText;
@@ -31,7 +31,7 @@
3131
</b>
3232

3333

34-
<form action="/add">
34+
<form action="/demo/add">
3535
<p>What is my favorite language?:</p>
3636
<div>
3737
<input type="radio" id="contactChoice1"

0 commit comments

Comments
 (0)