The plan
- Motivation and concepts
- Docker basics
- Docker file
A short story
Back in time, we used to have 1:1 relation between a server and an application.

If we have 2 applications, we needed 2 servers.


You know where I'm going with this. More applications means more servers.
And with it, networking, backup, operating system installation and configuration.

Virtualization:
A method of divide the system resources between different applications. To use it, we need a virtual machine.
Virtual machine:
Is an emulation of a physical computing environment.

Problem solved! \o/
Right?

Wait! What is the price of running a VM?
Virtual Machines waste a lot of resources. A VM requests for CPU, memory, hard disk, network and other hardware resources, which are managed by a virtualization layer (hypervisor) responsible for translating these requests to the underlying physical hardware.

This is how it works
Vagrant
Vagrant is a virtual machine manager, with it you can easily create, modify and delete virtual machines using the command.
The creation of the virtual machines became faster with Vagrant. It offers a repository with virtual machines pre-installed to be downloaded and used as fast as your internet connection can download it.

Vagrant
But, under the hood, it's still running a virtual machine.

What is docker?
Docker is a piece of software which aims to simplify the process of building, deploying, shipping and running applications in a controllable environment (sandbox).
The sandbox environment is called container and this is really important to know.
The container revolution started when the maintaners of the Linux Kernel added a feature called namespaces.
Namespaces, among other things, can be used to create an isolated container that has no access to objects outside it. This enables multiple isolated executions within a single operating system kernel.
What is docker?
Advantages:
- A container doesn't need to boot the operating system kernel, it's running on the atual operating system kernel.
- The creation of a container is so fast as the instantiation of a class.
- Containers states (or images) are very small compared to VM's, so they are easy to distribute.
What is docker?

Where is the hypervisor?
What is docker?
Docker is technology agnostic, you can use docker with almost any language or framework.
Here, on this presentation I'm going to use for example Java and Python, but I can easily run Ruby, Go, Scala, Smalltalk, Groovy.
Docker containers
A container is a stardard way to ship your code to different environments.
A running instance of an image is also know as a container.
Docker images
An image is the basis of a container, a blueprint to get a container running, it's built to be used in a container.
Also, an image doesn't have a state and it never changes.
Enough of theory.
Let's start the hands-on!
Hands-on
Docker ships with a client which is used to put stuff in a container, load containers in ships, remove containers from ships and so on.
It's how we interact with images and containers.
To access the help:
docker
or
docker -h
Hands-on
The guys from docker built a hello world image to test our docker installation and understand the basics of it.
docker run hello-world
docker images
docker ps
docker ps -a
Hands-on
We saw docker downloading the hello world image, but where the image is stored?

Hands-on
To download images previously before running them
docker pull docker/whalesay
docker run docker/whalesay cowsay PLACE_YOUR_NAME
As you can see, no images were downloaded this time and you also got a whale saying your name as a gift.

Hands-on
I've created an image to show simple static website. While we are running this image, I'll show some other docker commands.
Hummm. I can't remember it's name...

Hands-on
docker search carlancalazans
docker run carlancalazans/static_site
docker run -d -P carlancalazans/static_site
docker ps
docker rename ID_OR_NAME static_site
docker stats static_site
docker inspect static_site
[QUESTION: which container I'm using to serve the site?]
docker top static_site
top
docker port static_site
[DO: Open a browser and access the website]
docker logs static_site
Hands-on
[DO: Clean up]
docker kill static_site
docker ps
docker rm static_site
docker ps -a
docker rmi carlancalazans/static_site
docker images -a
[INFO: Other way]
docker kill $(docker ps -qa)
docker rm $(docker ps -qa)
docker rmi $(docker images -qa)
Hands-on
The intent of the new sample is show some quotes in the middle of the screen. Let's run it.
docker run -d -p 4000:4000 carlancalazans/quotes
Now, we are using -p, which links a local port to a container port.
Test the application on a browser.
Hands-on
How to add more quotes or how to change the quotes presented?
- What we know?
- /usr/src/app/database/quotes.txt
- What we have to do?
- How we are going to do it?
- Creating a directory on our computer
- Create our own quotes.txt
- Create a volume and associate with a directory
Hands-on
pwd
mkdir my_quotes
cd my_quotes
vim quotes.txt
docker run -d
-v PATH_MY_DIR:/usr/src/app/database
-p 4000:4000 carlancalazans/quotes
Did it work?
Dockerfile
A Dockerfile is a special text file, with it we can build images automatically. Inside a Dockerfile we can use a set of commands to shape the image.
It's your time to build the a static website. But first, let's do it without a Dockerfile.
docker run --name my_static_website -d nginx:alpine
docker exec -it my_static_website sh
vi /usr/share/nginx/html/index.html
docker commit -m "[message]" -a "[firstname] [lastname]"
my_static_website [DOCKER_HUB_USER]/my_static_website:latest
Dockerfile
Push you image to Docker hub:
docker login --username=[DOCKER_HUB_USER]
docker push [DOCKER_HUB_USER]/my_static_website
And test it:
docker kill $(docker ps -qa)
docker rm $(docker ps -qa)
docker rmi $(docker images -qa)
docker run -p 4000:80 -d /my_static_website
It was easy?
Dockerfile
Now, we are diving in the Dockerfile to automate the creation of the same image.
We will need:
- A directory to store the Dockerfile
- A site directory to store the index.html
- The index.html
- A Dockerfile
The format of the Dockerfile:
INSTRUCTION arguments
Dockerfile
FROM
LABEL
COPY
EXPOSE
WORKDIR
VOLUME
Our Dockerfile:
FROM nginx:alpine
LABEL maintainer=="[firstname] [lastname] [email]"
WORKDIR /usr/share/nginx/html
COPY site .
EXPOSE 80
Dockerfile
We are saying to Docker:
- The base image with the instruction
FROM
- The maintainer information with
LABEL
- The work directory inside the image using
WORKDIR
- To copy the contents of site to work directory with
COPY
- The container listens on a port at runtime using
EXPOSE
To build the image:
docker build -t my_new_static_website:latest .
docker run -p 4000:80 -d my_new_static_website
Dockerfile
Push to docker hub:
docker tag my_new_static_website
[DOCKER_HUB_USER]/my_new_static_website:latest
docker push [DOCKER_HUB_USER]/my_new_static_website