What is Docker ? – How to use Docker ? – Container VS VM



What is Docker ? – How to use Docker ? – Container VS VM

0 0


docker-introduction-slides


On Github Babacooll / docker-introduction-slides

07/2015

Michaël Garrez

What is Docker ?

Wikipedia Definition

Docker is an open-source project that automates the deployment of applications inside software containers, by providing an additional layer of abstraction and automation of operating-system-level virtualization on Linux.

Linux Containers

Docker is based on LXC (Linux Containers) which allows

it to isolate containers from each other.

 

LXC use mainly two Linux Kernel features to acheave it :

 

  • Namespaces (Isolation of resources)
  • Cgroups (Isolation of resource usage as CPU/RAM)

Docker World

Docker can be split in multiple different parts :

 

  • Docker Engine
  • Docker Hub
  • Docker Company

How to use Docker ?

Docker can be used natively on Linux (and only Linux) by the simple bash command :

wget -qO- https://get.docker.com/ | sh

Docker installation requires at least 3.13 Linux Kernel.

How to use Docker ?

Docker uses boot2docker to create a VM with a Tiny Core Linux version including Docker.

Container VS VM

Container VS Image

Image

An image is an inert snapshot of a container.

 

When runned (with a run command) it becomes a container.

 

As images can be quite heavy they are designed to be composed of layers of other images which allows to minimize an image weight.

 

Images are stored on Docker Hub.

Container VS Image

Image

An image is built from a Dockerfile. A file describing how the image is supposed to behave, what it extends from, ...

 

docker images command lists all local images :

Container VS Image

Container

Programmatically speaking, if an image what a class, then a container would be an instance of this class.

 

You may launch multiple containers for the same image.

docker ps command lists all running containers :

Build our first container

$ docker run -itd ubuntu

To create a container from the official ubuntu image :

To enter into a container with your terminal :

$ docker exec -it CONTAINERID /bin/bash

To check your running containers and their ids :

$ docker ps

Turn our container in an image

As said before : 

 An image is a snapshot of a container

Let's take a snapshot of our container :

$ docker commit -m "First commit" CONTAINERID babacooll/lftmyfirstimage

Build a container

You can now create as many container from this image as you want :

$ docker run -dit babacooll/lftmyfirstimage

-d : Daemonized

-it : Allocate TTY

Image name

Dockerfile

This approach is nice but as developers

we highly prefer working in files than terminals.

 

That's why Docker uses Dockerfiles.

Dockerfile

A Dockerfile translation of our current image would be :

FROM ubuntu
MAINTAINER Michael Garrez <michael.garrez@gmail.com>

ENV REFRESHED_AT 2015-05-11

RUN mkdir -p /var/www

To build an image from this Dockerfile :

$ docker build -t babacooll/lftmysecondimage .

Dockerfile

A Dockerfile translation of our current image would be :

FROM ubuntu
MAINTAINER Michael Garrez <michael.garrez@gmail.com>

ENV REFRESHED_AT 2015-05-11

RUN mkdir -p /var/www

To build an image from this Dockerfile :

$ docker build -t babacooll/lftmysecondimage .

Container

To create a container from our new image :

$ docker run -dit babacooll/lftmysecondimage

-d : Daemonized

-it : Allocate TTY

Image name

Image : Apache + PHP7

Dockerfile

# use the latest Ubuntu base image
# adapted from https://registry.hub.docker.com/u/coderstephen/php7 for additional needs
FROM ubuntu:14.04
MAINTAINER LFT <lft@quanta.lu>

# install packages for Apache and for compiling PHP
RUN apt-get update && apt-get install -y \
    apache2-mpm-prefork \
    apache2-prefork-dev \
    aufs-tools \
    automake \
    bison \
    btrfs-tools \
    build-essential \
    curl \
    git \
    libbz2-dev \
    libcurl4-openssl-dev \
    libmcrypt-dev \
    libxml2-dev \
    re2c

# get the latest PHP source from master branch
RUN git clone --depth=1 https://github.com/php/php-src.git /usr/local/src/php

# we're going to be working out of the PHP src directory for the compile steps
WORKDIR /usr/local/src/php
ENV PHP_DIR /usr/local/php

# configure the build
RUN ./buildconf && ./configure \
    --prefix=$PHP_DIR \
    --with-config-file-path=$PHP_DIR \
    --with-config-file-scan-dir=$PHP_DIR/conf.d \
    --with-apxs2=/usr/bin/apxs2 \
    --with-libdir=/lib/x86_64-linux-gnu \
    --enable-bcmath \
    --with-bz2 \
    --enable-calendar \
    --with-curl \
    --enable-exif \
    --enable-ftp \
    --with-ldap \
    --enable-mbstring \
    --enable-mbregex \
    --with-mcrypt \
    --with-mysqli=mysqlnd \
    --with-openssl \
    --enable-pcntl \
    --without-pear \
    --enable-pdo \
    --with-pdo-mysql=mysqlnd \
    --enable-sockets \
    --with-zip \
    --with-zlib

# compile and install
RUN make && make install

ENV PATH=$PATH:/usr/local/php/bin

# set up Apache environment variables
ENV APACHE_RUN_USER=www-data \
    APACHE_RUN_GROUP=www-data \
    APACHE_LOG_DIR=/var/log/apache2 \
    APACHE_LOCK_DIR=/var/lock/apache2 \
    APACHE_PID_FILE=/var/run/apache2.pid

# Remove default site
RUN rm -f sites-enabled/000-default.conf

# Enable additional configs and mods
ENV HTTPD_PREFIX /etc/apache2
RUN a2dismod mpm_event && a2enmod mpm_prefork
RUN ln -s $HTTPD_PREFIX/mods-available/expires.load $HTTPD_PREFIX/mods-enabled/expires.load \
    && ln -s $HTTPD_PREFIX/mods-available/headers.load $HTTPD_PREFIX/mods-enabled/headers.load \
    && ln -s $HTTPD_PREFIX/mods-available/rewrite.load $HTTPD_PREFIX/mods-enabled/rewrite.load

EXPOSE 80

# By default, simply start apache.
CMD /usr/sbin/apache2ctl -D FOREGROUND

Build the image

$ docker build -t babacooll/lftapachephp7 .

Image name

Dockerfile directory

Run a Container

$ docker run -dit babacooll/lftapachephp7

-d : Daemonized

-it : Allocate TTY

Image name

Let's check Apache

$ boot2docker ip

To know the IP of Docker Host :

Open ports

To open a port on the Container :

$ docker run -dit -p 80:80 babacooll/lftapachephp7

How to add PHP code ?

Solutions ?

  • Open Container and work in it ?

 

  • Open Container in FTP ?

How to add PHP code ?

How to add PHP code ?

Let's build a new Image babacooll/lftapachephp7project 

extending babacooll/lftapachephp7

Dockerfile

FROM babacooll/lftapachephp7
MAINTAINER LFT <lft@quanta.lu>

ENV REFRESHED_AT 2015-05-11

COPY vhost.conf /etc/apache2/sites-enabled/

RUN mkdir -p /var/www/lft
VOLUME ["/var/www/lft"]

COPY application/ /var/www/lft

WORKDIR /var/www/lft
EXPOSE 80
CMD ["apache2ctl", "-D", "FOREGROUND"]

How to add PHP code ?

Vhost

<VirtualHost *:80>
  ServerName lft.dev
  DocumentRoot /var/www/lft
  #RewriteEngine On
  DirectoryIndex index.php

  <Directory /var/www/lft>
    Options FollowSymLinks
    AllowOverride All
    Require all granted
  </Directory>

  LogLevel info
  ErrorLog /var/log/apache2/myapp-error.log
  CustomLog /var/log/apache2/myapp-access.log combined

</VirtualHost>

<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>

How to add PHP code ?

Let's build our image :

$ docker build -t lft/lftapachephp7project .

Let's run a container from this image :

$ docker run -dit -p 80:80 babacooll/lftapachephp7project

How to add PHP code ?

Add lft.dev to local hosts

 

http://lft.dev:80

How to add PHP code ?

Our image contains now :

 

  • Our Project (./application)
  • An Apache vhost (vhost.conf)
  • Apache + PHP7 (babacooll/lftapachephp7)

How to add PHP code ?

How to add PHP code ?

Now let's change our index.php file

to display phpinfo() :

<?php

phpinfo();

How to add PHP code ?

How to add PHP code ?

As babacooll/lftapachephp7project is an inert image of Apache + PHP7 + Our project, a modification in my local files CAN'T affect this image.

 

As our container is a living babacooll/lftapachephp7project,

a modification in my local files CAN'T affect this container.

How to add PHP code ?

A solution would be to update files directly in the container but it would only update the ​current container, not all containers related to 

babacooll/lftapachephp7project.​

How to add PHP code ?

Another (better) solution is to mount dynamically 

a volume in the container :

$ docker run \
-dit \
-p 80:80 \
-v /Users/michaelgarrez/Docker/LFT/ApachePHP7Project/application/:/var/www/lft \
babacooll/lftapachephp7project

Local folder to mount from

Container folder to mount on

Image : MariaDB

Docker Hub

Let's use the official mariadb repository

$ docker pull mariadb

MariaDB Container

To run a Container with MariaDB :

$ docker run \
--name mymariadb \
-e MYSQL_ROOT_PASSWORD=mypassword \
-e MYSQL_DATABASE=lft \
-d \
mariadb

MariaDB Container

That's all for the MariaDB container !

Link Containers

Let's link our application to MariaDB !

<?php

try {
    $dbh = new PDO('mysql:host=mariadbcontainer;dbname=lft', 'root', 'mypassword');
} catch (PDOException $e) {
    print "Erreur !: " . $e->getMessage() . "<br/>";
    die();
}

Link Containers

Our PHP container has no knowledge of the MariaDB container.

 

Let's fix that !

$ docker run \
-dit \
-p 80:80 \
--link mymariadb:mariadbcontainer \
-v /Users/michaelgarrez/Docker/LFT/ApachePHP7Project/application/:/var/www/lft \
babacooll/lftapachephp7project

Docker Hub

Docker Hub contains a lot of images for various purposes.

 

Docker Hub provides a free storage facility for your public images and a paying storage facility for your private images (GitHub style).

Push on Docker Hub

$ docker push babacooll/lftapachephp7project

Pull on Docker Hub

$ docker pull babacooll/lftapachephp7project

Automated Builds

Docker Hub allows you to link a GitHub repository to a Docker Hub repository.

 

Your GitHub repository should contain a Dockerfile on the root directory.

 

Each time you push on GitHub, Docker Hub (through webhooks) will pull your repository and build an image into your Docker Hub repository.

Orchestration

Running all containers by hand can

be really messy and complicated.

 

But Docker has a solution for us :

 

Docker Compose

Orchestration

For the moment our simple application requires us to do those commands :

$ docker run \
--name mymariadb \
-e MYSQL_ROOT_PASSWORD=mypassword \
-e MYSQL_DATABASE=lft \
-d \
mariadb

$ docker run \
-dit \
-p 80:80 \
--link mymariadb:mariadbcontainer \
-v /Users/michaelgarrez/Docker/LFT/ApachePHP7Project/application/:/var/www/lft \
babacooll/lftapachephp7project

Orchestration

Solutions ?

 

  • Put those commands into a README ?
  • Put those commands into a bash script ?

 

No.

 

The solution is Docker compose !

Orchestration

Docker compose works by putting a single yaml file in your project.

 

For example this is the corresponding docker-compose.yml file for our project :

web:
  image: babacooll/lftapachephp7project
  ports:
    - "80:80"
  volumes:
    - /Users/michaelgarrez/Docker/LFT/ApachePHP7Project/application/:/var/www/lft
  links:
    - mariadb:mariadbcontainer

mariadb:
    image: mariadb
    environment:
        MYSQL_ROOT_PASSWORD: mypassword
        MYSQL_DATABASE: lft

Orchestration

Our project can now be started by a simple command :

$ docker-compose up -d

Scalability

Docker compose allows you to multiply a container by using the scale command :

$ docker-compose scale mariadb=5

To allow links to properly function you have to stop and remove your web container and relaunch it :

$ docker-compose stop web
$ docker-compose rm web
$ docker-compose run -d web