Introduction to Docker by Alex Collins

Overview

  1. What is Docker and why should I care?
  2. Hands on running your first container.
  3. Hands on contain your own app.

What is Docker?

For isolating an application from others by putting it into a container.

A container can contain anything (Java Web App, Wordpress, a database).

A container can be saved as an image, which can be shared.

"Like VirtualBox then?"

Same same, but different.

Why should I care?

Fast to Get Set-up

Want to start MySQL? Easy - just use a container someone else has made first.

Enables a Dev/Ops Contract

"If you can contain it, we can deploy it."

Extremely light weight.

Broken container? No problem!

Kill it and start a new one from the image.

Each Container Isolated

Fewer dependency problems.

Same container everywhere

On the devs machine, on the QA's machine, and in production.

Limitations

"As a rule of thumb, if you ever read an article citing scientific evidence that doesn’t mention its limitations, the author has not presented the full story."

Linux only. No native support on Mac or Windows.

Free, but not OSS: commercial.

Fresh and not quite proven.

Tooling Jungle.

Many tools for every task, but how do you know you've choosen the best?

Hands On #1:
First Steps

Install the Dependencies

brew install boot2docker docker

Interlude #1:
What is Boot2Docker

Start-up Daemon

boot2docker init
boot2docker up

Find An Image

docker search mysql

Start A MySQL Container

docker run  -P -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql

Find the started container in the Docker process list

docker ps

Interlude #2:
Port-forwarding

VBoxManage controlvm boot2docker-vm natpf1 "3306,tcp,127.0.0.1,3306,,3306"

Test Container

brew install mysql
mysql --protocol TCP -u root -p
create database mydb;
connect mydb;
create table foo(bar int);

Or use an SQL client you like.

Stop Container

docker ps
docker stop ...

Oneliner to stop all containers:

docker stop $(docker ps -q)

Building You Own Container

Rather than Vagrantfile, we have Dockerfile:

FROM centos
    
ADD somefile.png .

EXPOSE 80

CMD ["echo", "Hello Docker"]

Hands-On #2:
Contain Something!

Create new directory with this Dockerfile:

FROM nginix

Build it:

docker build -t myapp .

Run it:

docker run -P -p 8080:80 myapp
VBoxManage controlvm boot2docker-vm natpf1 "8080,tcp,127.0.0.1,8080,,8080"

Open http://localhost:8080 in your browser to sees your work.

Get an image and save it.

Create index.html:


I love Docker!

Add to Dockerfile:

ADD myimage.png /usr/share/nginx/html/
ADD index.html /usr/share/nginx/html/

Kill the running container, rebuild and run again.

Hands On #3:
Immutability

docker run  -P -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql

mysql --protocol TCP -u root -p
create database mydb;
docker stop $(docker ps -q);

Volumes to the Rescue!

docker run -v /var/lib/mysql --name data busybox
docker run -P -p 3306:3306 -volumes-from data -e MYSQL_ROOT_PASSWORD=123456 mysql

mysql --protocol TCP -u root -p
create database mydb;

docker stop $(docker ps -q)

Other Things To Learn

  • Fig
  • Testing Containers
  • Docker API
  • Registries

Fin