Your browser doesn't support the features required by impress.js, so you are presented with a simplified version of this presentation.

For the best experience please use the latest Chrome, Safari or Firefox browser.

 

 

 

The plan

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:

 

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 # download, build container and run
docker images # list the docker images
docker ps # list running containers
docker ps -a # list all containers

 

 

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 # download an image 
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 # search for an image

docker run carlancalazans/static_site # blocked terminal
docker run -d -P carlancalazans/static_site # detached mode
docker ps # running containers
docker rename ID_OR_NAME static_site # rename a container
docker stats static_site # memory, cpu and other useful info
docker inspect static_site # even more info formatted in json

[QUESTION: which container I'm using to serve the site?]
docker top static_site # list running processes
top # list running processes on Linux
docker port static_site # mapped port container and localhost

[DO: Open a browser and access the website]
docker logs static_site # show logs

Hands-on

[DO: Clean up]
docker kill static_site # kill, stop, start, pause, unpause
docker ps

docker rm static_site # remove container
docker ps -a

docker rmi carlancalazans/static_site # remove image
docker images -a

[INFO: Other way]
# -q = get only the list of ids
docker kill $(docker ps -qa) # kill all
docker rm $(docker ps -qa) # remove all containers
docker rmi $(docker images -qa) # remove all images

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?

Hands-on

pwd # check current directory
mkdir my_quotes # create a directory
cd my_quotes # change the current directory
vim quotes.txt # edit or create new file
docker run -d -v PATH_MY_DIR:/usr/src/app/database # absolute path -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:

  1. A directory to store the Dockerfile
  2. site directory to store the index.html
  3. The index.html
  4. A Dockerfile

 The format of the Dockerfile:

# comment 
INSTRUCTION arguments

Dockerfile

FROM # name of the base image from which we are building.
LABEL # add metadata information to an image.
COPY # copy the contents of a folder to another one.
EXPOSE # tell that a container listen on a port at runtime.
WORKDIR # working directory for RUN, CMD, ENTRYPOINT, COPY and ADD.
VOLUME # create a volume with a mount point with the same name.

 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:

  1. The base image with the instruction FROM
  2. The maintainer information with LABEL
  3. The work directory inside the image using WORKDIR
  4. To copy the contents of site to work directory with COPY
  5. The container listens on a port at runtime using EXPOSE

To build the image:

docker build -t my_new_static_website:latest .
# run it
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

 

 

 

 

Cheers!