<?xml version="1.0" encoding="UTF-8"?>
<chapter annotations="slide" version="5.1" xml:id="sdiDocker_chap"
         xmlns="http://docbook.org/ns/docbook"
         xmlns:xlink="http://www.w3.org/1999/xlink"
         xmlns:xila="http://www.w3.org/2001/XInclude/local-attributes"
         xmlns:xi="http://www.w3.org/2001/XInclude"
         xmlns:trans="http://docbook.org/ns/transclusion"
         xmlns:svg="http://www.w3.org/2000/svg"
         xmlns:m="http://www.w3.org/1998/Math/MathML"
         xmlns:html="http://www.w3.org/1999/xhtml"
         xmlns:db="http://docbook.org/ns/docbook">
  <title>Docker</title>

  <section xml:id="sdiDocker_sect_introduction">
    <title>Introduction</title>

    <figure xml:id="sdiDocker_figInstall">
      <title>Installation</title>

      <itemizedlist>
        <listitem>
          <para><uri
          xlink:href="https://docs.docker.com/install">https://docs.docker.com/install</uri></para>
        </listitem>

        <listitem>
          <para><link
          xlink:href="https://github.com/wsargent/docker-cheat-sheet">Docker
          Cheat Sheet</link></para>
        </listitem>
      </itemizedlist>
    </figure>

    <figure xml:id="sdiDocker_Hub">
      <title>Docker hub</title>

      <para>Repository hosting publicly available Docker images:</para>

      <para><uri
      xlink:href="https://hub.docker.com/explore">https://hub.docker.com/explore</uri></para>
    </figure>

    <section xml:id="sdiDocker_manageImages_sect">
      <title>Managing images</title>

      <figure xml:id="sdiDocker_figSearchImages">
        <title>Search images</title>

        <screen>&gt; <link
            xlink:href="https://docs.docker.com/engine/reference/commandline/search">docker search</link> nextcloud
NAME                       DESCRIPTION      <link
            xlink:href="https://docs.docker.com/docker-hub/repos/#stars">STARS</link>  <link
            xlink:href="https://docs.docker.com/docker-hub/official_repos">OFFICIAL</link>   <link
            xlink:href="https://docs.docker.com/docker-cloud/builds/automated-build">AUTOMATED</link>
nextcloud                  A safe home …    424    [OK]
linuxserver/nextcloud      A Nextcloud …    56
greyltc/nextcloud          Nextcloud: …     34                [OK]
wonderfall/nextcloud       All-in-one …     27                [OK]
rootlogin/nextcloud        Nextcloud …      17                [OK]
lsioarmhf/nextcloud        ARMHF based …    8
ownyourbits/nextcloudpi    NextCloud ARM …  7
...</screen>
      </figure>

      <figure xml:id="sdiDocker_figPullImage">
        <title>Pull image using <xref linkend="glo_CLI"/></title>

        <screen>&gt; <link
            xlink:href="https://docs.docker.com/engine/reference/commandline/image_pull">docker image pull</link> alpine
Using default tag: latest
latest: Pulling from library/alpine
<emphasis role="red">ff3a5c916c92</emphasis>: Pull complete
Digest: sha256:7df6db5aa61ae9480f52f0b3a06a140ab98d427f86d8d5de0bedab9b8df6b1c0
Status: Downloaded newer image for alpine:latest</screen>
      </figure>

      <figure xml:id="sdiDocker_figPullNextcloudImage">
        <title>Pull <productname>Nextcloud</productname> image</title>

        <screen>&gt; <link
            xlink:href="https://docs.docker.com/engine/reference/commandline/pull">docker image pull</link> <emphasis
            role="bold">wonderfall/nextcloud</emphasis> <co
            linkends="sdiDocker_figPullNextcloudImage-1"
            xml:id="sdiDocker_figPullNextcloudImage-1-co"/>
Using default tag: latest <co linkends="sdiDocker_figPullNextcloudImage-2"
            xml:id="sdiDocker_figPullNextcloudImage-2-co"/>
latest: Pulling from wonderfall/nextcloud
<emphasis role="red">ff3a5c916c92</emphasis>: Already exists <co
            linkends="sdiDocker_figPullNextcloudImage-3"
            xml:id="sdiDocker_figPullNextcloudImage-3-co"/>
a542d4c3cffb: Pull complete <co linkends="sdiDocker_figPullNextcloudImage-4"
            xml:id="sdiDocker_figPullNextcloudImage-4-co"/>
83001cc0bea0: Pull complete
41a33c66b2c1: Pull complete
a70bf67726f3: Pull complete
fea90b3d29ac: Pull complete
Digest: sha256:472c5c7...2ca20cca45 <co
            linkends="sdiDocker_figPullNextcloudImage-5"
            xml:id="sdiDocker_figPullNextcloudImage-5-co"/>
Status: Downloaded newer image for wonderfall/nextcloud:latest</screen>
      </figure>

      <calloutlist>
        <callout arearefs="sdiDocker_figPullNextcloudImage-1-co"
                 xml:id="sdiDocker_figPullNextcloudImage-1">
          <para>Get docker image from remote server.</para>
        </callout>

        <callout arearefs="sdiDocker_figPullNextcloudImage-2-co"
                 xml:id="sdiDocker_figPullNextcloudImage-2">
          <para>Version defaults to «latest»: Subject to be overridden.</para>
        </callout>

        <callout arearefs="sdiDocker_figPullNextcloudImage-3-co"
                 xml:id="sdiDocker_figPullNextcloudImage-3">
          <para>Reference to existing <link
          linkend="sdiDocker_figPullImage">«alpine» layer</link>.</para>
        </callout>

        <callout arearefs="sdiDocker_figPullNextcloudImage-4-co"
                 xml:id="sdiDocker_figPullNextcloudImage-4">
          <para>Layer downloads.</para>
        </callout>

        <callout arearefs="sdiDocker_figPullNextcloudImage-5-co"
                 xml:id="sdiDocker_figPullNextcloudImage-5">
          <para>Hash value allowing for image verification.</para>
        </callout>
      </calloutlist>

      <figure xml:id="sdiDocker_figNextcloudFromAlpine">
        <title><productname>Nextcloud</productname> based on
        <productname>Alpine</productname></title>

        <mediaobject>
          <imageobject>
            <imagedata fileref="Ref/Docker/layer.multi.svg"/>
          </imageobject>
        </mediaobject>
      </figure>

      <figure xml:id="sdiDocker_inspectImage_fig">
        <title><link
        xlink:href="http://www.googlinux.com/list-all-tags-of-docker-image/index.html">Inspect
        image</link></title>

        <screen>&gt; docker image inspect dac77467ddcc
[
    {
        "Id": "sha256:dac77467ddccc2287d99558245bd34707...",
        "RepoTags": [
            "nextcloud:13.0.4"
        ],
        "RepoDigests": [
            "nextcloud@sha256:fb3e77f19b21364925e8f02..."
        ],
 ...</screen>
      </figure>

      <figure xml:id="sdiDocker_figSearchImageTags">
        <title><link
        xlink:href="http://www.googlinux.com/list-all-tags-of-docker-image/index.html">Search
        an image's tags</link></title>

        <screen>&gt; curl 'https://registry.hub.docker.com/v2/repositories/library/<emphasis
            role="red">nextcloud</emphasis>/tags/'|\
      jq '."results"[]["name"]'  <emphasis role="red"># requires «aptitude install curl jq»</emphasis>
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 11856    0 11856    0     0  11856      0 --:--:-- --:--:-- --:--:-- 20371
"fpm"
"production"
"stable"
"latest"
"13"
"13.0"
"13.0.2"
"production-apache"
"stable-apache"
"apache"</screen>
      </figure>

      <figure xml:id="sdiDocker_figNextcloudImageByVersion">
        <title><productname>Nextcloud</productname> image by version</title>

        <screen>&gt; <link
            xlink:href="https://docs.docker.com/engine/reference/commandline/pull">docker image pull</link> nextcloud:13.0.4
13.0.4: Pulling from library/nextcloud
3d77ce4481b1: Pull complete
32bfdb6043a8: Pull complete
028453741593: Pull complete
f93d7bd342a3: Pull complete
4a2fac611953: Pull complete
87fdfc7d0f94: Pull complete
Digest: sha256:<emphasis role="red">fb3e77f19b21364925e8f02d6e5ad3a1</emphasis> ...
Status: Downloaded newer image for <emphasis role="red">nextcloud:13.0.4</emphasis></screen>
      </figure>

      <figure xml:id="sdiDocker_figListImages">
        <title>List images by <xref linkend="glo_CLI"/></title>

        <screen>&gt; <link
            xlink:href="https://docs.docker.com/engine/reference/commandline/image">docker image ls</link>
REPOSITORY            TAG      IMAGE ID       CREATED        SIZE
nextcloud             13.0.4   dac77467ddcc   3 days ago     544MB
wonderfall/nextcloud  latest   57e1fb51b334   2 months ago   328MB
alpine                latest   3fd9065eaf02   4 months ago   4.15MB</screen>
      </figure>

      <figure xml:id="sdiDocker_figNextcloudImageLatest">
        <title><productname>Nextcloud</productname> latest image</title>

        <screen>&gt; <link
            xlink:href="https://docs.docker.com/engine/reference/commandline/pull">docker image pull</link> nextcloud
Using default tag: latest
latest: Pulling from library/nextcloud <co
            linkends="sdiDocker_figNextcloudImageByVersion-1"
            xml:id="sdiDocker_figNextcloudImageByVersion-1-co"/>
Digest: sha256:<emphasis role="red">fb3e77f19b21364925e8f02d6e5ad3a1</emphasis> ... <co
            linkends="sdiDocker_figNextcloudImageByVersion-2"
            xml:id="sdiDocker_figNextcloudImageByVersion-2-co"/>
Status: Downloaded newer image for <emphasis role="red">nextcloud:latest</emphasis></screen>
      </figure>

      <calloutlist>
        <callout arearefs="sdiDocker_figNextcloudImageByVersion-1-co"
                 xml:id="sdiDocker_figNextcloudImageByVersion-1">
          <para>No «<link
          xlink:href="https://docs.docker.com/engine/reference/commandline/pull/#examples">Pull
          complete</link>» messages, image is alias of
          <code>nextcloud:13.0.4</code>.</para>
        </callout>

        <callout arearefs="sdiDocker_figNextcloudImageByVersion-2-co"
                 xml:id="sdiDocker_figNextcloudImageByVersion-2">
          <para>Digest value identical to <xref
          linkend="sdiDocker_figNextcloudImageByVersion"/>.</para>
        </callout>
      </calloutlist>

      <figure xml:id="sdiDocker_figListNextcloudImages">
        <title>Duplicate <productname>Nextcloud</productname> images</title>

        <screen>&gt; docker image ls 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nextcloud           13.0.4              <emphasis role="red">dac77467ddcc</emphasis>        4 days ago          <emphasis
            role="red">544MB</emphasis>
nextcloud           latest              <emphasis role="red">dac77467ddcc</emphasis>        4 days ago          <emphasis
            role="red">544MB</emphasis>
...</screen>
      </figure>

      <figure xml:id="sdiDocker_figCompareMavenArtifacts">
        <title>Maven ringing a bell?</title>

        <programlisting language="xml">&lt;dependency&gt;
  &lt;groupId&gt;<emphasis role="red">junit</emphasis>&lt;/groupId&gt;           &lt;!-- <emphasis
            role="red">wonderfall</emphasis> --&gt;
  &lt;artifactId&gt;<emphasis role="red">junit</emphasis>&lt;/artifactId&gt;     &lt;!-- <emphasis
            role="red">nextcloud</emphasis> --&gt;
  &lt;version&gt;<emphasis role="red">4.12</emphasis>&lt;/version&gt;            &lt;!-- <emphasis
            role="red">13.0.4</emphasis> --&gt;
&lt;/dependency&gt;</programlisting>
      </figure>

      <figure xml:id="sdiDocker_figUntagImage_fig">
        <title>Un tag image by version</title>

        <screen>&gt; docker image rm nextcloud:latest
Untagged: nextcloud:latest

&gt; docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nextcloud           13.0.4              dac77467ddcc        4 days ago          544MB
...</screen>
      </figure>

      <figure xml:id="sdiDocker_figRemoveImage_fig">
        <title>Remove image by version</title>

        <screen>&gt; docker image rm nextcloud:13.0.2
Untagged: nextcloud:latest
Untagged: nextcloud@sha256:c693921e69cb89cd0bee7c014280159df647e5ba87f54c895650156d973df035
Deleted: sha256:10ae267ddcf25bfe5cc059685d3d005bcfe7229b44c3a6f93e0a07795d33b5b2
Deleted: sha256:1c271f4f2f19a222cd116c771b02294c5dd596fa8a0366559061f83a0de8de3f
...
Deleted: sha256:2c833f307fd8f18a378b71d3c43c575fabdb88955a2198662938ac2a08a99928</screen>
      </figure>
    </section>

    <section xml:id="sdiDocker_runningContainer_sect">
      <title>Running container</title>

      <figure xml:id="sdiDocker_fig_startNexctcloud">
        <title>Starting <productname>Nextcloud</productname> container</title>

        <screen>&gt; docker run -d -p 8080:80 --name nc13 nextcloud
517e3dba488763eb615f2c68d417e1b7fb3a9ce5bb4a5ffc346dcb86c00d13d2</screen>
      </figure>

      <figure xml:id="sdiDocker_figListContainer">
        <title>List running containers</title>

        <screen>&gt; docker ps -a  <emphasis role="red"># show stopped containers as well</emphasis>
CONTAINER ID  IMAGE      COMMAND                ... PORTS                 NAMES
517e3dba4887  nextcloud  "/entrypoint.sh apac…" ... 0.0.0.0:8080-&gt;80/tcp  <emphasis
            role="red">nc13</emphasis></screen>
      </figure>

      <figure xml:id="sdiDocker_enterRunningContainer">
        <title>Enter running container</title>

        <screen>&gt; docker exec -it nextcloud_db_1 /bin/bash
# ls -al
total 84
drwxr-xr-x   1 root root 4096 Jun 20 09:28 .
drwxr-xr-x   1 root root 4096 Jun 20 09:28 ..
-rwxr-xr-x   1 root root    0 Jun 20 09:28 .dockerenv
drwxr-xr-x   1 root root 4096 May 25 20:20 bin
drwxr-xr-x   2 root root 4096 Nov 19  2017 boot
drwxr-xr-x   5 root root  340 Jun 20 09:28 dev
drwxr-xr-x   2 root root 4096 Apr 30 15:37 docker-entrypoint-initdb.d
lrwxrwxrwx   1 root root   34 May 25 20:20 docker-entrypoint.sh -&gt; usr/local/bin/docker-entrypoint.sh
...</screen>
      </figure>

      <figure xml:id="sdiDocker_figKillProcess">
        <title>Remove container</title>

        <screen>&gt; docker rm <emphasis role="red">nc13</emphasis>
nc13</screen>

        <screen>&gt; docker ps -a
CONTAINER ID   IMAGE       COMMAND             CREATED      STATUS                   ... NAMES</screen>
      </figure>
    </section>

    <section xml:id="sdiDocker_volumes_sect">
      <title>Volumes</title>

      <figure xml:id="sdiDocker_listVolumes">
        <title>List volumes</title>

        <informaltable border="0">
          <tr>
            <td valign="top"><screen>&gt; docker volume ls
DRIVER              VOLUME NAME
local               nextcloud_db
local               nextcloud_nextcloud</screen></td>

            <td valign="top"><screen># ls -al /var/lib/docker/volumes
total 40
drwx------  4 root root  4096 Jun 20 11:13 .
drwx--x--x 14 root root  4096 Jun 20 09:40 ..
-rw-------  1 root root 32768 Jun 20 11:13 metadata.db
drwxr-xr-x  3 root root  4096 Jun 20 11:13 nextcloud_db
drwxr-xr-x  3 root root  4096 Jun 20 11:13 nextcloud_nextcloud</screen></td>
          </tr>
        </informaltable>
      </figure>

      <figure xml:id="sdiDocker_listOrphaneVolumes">
        <title>List orphaned volumes</title>

        <screen>&gt; docker volume ls <emphasis role="red">-f dangling=true</emphasis>
DRIVER              VOLUME NAME
local               nextcloud_db
local               nextcloud_nextcloud</screen>
      </figure>

      <figure xml:id="sdiDocker_removeVolume">
        <title>Remove volumes</title>

        <screen>&gt; docker volume rm nextcloud_db nextcloud_nextcloud
nextcloud_db
nextcloud_nextcloud
</screen>
      </figure>
    </section>
  </section>

  <section xml:id="sdiDocker_sect_exercises">
    <title>Exercises</title>

    <para>In this exercise you are about to replace your package based
    <productname>Nextcloud</productname> installation by a
    <productname>Docker</productname> container based on an already existing
    image. The image is being described at:</para>

    <blockquote>
      <para><uri
      xlink:href="https://github.com/nextcloud/docker">https://github.com/nextcloud/docker</uri></para>
    </blockquote>

    <section xml:id="sdiDocker_installNcSimple">
      <title>The simple way: Using SQLite</title>

      <orderedlist>
        <listitem>
          <para>Prepare your system for using docker.</para>
        </listitem>

        <listitem>
          <para>Read the <link
          xlink:href="https://github.com/nextcloud/docker">Nextcloud docker
          image documentation</link>.</para>
        </listitem>

        <listitem>
          <para>Pull the <code>nextcloud</code> container and start it
          according to the documentation mapping port 80 to 8080 of your host
          system.</para>

          <para>Use <command>ssh ... -L 8080:localhost:8080</command>
          forwarding the port in question to your local system and direct your
          web browser to <uri
          xlink:href="http://localhost:8080">http://localhost:8080</uri>.</para>
        </listitem>

        <listitem>
          <para>Continue the installation by entering admin/admin for username
          and password.</para>
        </listitem>
      </orderedlist>
    </section>

    <section xml:id="sdiDocker_installNcMaria">
      <title>The real McCoy: Using <productname>Mariadb</productname></title>

      <para><productname>SQLite</productname> is not appropriate for (larger)
      production systems. We thus replace it by a
      <productname>mariadb</productname> service.</para>

      <orderedlist>
        <listitem>
          <para>Drop the existing containers among with their volumes.</para>
        </listitem>

        <listitem>
          <para>Follow <uri
          xlink:href="https://github.com/nextcloud/docker#user-content-base-version---apache">https://github.com/nextcloud/docker#user-content-base-version---apache</uri>.
          Create the proposed <filename>docker-compose.yml</filename> file.
          Enter require credentials and start the container using <command
          xlink:href="https://docs.docker.com/compose">docker-compose</command>
          <parameter>up</parameter>.</para>
        </listitem>

        <listitem>
          <para>This time we choose <productname>Mariadb</productname> as
          database server. Connect to <uri
          xlink:href="http://localhost:8080">http://localhost:8080</uri>
          again. Use the database server's container name as hostname
          parameter allowing the <productname>nextcloud</productname>
          container to access the <productname>mariadb</productname> service.
          Then fill in the remaining required connection parameter
          values.</para>
        </listitem>

        <listitem>
          <para>Now configure <uri>ldap://ldap1.hdm-stuttgart.de</uri> as your
          credential provider like you did in your regular
          <productname>Nextcloud</productname> installation before. The log in
          using your regular credentials.</para>
        </listitem>
      </orderedlist>
    </section>
  </section>
</chapter>