Think Inside The Box

#boscc

Gleb Bahmutov, PhD

Boston Code Camp 2018

About: @bahmutov

  • Startups / medium size / MathWorks
  • VP of Eng at Cypress.io

"Testing, the way it should be"

  • Computer vision background

About: @bahmutov

It works on my machine ...

I use Mac to

develop locally

Our servers

are usually linux boxes on AWS

Our users

can have anything

Shipping industry

  1. Fill standard box

  2. Ship the box

  3. Open the box

IT Operations

  1. Build a standard image

  2. Distribute the image

  3. Run the image

This presentation

  1. Using Docker

  2. Instant deploys with Zeit Now

  3. Your own Heroku with Dokku

Kubernetes?!

Going from 0 to 60 with Kubernetes in an Hour

04:30 PM in Jay Peak

Bill Pratt @dev_enginerd

How to Build a Container

02:00 PM Bretton Woods

Laura Stone

Using Docker

This program runs on Ubuntu 12.04

This program runs on Node 8.9

This program requires GunDB (?!)

Find Ubuntu Image

Find Specific Ubuntu Version

Run Ubuntu Image

$ docker run -it ubuntu:12.04
Unable to find image 'ubuntu:12.04' locally
12.04: Pulling from library/ubuntu
d8868e50ac4c: Pull complete 
83251ac64627: Pull complete 
589bba2f1b36: Pull complete 
d62ecaceda39: Pull complete 
6d93b41cfc6b: Pull complete 
Digest: sha256:18305429af...
Status: Downloaded newer image for ubuntu:12.04
root@58776820952d:/# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=12.04
DISTRIB_CODENAME=precise
DISTRIB_DESCRIPTION="Ubuntu 12.04.5 LTS"

Run Ubuntu Image

$ docker run -it ubuntu:12.04
--interactive, -i = session
--tty, -t = start terminal
$ docker run <options> <image name:tag>
$ docker run -it ubuntu:12.04
root@8b9edea5134c:/# echo $SHELL
/bin/bash

Docker Hub

(many, many)

images

Local Docker

copied image

container

docker run

Docker Engine

$ docker ps
CONTAINER ID  IMAGE         COMMAND      NAMES
8b9edea5134c  ubuntu:12.04  "/bin/bash"  upbeat_jackson
$ docker run -it --name my-ub ubuntu:12.04
$ docker ps
CONTAINER ID  IMAGE         COMMAND      NAMES
79b05f517c9a  ubuntu:12.04  "/bin/bash"  my-ub
$ docker stop 79b
79b

Stopping a container

$ docker images
REPOSITORY  TAG    IMAGE ID      CREATED       SIZE
ubuntu      12.04  5b117edd0b76  7 months ago  104MB
$ docker rmi ubuntu:12.04
Error response from daemon: conflict: 
unable to remove repository reference 
"ubuntu:12.04" (must force) - container b0ffbace152d 
is using its referenced image 5b117edd0b76

Need to delete all containers (even stopped) that reference the image "ubuntu:12.04"

Removing an image

CONTAINER ID        IMAGE               STATUS                      NAMES
79b05f517c9a        ubuntu:12.04        Exited (137) 6 minutes ago  my-ub
8b9edea5134c        ubuntu:12.04        Exited (0) 10 minutes ago   upbeat_jackson
3246d0e8230a        ubuntu:12.04        Exited (0) 22 minutes ago   cranky_benz
b0ffbace152d        ubuntu:12.04        Exited (127) 24 minutes ago thirsty_kalam
58776820952d        ubuntu:12.04        Exited (0) 24 minutes ago   nostalgic_bardeen
$ docker ps -a --filter ancestor=ubuntu:12.04
$ docker rm 79b05f517c9a 8b9edea5134c ...
$ docker rm 79b 8b9 ...
$ docker rm $(docker ps -a --filter ancestor=ubuntu:12.04 -q)
58776820952d
b0ffbace152d
...

Removing found images

Run Your Code in Node 8.9

$ ls .
index.js	package.json
$ cat index.js 
console.log('node version', process.version)
$ docker run -v $PWD:/src -w /src -it node:8.9 /bin/bash
root@513ed6d3776d:/src# ls
index.js  package.json
root@513ed6d3776d:/src# node index.js 
node version v8.9.1
-v host:container = attach volume
-w path = working folder

Test Your Code in Node 8.9 on CI

docker:
  - image: node:8.9

CircleCI

Codeship

GitLabCI

...

Run Against GunDB

$ docker run -p 8080:8080 gundb/gun
Unable to find image 'gundb/gun:latest' locally
latest: Pulling from gundb/gun
...
Server started on port 8080 with /gun

Then open localhost:8080

-p host_port:container_port = map port

Run Against GunDB

Build My Image

FROM gundb/gun:0.8

Dockerfile

$ docker build -t gleb/my-gun:0.8 .
Sending build context to Docker daemon  2.048kB
Step 1/1 : FROM gundb/gun:0.8
 ---> c88081efafb3
Successfully built c88081efafb3
Successfully tagged gleb/my-gun:0.8
--tag, -t <name> = tag image

Personal Docker cheatsheet

Run My Image

$ docker run -it -p 9000:8080 gleb/my-gun:0.8

Then open localhost:9000

Dockerfile: layers

FROM mhart/alpine-node:8

WORKDIR /app
COPY . .

RUN npm install
RUN npm run lint
RUN npm test

EXPOSE 1337
CMD ["node", "index.js"]

docker history <image name>

0dc9a45a3171

8b66fb0060f5

79a9d606ae3c

300beaa8696b

a17394505502

e6ebb0e80ecb

343874389450

2777ecbd35ff

Make smaller image

$ docker images gleb/hello-world
REPOSITORY        TAG         IMAGE ID      SIZE
gleb/hello-world  bare        78cdddcd77ac  37.9MB
gleb/hello-world  multi-stage 44dcd42b11f4  49.4MB
gleb/hello-world  simple      aed28e2bde45  202MB

Security

  • Make separate user

  • Whitelist what process can do

  • Firewall container from system

Deploy My Image

$ ls
Dockerfile
$ cat Dockerfile 
FROM gundb/gun:0.8
$ now
> Deploying ~/git/training-github/docker/gun-test under bahmutov
> Ready! https://gun-test-dlusfciqnx.now.sh (copied to clipboard) [3s]
> Synced 1 file (19B) [0ms] 
> Initializing…
> Building
> ▲ docker build
...
> Successfully built c88081efafb3
> ▲ Storing image
> ▲ Deploying image
> ▲ Container started

with Zeit.co Now tool

$ time now
...
real	0m18.863s

Zeit Now ▲

  1. Every deploy gives new container

  2. Deploy static sites, Node, Docker

  3. DNS, envs, etc!

 

I Need More Control

Multiple Apps

Microservices

Databases

Etc.

Dokku: Smallest PaaS


 $ wget https://raw.githubusercontent.com/dokku/dokku/v0.10.5/bootstrap.sh
 $ sudo DOKKU_TAG=v0.10.5 bash bootstrap.sh

Follow Instructions

root@gleb-demos:~# dokku apps:create my-gun
Creating my-gun... done
$ git push dokku master
Counting objects: 3, done.
Writing objects: 100% (3/3), 236 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
-----> Cleaning up...
-----> Building my-gun from dockerfile...
remote: build context to Docker daemon  2.048kB
Step 1/1 : FROM gundb/gun:0.8
...
=====> Application deployed:
       http://my-gun.gleb-demos.com
       http://my-gun.gleb-demos.com:8080
$ git remote add dokku dokku@gleb-demos.com:my-gun

Dokku

Isolated Services

Lets Encrypt

Various Plugins

Cheap!

Several Apps Using Common Database + Volume

Deploy = git push

Learn Docker and k8s at www.katacoda.com

Use images from Docker Hub

Main Ideas 1/2

Main Ideas 2/2

Build your own images

Deploy using Zeit Now or Dokku

Cattle,

not pets

If server is not working, fix <code, Dockerfile, environment variables> and deploy new server

Thank you

#boscc