Docker

Docker

A worthwhale tool to learn

Roadmap

Part 1 Intro to Docker / Containers
Part 2 Getting Started with Docker
Part 3 Cool Docker Stuff
Part 4 Spilling Company Secrets
Cute Docker Whale with a Gopher

1. What is Docker?

Containers

What is a Container?

A standardized unit of software
VM Architecture
Container Architecture

Analogy

Installing an application on a VM is like manually setting up a tent
Installing an application with Docker is like a popup tent
We'll ignore taking down the tents for the purposes of this analogy

Explanation Part 1; Manual Tent

Imagine the ground is a nice clean VM that we just made
We gotta set up the tent poles (requirements) and the fabric (the application)
Putting tent poles into tent fabric is hard and might take multiple people
If the poles break / need upgrading it's hard to do

Explanation Part 2; Popup Tent

Imagine the ground is (again) a nice clean VM that we just made
We have to install 1 (usually) thing on it; Docker (no analogy here sorry)
The tent is in its bag (Docker image) ready to be opened
We open the bag and the popup tent sets itself up for us (Docker container)

Close Enough

2. Let's talk about Docker

Docker Harbour with Friends

One more mention of tents

Back in the dumb tent analogy I mentioned Images and Containers
Images are a built application (or whatever) ready to be run by Docker
Containers are running instances of Images
I'm now done talking about tents

Let's Install Docker

For Windows / Mac Users;

Install Guide

For (most) Linux Users;

curl -sSL https://get.docker.com/ | sh
For Arch users; it's in the community repo so just install it with whatever it is you use

Even Docker isn't safe from "Hello, World!"

Once you have it installed;

docker run hello-world

should contain the following output;


                            Hello from Docker!
                            This message shows that your installation appears to be working correctly.
                        

2.5 More on Images

In the output of the last command I gave you, you might have seen something like this;


                            Unable to find image 'hello-world:latest' locally
                            latest: Pulling from library/hello-world
                        

This is Docker saying
"I don't have the 'hello-world' image on this machine, let me go get it"

Where does it get images?

If only there was some kind of website where people could upload Docker images for various widely used applications...

The Docker Hub!

Docker Hub

2.8 Building our own images

"So the Docker Hub is great but it doesn't have Docker images for my stuff"
Of course it doesn't, it's on you to make images for your stuff
How? Let me show you!

Example: techtalks-flask

I have a nice website at freyamade.netsoc.co
I want to make a standalone version using flask
I want to use Docker to deploy it
I guarantee nobody has made one for me since it's a new project
Follow Along

Step 1: Dockerfile

First thing's first, we need a Dockerfile
This is a set of instructions for Docker to follow to build my image
We need to copy the code into the image, install requirements, and set the command that the image will run

Step 1: Dockerfile


                            # Base image is the python 3.7 docker image
                            FROM python:3.7

                            # Set the working directory inside the image
                            WORKDIR /home/freyamade/public_html

                            # Copy from the build context to the image
                            COPY . .

                            # Install requirements
                            RUN pip install -r requirements.txt

                            # Load the latest submodules
                            RUN git submodule init
                            RUN git submodule update --remote

                            # Expose port 5000
                            EXPOSE 5000

                            # Set the entrypoint (CMD also works here)
                            ENTRYPOINT python3.7 app.py
                        

Step 2: Building the image

Now that we have a Dockerfile, we can build our own image
In our project directory we can run the following;
docker build -t techtalks-flask .

Step 3: Running the image

After building our image, we can verify it is there using;
docker images

To then run the image we need to do the following;
docker run -p 5000:5000 -d techtalks-flask

Step 4: Test

To test, go to localhost:5000 and ensure the page loads
Last time I checked, it works, so if it doesn't for you it's your fault >.>
Also you can use the following command to list all running containers;

                                # show running containers
                                docker ps

                                # show all containers, dead or alive
                                docker ps -a
                            

0.2 Some other cool stuff

Before I spill some company secrets, I want to share a couple of other cool things with you

-v

Run this from the project directory;
docker run -p 5000:5000 -v $(pwd):/home/freyamade/public_html -d freyamade

Now edit templates/index.html, reload the page and you'll see it update in real time!
This is handy if you have your application Dockerised already and want to do development work

-v + web development

Working on a new web project but don't want to install Apache on your machine to test?

                                # Run from inside project directory, then go to localhost:8080
                                docker run -v $(pwd):/usr/local/apache2/htdocs/ -p 8080:80 -d httpd
                            

Compose

What if you have an application with a lot of required external services (sql, load balancer, etc)?
You could set up all the containers yourself manually...
Or you could use Docker Compose (installed separately)

techtalks-flask + Compose

Let's say I add some reason to use redis in the techtalks-flask project.

I could have the following compose file;


                            version: '3'
                            services:
                              web:
                                image: "techtalks-flask"
                                ports:
                                 - "5000:5000"
                              redis:
                                image: "redis:alpine"
                        

And then bring up both images using;

docker-compose up -d

Kubernetes

Kubernetes (k8s) is an open-source system for automating deployment, scaling, and management of containerized applications.

Docker DOOM

3. Let's spill some trade secrets

Docker Harbour with Friends

CIX Software

CIX SaaS Diagram
CIX old architecture
CIX new architecture

Why is the second one better?

Resiliency If one server goes down, we don't lose the API
Load Balancing We now have multiple servers all able to handle requests instead of one
Fragmentation If all the servers go down, we lose a single application, not all of them*

Speed Increase

We needed to test that our system actually made an improvement.
List 100 Users from Py2 Live and Py3 Stage APIs one million times, calculate average time locally.
Speed increase from Python2 to Python3; 57x

Speed Increase (cont'd)

Speed increase mostly due to architectural changes.
Original API running as monolith on single server.
New API running as clusters of smaller servers with load balancing.

Old Deployment System

Gitlab CI automates deployment when code is pushed to specific branches.
Old method of deployment; fabric
SSH onto appropriate server, git fetch origin, and git reset --hard <branch>
Most of the time, deployment scripts did not contain a job to update requirements
New Deployment System
Deployment still automated but handled differently
Gitlab CI triggers Docker builds from specific branches
After the image(s) are built, we deploy them to the appropriate cluster(s)
Rolling release on the cluster; new images brought up before bringing down old ones

I've Dockerised my Application...

How and where do I deploy it?
Keep your eyes on this project for Netsoc's own CaaS platform
Other cloud providers support containers but as far as I can tell, there are no free trials
If you're really anxious to deploy something, ask the lovely NetSoc SysAdmins where Windlass is c:

Conclusions

Containers vs VMs
T E N T S
Installation
Docker Hub
How to build your image
Docker Compose

Conclusions (cont'd)

Kubernetes kinda
Company secrets that my boss let me share
Another cool Netsoc project on the way
What are your questions?

                        FROM docker-talk:audience
                        RUN answer-questions.sh
                    

Thank you!


Slides:

freyamade.netsoc.co/techtalks/docker

Code:

freyamade/netsoc-docker-talk

Example:

freyamade/techtalks-flask