On Github Babacooll / docker-introduction-slides
07/2015
Michaël Garrez
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.
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 :
Docker can be split in multiple different parts :
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.
Docker uses boot2docker to create a VM with a Tiny Core Linux version including Docker.
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.
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
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 :
$ 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
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
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
This approach is nice but as developers
we highly prefer working in files than terminals.
That's why Docker uses Dockerfiles.
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 .
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 .
To create a container from our new image :
$ docker run -dit babacooll/lftmysecondimage
-d : Daemonized
-it : Allocate TTY
Image name
# 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
$ docker build -t babacooll/lftapachephp7 .
Image name
Dockerfile directory
$ docker run -dit babacooll/lftapachephp7
-d : Daemonized
-it : Allocate TTY
Image name
$ boot2docker ip
To know the IP of Docker Host :
To open a port on the Container :
$ docker run -dit -p 80:80 babacooll/lftapachephp7
Solutions ?
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"]
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>
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
Our image contains now :
Now let's change our index.php file
to display phpinfo() :
<?php phpinfo();
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.
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.
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
Let's use the official mariadb repository
$ docker pull mariadb
To run a Container with MariaDB :
$ docker run \ --name mymariadb \ -e MYSQL_ROOT_PASSWORD=mypassword \ -e MYSQL_DATABASE=lft \ -d \ mariadb
That's all for the MariaDB container !
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(); }
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 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).
$ docker push babacooll/lftapachephp7project
$ docker pull babacooll/lftapachephp7project
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.
Running all containers by hand can
be really messy and complicated.
But Docker has a solution for us :
Docker Compose
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
Solutions ?
No.
The solution is Docker compose !
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
Our project can now be started by a simple command :
$ docker-compose up -d
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