From 1868d91c0f1243d93c4bcc27fc93babe1ecfe9b5 Mon Sep 17 00:00:00 2001 From: Bot <bot@takima.fr> Date: Wed, 30 Apr 2025 09:18:21 +0000 Subject: [PATCH] Added chapter 01-docker --- public/404.html | 102 +- public/ch1-discover-docker-td/index.html | 1152 +++++++++++++++++++++ public/ch1-discover-docker-tp/index.html | 1187 ++++++++++++++++++++++ public/cheatsheet/index.html | 142 ++- public/index.html | 138 ++- public/search/search_index.json | 2 +- public/sitemap.xml | 10 + public/sitemap.xml.gz | Bin 195 -> 200 bytes 8 files changed, 2631 insertions(+), 102 deletions(-) create mode 100644 public/ch1-discover-docker-td/index.html create mode 100644 public/ch1-discover-docker-tp/index.html diff --git a/public/404.html b/public/404.html index c311688..44a83e5 100644 --- a/public/404.html +++ b/public/404.html @@ -152,11 +152,15 @@ - <li class="md-tabs__item"> - <a href="/." class="md-tabs__link"> - Devops in Action - Guide - </a> - </li> + + + + <li class="md-tabs__item"> + <a href="/ch1-discover-docker-tp/" class="md-tabs__link"> + TP + </a> + </li> + @@ -164,11 +168,15 @@ - <li class="md-tabs__item"> - <a href="/cheatsheet/" class="md-tabs__link"> - Cheatsheet - </a> - </li> + + + + <li class="md-tabs__item"> + <a href="/ch1-discover-docker-td/" class="md-tabs__link"> + TD + </a> + </li> + </ul> @@ -209,13 +217,46 @@ + + <li class="md-nav__item md-nav__item--nested"> + + + <input class="md-nav__toggle md-toggle" data-md-toggle="__nav_1" type="checkbox" id="__nav_1" > + + + + + <label class="md-nav__link" for="__nav_1"> + TP + <span class="md-nav__icon md-icon"></span> + </label> + + <nav class="md-nav" aria-label="TP" data-md-level="1"> + <label class="md-nav__title" for="__nav_1"> + <span class="md-nav__icon md-icon"></span> + TP + </label> + <ul class="md-nav__list" data-md-scrollfix> + + + + + + <li class="md-nav__item"> - <a href="/." class="md-nav__link"> - Devops in Action - Guide + <a href="/ch1-discover-docker-tp/" class="md-nav__link"> + TP part 01 - Docker </a> </li> + + + </ul> + </nav> + </li> + + @@ -224,13 +265,46 @@ + + <li class="md-nav__item md-nav__item--nested"> + + + <input class="md-nav__toggle md-toggle" data-md-toggle="__nav_2" type="checkbox" id="__nav_2" > + + + + + <label class="md-nav__link" for="__nav_2"> + TD + <span class="md-nav__icon md-icon"></span> + </label> + + <nav class="md-nav" aria-label="TD" data-md-level="1"> + <label class="md-nav__title" for="__nav_2"> + <span class="md-nav__icon md-icon"></span> + TD + </label> + <ul class="md-nav__list" data-md-scrollfix> + + + + + + <li class="md-nav__item"> - <a href="/cheatsheet/" class="md-nav__link"> - Cheatsheet + <a href="/ch1-discover-docker-td/" class="md-nav__link"> + TD part 01 - Docker </a> </li> + + + </ul> + </nav> + </li> + + </ul> </nav> diff --git a/public/ch1-discover-docker-td/index.html b/public/ch1-discover-docker-td/index.html new file mode 100644 index 0000000..8598a3c --- /dev/null +++ b/public/ch1-discover-docker-td/index.html @@ -0,0 +1,1152 @@ + +<!doctype html> +<html lang="en" class="no-js"> + <head> + + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width,initial-scale=1"> + + + + <link rel="icon" href="../assets/images/favicon.png"> + <meta name="generator" content="mkdocs-1.3.0, mkdocs-material-8.2.15"> + + + + <title>TD part 01 - Docker - Devops</title> + + + + <link rel="stylesheet" href="../assets/stylesheets/main.c382b1dc.min.css"> + + + <link rel="stylesheet" href="../assets/stylesheets/palette.cc9b2e1e.min.css"> + + + + <meta name="theme-color" content="#e92063"> + + + + + + + + + <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> + <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,700,700i%7CRoboto+Mono:400,400i,700,700i&display=fallback"> + <style>:root{--md-text-font:"Roboto";--md-code-font:"Roboto Mono"}</style> + + + + <script>__md_scope=new URL("..",location),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script> + + + + + + </head> + + + + + + + + <body dir="ltr" data-md-color-scheme="" data-md-color-primary="pink" data-md-color-accent=""> + + + + <input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off"> + <input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off"> + <label class="md-overlay" for="__drawer"></label> + <div data-md-component="skip"> + + + <a href="#discover-docker" class="md-skip"> + Skip to content + </a> + + </div> + <div data-md-component="announce"> + + </div> + + + + +<header class="md-header" data-md-component="header"> + <nav class="md-header__inner md-grid" aria-label="Header"> + <a href=".." title="Devops" class="md-header__button md-logo" aria-label="Devops" data-md-component="logo"> + + <img src="../assets/logo.png" alt="logo"> + + </a> + <label class="md-header__button md-icon" for="__drawer"> + <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M3 6h18v2H3V6m0 5h18v2H3v-2m0 5h18v2H3v-2Z"/></svg> + </label> + <div class="md-header__title" data-md-component="header-title"> + <div class="md-header__ellipsis"> + <div class="md-header__topic"> + <span class="md-ellipsis"> + Devops + </span> + </div> + <div class="md-header__topic" data-md-component="header-topic"> + <span class="md-ellipsis"> + + TD part 01 - Docker + + </span> + </div> + </div> + </div> + + + + <label class="md-header__button md-icon" for="__search"> + <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5Z"/></svg> + </label> + <div class="md-search" data-md-component="search" role="dialog"> + <label class="md-search__overlay" for="__search"></label> + <div class="md-search__inner" role="search"> + <form class="md-search__form" name="search"> + <input type="text" class="md-search__input" name="query" aria-label="Search" placeholder="Search" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck="false" data-md-component="search-query" required> + <label class="md-search__icon md-icon" for="__search"> + <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5Z"/></svg> + <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> + </label> + <nav class="md-search__options" aria-label="Search"> + + <button type="reset" class="md-search__icon md-icon" aria-label="Clear" tabindex="-1"> + <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19 6.41 17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12 19 6.41Z"/></svg> + </button> + </nav> + + </form> + <div class="md-search__output"> + <div class="md-search__scrollwrap" data-md-scrollfix> + <div class="md-search-result" data-md-component="search-result"> + <div class="md-search-result__meta"> + Initializing search + </div> + <ol class="md-search-result__list"></ol> + </div> + </div> + </div> + </div> +</div> + + + </nav> + +</header> + + <div class="md-container" data-md-component="container"> + + + + + +<nav class="md-tabs" aria-label="Tabs" data-md-component="tabs"> + <div class="md-tabs__inner md-grid"> + <ul class="md-tabs__list"> + + + + + + + + + + <li class="md-tabs__item"> + <a href="../ch1-discover-docker-tp/" class="md-tabs__link"> + TP + </a> + </li> + + + + + + + + + + + + + + <li class="md-tabs__item"> + <a href="./" class="md-tabs__link md-tabs__link--active"> + TD + </a> + </li> + + + + </ul> + </div> +</nav> + + + + <main class="md-main" data-md-component="main"> + <div class="md-main__inner md-grid"> + + + + <div class="md-sidebar md-sidebar--primary" data-md-component="sidebar" data-md-type="navigation" > + <div class="md-sidebar__scrollwrap"> + <div class="md-sidebar__inner"> + + + + + +<nav class="md-nav md-nav--primary md-nav--lifted" aria-label="Navigation" data-md-level="0"> + <label class="md-nav__title" for="__drawer"> + <a href=".." title="Devops" class="md-nav__button md-logo" aria-label="Devops" data-md-component="logo"> + + <img src="../assets/logo.png" alt="logo"> + + </a> + Devops + </label> + + <ul class="md-nav__list" data-md-scrollfix> + + + + + + + + + + <li class="md-nav__item md-nav__item--nested"> + + + <input class="md-nav__toggle md-toggle" data-md-toggle="__nav_1" type="checkbox" id="__nav_1" > + + + + + <label class="md-nav__link" for="__nav_1"> + TP + <span class="md-nav__icon md-icon"></span> + </label> + + <nav class="md-nav" aria-label="TP" data-md-level="1"> + <label class="md-nav__title" for="__nav_1"> + <span class="md-nav__icon md-icon"></span> + TP + </label> + <ul class="md-nav__list" data-md-scrollfix> + + + + + + + <li class="md-nav__item"> + <a href="../ch1-discover-docker-tp/" class="md-nav__link"> + TP part 01 - Docker + </a> + </li> + + + + + </ul> + </nav> + </li> + + + + + + + + + + + + + + <li class="md-nav__item md-nav__item--active md-nav__item--nested"> + + + <input class="md-nav__toggle md-toggle" data-md-toggle="__nav_2" type="checkbox" id="__nav_2" checked> + + + + + <label class="md-nav__link" for="__nav_2"> + TD + <span class="md-nav__icon md-icon"></span> + </label> + + <nav class="md-nav" aria-label="TD" data-md-level="1"> + <label class="md-nav__title" for="__nav_2"> + <span class="md-nav__icon md-icon"></span> + TD + </label> + <ul class="md-nav__list" data-md-scrollfix> + + + + + + + + + <li class="md-nav__item md-nav__item--active"> + + <input class="md-nav__toggle md-toggle" data-md-toggle="toc" type="checkbox" id="__toc"> + + + + + + <label class="md-nav__link md-nav__link--active" for="__toc"> + TD part 01 - Docker + <span class="md-nav__icon md-icon"></span> + </label> + + <a href="./" class="md-nav__link md-nav__link--active"> + TD part 01 - Docker + </a> + + + +<nav class="md-nav md-nav--secondary" aria-label="Table of contents"> + + + + + + + <label class="md-nav__title" for="__toc"> + <span class="md-nav__icon md-icon"></span> + Table of contents + </label> + <ul class="md-nav__list" data-md-component="toc" data-md-scrollfix> + + <li class="md-nav__item"> + <a href="#setup" class="md-nav__link"> + Setup + </a> + + <nav class="md-nav" aria-label="Setup"> + <ul class="md-nav__list"> + + <li class="md-nav__item"> + <a href="#prerequisites" class="md-nav__link"> + Prerequisites + </a> + +</li> + + <li class="md-nav__item"> + <a href="#setting-up-your-computer" class="md-nav__link"> + Setting up your computer + </a> + +</li> + + <li class="md-nav__item"> + <a href="#running-your-first-container" class="md-nav__link"> + Running your first container + </a> + +</li> + + </ul> + </nav> + +</li> + + <li class="md-nav__item"> + <a href="#11-docker-run" class="md-nav__link"> + 1.1 Docker Run + </a> + +</li> + + <li class="md-nav__item"> + <a href="#12-terminology" class="md-nav__link"> + 1.2 Terminology + </a> + +</li> + + <li class="md-nav__item"> + <a href="#20-webapps-with-docker" class="md-nav__link"> + 2.0 Webapps with Docker + </a> + +</li> + + <li class="md-nav__item"> + <a href="#21-run-a-static-website-in-a-container" class="md-nav__link"> + 2.1 Run a static website in a container + </a> + +</li> + + <li class="md-nav__item"> + <a href="#22-docker-images" class="md-nav__link"> + 2.2 Docker Images + </a> + +</li> + + <li class="md-nav__item"> + <a href="#23-create-your-first-image" class="md-nav__link"> + 2.3 Create your first image + </a> + +</li> + + <li class="md-nav__item"> + <a href="#231-create-a-python-flask-app-that-displays-random-cat-pix" class="md-nav__link"> + 2.3.1 Create a Python Flask app that displays random cat pix. + </a> + + <nav class="md-nav" aria-label="2.3.1 Create a Python Flask app that displays random cat pix."> + <ul class="md-nav__list"> + + <li class="md-nav__item"> + <a href="#apppy" class="md-nav__link"> + app.py + </a> + +</li> + + <li class="md-nav__item"> + <a href="#requirementstxt" class="md-nav__link"> + requirements.txt + </a> + +</li> + + <li class="md-nav__item"> + <a href="#templatesindexhtml" class="md-nav__link"> + templates/index.html + </a> + +</li> + + </ul> + </nav> + +</li> + + <li class="md-nav__item"> + <a href="#232-write-a-dockerfile" class="md-nav__link"> + 2.3.2 Write a Dockerfile + </a> + +</li> + + <li class="md-nav__item"> + <a href="#233-build-the-image" class="md-nav__link"> + 2.3.3 Build the image + </a> + +</li> + + <li class="md-nav__item"> + <a href="#234-run-your-image" class="md-nav__link"> + 2.3.4 Run your image + </a> + +</li> + + <li class="md-nav__item"> + <a href="#234-dockerfile-commands-summary" class="md-nav__link"> + 2.3.4 Dockerfile commands summary + </a> + +</li> + + </ul> + +</nav> + + </li> + + + + + </ul> + </nav> + </li> + + + + </ul> +</nav> + </div> + </div> + </div> + + + + <div class="md-sidebar md-sidebar--secondary" data-md-component="sidebar" data-md-type="toc" > + <div class="md-sidebar__scrollwrap"> + <div class="md-sidebar__inner"> + + +<nav class="md-nav md-nav--secondary" aria-label="Table of contents"> + + + + + + + <label class="md-nav__title" for="__toc"> + <span class="md-nav__icon md-icon"></span> + Table of contents + </label> + <ul class="md-nav__list" data-md-component="toc" data-md-scrollfix> + + <li class="md-nav__item"> + <a href="#setup" class="md-nav__link"> + Setup + </a> + + <nav class="md-nav" aria-label="Setup"> + <ul class="md-nav__list"> + + <li class="md-nav__item"> + <a href="#prerequisites" class="md-nav__link"> + Prerequisites + </a> + +</li> + + <li class="md-nav__item"> + <a href="#setting-up-your-computer" class="md-nav__link"> + Setting up your computer + </a> + +</li> + + <li class="md-nav__item"> + <a href="#running-your-first-container" class="md-nav__link"> + Running your first container + </a> + +</li> + + </ul> + </nav> + +</li> + + <li class="md-nav__item"> + <a href="#11-docker-run" class="md-nav__link"> + 1.1 Docker Run + </a> + +</li> + + <li class="md-nav__item"> + <a href="#12-terminology" class="md-nav__link"> + 1.2 Terminology + </a> + +</li> + + <li class="md-nav__item"> + <a href="#20-webapps-with-docker" class="md-nav__link"> + 2.0 Webapps with Docker + </a> + +</li> + + <li class="md-nav__item"> + <a href="#21-run-a-static-website-in-a-container" class="md-nav__link"> + 2.1 Run a static website in a container + </a> + +</li> + + <li class="md-nav__item"> + <a href="#22-docker-images" class="md-nav__link"> + 2.2 Docker Images + </a> + +</li> + + <li class="md-nav__item"> + <a href="#23-create-your-first-image" class="md-nav__link"> + 2.3 Create your first image + </a> + +</li> + + <li class="md-nav__item"> + <a href="#231-create-a-python-flask-app-that-displays-random-cat-pix" class="md-nav__link"> + 2.3.1 Create a Python Flask app that displays random cat pix. + </a> + + <nav class="md-nav" aria-label="2.3.1 Create a Python Flask app that displays random cat pix."> + <ul class="md-nav__list"> + + <li class="md-nav__item"> + <a href="#apppy" class="md-nav__link"> + app.py + </a> + +</li> + + <li class="md-nav__item"> + <a href="#requirementstxt" class="md-nav__link"> + requirements.txt + </a> + +</li> + + <li class="md-nav__item"> + <a href="#templatesindexhtml" class="md-nav__link"> + templates/index.html + </a> + +</li> + + </ul> + </nav> + +</li> + + <li class="md-nav__item"> + <a href="#232-write-a-dockerfile" class="md-nav__link"> + 2.3.2 Write a Dockerfile + </a> + +</li> + + <li class="md-nav__item"> + <a href="#233-build-the-image" class="md-nav__link"> + 2.3.3 Build the image + </a> + +</li> + + <li class="md-nav__item"> + <a href="#234-run-your-image" class="md-nav__link"> + 2.3.4 Run your image + </a> + +</li> + + <li class="md-nav__item"> + <a href="#234-dockerfile-commands-summary" class="md-nav__link"> + 2.3.4 Dockerfile commands summary + </a> + +</li> + + </ul> + +</nav> + </div> + </div> + </div> + + + <div class="md-content" data-md-component="content"> + <article class="md-content__inner md-typeset"> + + + + +<h1 id="discover-docker">Discover Docker</h1> +<div class="admonition check"> +<p class="admonition-title">Check</p> +<p>Checkpoint: call us to check your results (don’t stay blocked on a checkpoint if we are busy, we can check ⅔ checkpoints at the same time) </p> +</div> +<div class="admonition question"> +<p class="admonition-title">Question</p> +<p>Point to document/report </p> +</div> +<div class="admonition tip"> +<p class="admonition-title">Tip</p> +<p>Interesting information</p> +</div> +<h2 id="setup">Setup</h2> +<h3 id="prerequisites">Prerequisites</h3> +<p>There are no specific skills needed for this tutorial beyond a basic comfort with the command line and using a text editor.<br /> +Prior experience in developing web applications will be helpful but is not required. As you proceed further along the tutorial, we'll make use of https://cloud.docker.com/. </p> +<h3 id="setting-up-your-computer">Setting up your computer</h3> +<p>Getting all the tooling setup on your computer can be a daunting task, but getting Docker up and running on your favorite OS has become very easy.</p> +<p>The getting started guide on Docker has detailed instructions for setting up Docker on <a href="https://docs.docker.com/desktop/install/mac-install/">Mac</a>, <a href="https://docs.docker.com/engine/install/">Linux</a> and <a href="https://docs.docker.com/desktop/install/windows-install/">Windows</a></p> +<p>If you're using Docker for Windows make sure you have shared your drive.</p> +<p>Important note If you're using an older version of Windows or MacOS you may need to use Docker Machine instead.<br /> +All commands work in either bash or Powershell on Windows</p> +<p>Once you are done installing Docker, test your Docker installation by running the following: </p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a>docker<span class="w"> </span>run<span class="w"> </span>hello-world +<a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a>Unable<span class="w"> </span>to<span class="w"> </span>find<span class="w"> </span>image<span class="w"> </span><span class="s1">'hello-world:latest'</span><span class="w"> </span>locally +<a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a>latest:<span class="w"> </span>Pulling<span class="w"> </span>from<span class="w"> </span>library/hello-world +<a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a>03f4658f8b78:<span class="w"> </span>Pull<span class="w"> </span><span class="nb">complete</span> +<a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a>a3ed95caeb02:<span class="w"> </span>Pull<span class="w"> </span><span class="nb">complete</span> +<a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a>Digest:<span class="w"> </span>sha256:8be990ef2aeb16dbcb9271ddfe2610fa6658d13f6dfb8bc72074cc1ca36966a7 +<a id="__codelineno-0-7" name="__codelineno-0-7" href="#__codelineno-0-7"></a>Status:<span class="w"> </span>Downloaded<span class="w"> </span>newer<span class="w"> </span>image<span class="w"> </span><span class="k">for</span><span class="w"> </span>hello-world:latest +<a id="__codelineno-0-8" name="__codelineno-0-8" href="#__codelineno-0-8"></a> +<a id="__codelineno-0-9" name="__codelineno-0-9" href="#__codelineno-0-9"></a>Hello<span class="w"> </span>from<span class="w"> </span>Docker. +<a id="__codelineno-0-10" name="__codelineno-0-10" href="#__codelineno-0-10"></a>... +</code></pre></div> +<p>This message shows that your installation appears to be working correctly.</p> +<h3 id="running-your-first-container">Running your first container</h3> +<p>Now that you have everything setup, it's time to get our hands dirty. <br /> +In this section, you are going to run an <a href="https://www.alpinelinux.org/" target="_blank">Alpine Linux</a> container (a lightweight linux distribution) on your system and get a taste of the <code>docker run</code> command.</p> +<p>To get started, let's run the following in our terminal:</p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a>docker<span class="w"> </span>pull<span class="w"> </span>alpine +</code></pre></div> +<div class="admonition note"> +<p class="admonition-title">Note</p> +<p>Depending on how you've installed docker on your system, you might see a permission denied error after running the above command. Try the commands from the Getting Started tutorial to <a href="https://docs.docker.com/get-started/#/step-3-verify-your-installation" target="_blank">verify your installation</a>. If you're on Linux, you may need to prefix your <code>docker</code> commands with <code>sudo</code>. Alternatively you can <a href="https://docs.docker.com/engine/installation/linux/ubuntulinux/#/create-a-docker-group" target="_blank">create a docker group</a> to get rid of this issue.</p> +</div> +<p>The <code>pull</code> command fetches the alpine image from the Docker registry and saves it in our system. You can use the <code>docker images</code> command to see a list of all images on your system.</p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-2-1" name="__codelineno-2-1" href="#__codelineno-2-1"></a>docker<span class="w"> </span>images +<a id="__codelineno-2-2" name="__codelineno-2-2" href="#__codelineno-2-2"></a>REPOSITORY<span class="w"> </span>TAG<span class="w"> </span>IMAGE<span class="w"> </span>ID<span class="w"> </span>CREATED<span class="w"> </span>VIRTUAL<span class="w"> </span>SIZE +<a id="__codelineno-2-3" name="__codelineno-2-3" href="#__codelineno-2-3"></a>alpine<span class="w"> </span>latest<span class="w"> </span>c51f86c28340<span class="w"> </span><span class="m">4</span><span class="w"> </span>weeks<span class="w"> </span>ago<span class="w"> </span><span class="m">1</span>.109<span class="w"> </span>MB +<a id="__codelineno-2-4" name="__codelineno-2-4" href="#__codelineno-2-4"></a>hello-world<span class="w"> </span>latest<span class="w"> </span>690ed74de00f<span class="w"> </span><span class="m">5</span><span class="w"> </span>months<span class="w"> </span>ago<span class="w"> </span><span class="m">960</span><span class="w"> </span>B +</code></pre></div> +<h2 id="11-docker-run">1.1 Docker Run</h2> +<p>Great! Let's now run a Docker container based on this image. To do that you are going to use the docker run command.</p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-3-1" name="__codelineno-3-1" href="#__codelineno-3-1"></a>docker run alpine ls -l +<a id="__codelineno-3-2" name="__codelineno-3-2" href="#__codelineno-3-2"></a>total 48 +<a id="__codelineno-3-3" name="__codelineno-3-3" href="#__codelineno-3-3"></a>drwxr-xr-x 2 root root 4096 Mar 2 16:20 bin +<a id="__codelineno-3-4" name="__codelineno-3-4" href="#__codelineno-3-4"></a>drwxr-xr-x 5 root root 360 Mar 18 09:47 dev +<a id="__codelineno-3-5" name="__codelineno-3-5" href="#__codelineno-3-5"></a>drwxr-xr-x 13 root root 4096 Mar 18 09:47 etc +<a id="__codelineno-3-6" name="__codelineno-3-6" href="#__codelineno-3-6"></a>drwxr-xr-x 2 root root 4096 Mar 2 16:20 home +<a id="__codelineno-3-7" name="__codelineno-3-7" href="#__codelineno-3-7"></a>drwxr-xr-x 5 root root 4096 Mar 2 16:20 lib +<a id="__codelineno-3-8" name="__codelineno-3-8" href="#__codelineno-3-8"></a>...... +<a id="__codelineno-3-9" name="__codelineno-3-9" href="#__codelineno-3-9"></a>...... +</code></pre></div> +<p>What happened? Behind the scenes, a lot of stuff happened. When you call <code>run</code>: +1. The Docker client contacts the Docker daemon.</p> +<ol> +<li> +<p>The Docker daemon checks local store if the image (alpine in this case) is available locally, and if not, downloads it from Docker Store. (Since we have issued <code>docker pull alpine</code> before, the download step is not necessary)</p> +</li> +<li> +<p>The Docker daemon creates the container and then runs a command in that container.</p> +</li> +<li> +<p>The Docker daemon streams the output of the command to the Docker client</p> +</li> +</ol> +<p>When you run <code>docker run alpine</code>, you provided a command (<code>ls -l</code>), so Docker started the command specified and you saw the listing.</p> +<p>Let's try something more exciting. +<div class="highlight"><pre><span></span><code><a id="__codelineno-4-1" name="__codelineno-4-1" href="#__codelineno-4-1"></a>docker run alpine echo "hello from alpine" +<a id="__codelineno-4-2" name="__codelineno-4-2" href="#__codelineno-4-2"></a>hello from alpine +</code></pre></div></p> +<p>OK, that's some actual output. In this case, the Docker client dutifully ran the <code>echo</code> command in our alpine container and then exited it. If you've noticed, all of that happened pretty quickly. Imagine booting up a virtual machine, running a command and then killing it. Now you know why they say containers are fast!</p> +<p>Try another command. +<div class="highlight"><pre><span></span><code><a id="__codelineno-5-1" name="__codelineno-5-1" href="#__codelineno-5-1"></a>docker run alpine /bin/sh +</code></pre></div> +Wait, nothing happened! Is that a bug? Well, no. These interactive shells will exit after running any scripted commands, unless they are run in an interactive terminal - so for this example to not exit, you need to <code>docker run -it alpine /bin/sh</code> .</p> +<p>You are now inside the container shell and you can try out a few commands like <code>ls -l</code>, <code>uname -a</code> and others. Exit out of the container by giving the <code>exit</code> command.</p> +<p>Ok, now it's time to see the <code>docker ps</code> command. The <code>docker ps</code> command shows you all containers that are currently running.</p> +<p><div class="highlight"><pre><span></span><code><a id="__codelineno-6-1" name="__codelineno-6-1" href="#__codelineno-6-1"></a>docker<span class="w"> </span>ps +<a id="__codelineno-6-2" name="__codelineno-6-2" href="#__codelineno-6-2"></a>CONTAINER<span class="w"> </span>ID<span class="w"> </span>IMAGE<span class="w"> </span>COMMAND<span class="w"> </span>CREATED<span class="w"> </span>STATUS<span class="w"> </span>PORTS<span class="w"> </span>NAMES +</code></pre></div> +Since no containers are running, you see a blank line. Let's try a more useful variant: <code>docker ps -a</code></p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-7-1" name="__codelineno-7-1" href="#__codelineno-7-1"></a>docker<span class="w"> </span>ps<span class="w"> </span>-a +<a id="__codelineno-7-2" name="__codelineno-7-2" href="#__codelineno-7-2"></a>CONTAINER<span class="w"> </span>ID<span class="w"> </span>IMAGE<span class="w"> </span>COMMAND<span class="w"> </span>CREATED<span class="w"> </span>STATUS<span class="w"> </span>PORTS<span class="w"> </span>NAMES +<a id="__codelineno-7-3" name="__codelineno-7-3" href="#__codelineno-7-3"></a>36171a5da744<span class="w"> </span>alpine<span class="w"> </span><span class="s2">"/bin/sh"</span><span class="w"> </span><span class="m">5</span><span class="w"> </span>minutes<span class="w"> </span>ago<span class="w"> </span>Exited<span class="w"> </span><span class="o">(</span><span class="m">0</span><span class="o">)</span><span class="w"> </span><span class="m">2</span><span class="w"> </span>minutes<span class="w"> </span>ago<span class="w"> </span>fervent_newton +<a id="__codelineno-7-4" name="__codelineno-7-4" href="#__codelineno-7-4"></a>a6a9d46d0b2f<span class="w"> </span>alpine<span class="w"> </span><span class="s2">"echo 'hello from alp"</span><span class="w"> </span><span class="m">6</span><span class="w"> </span>minutes<span class="w"> </span>ago<span class="w"> </span>Exited<span class="w"> </span><span class="o">(</span><span class="m">0</span><span class="o">)</span><span class="w"> </span><span class="m">6</span><span class="w"> </span>minutes<span class="w"> </span>ago<span class="w"> </span>lonely_kilby +<a id="__codelineno-7-5" name="__codelineno-7-5" href="#__codelineno-7-5"></a>ff0a5c3750b9<span class="w"> </span>alpine<span class="w"> </span><span class="s2">"ls -l"</span><span class="w"> </span><span class="m">8</span><span class="w"> </span>minutes<span class="w"> </span>ago<span class="w"> </span>Exited<span class="w"> </span><span class="o">(</span><span class="m">0</span><span class="o">)</span><span class="w"> </span><span class="m">8</span><span class="w"> </span>minutes<span class="w"> </span>ago<span class="w"> </span>elated_ramanujan +<a id="__codelineno-7-6" name="__codelineno-7-6" href="#__codelineno-7-6"></a>c317d0a9e3d2<span class="w"> </span>hello-world<span class="w"> </span><span class="s2">"/hello"</span><span class="w"> </span><span class="m">34</span><span class="w"> </span>seconds<span class="w"> </span>ago<span class="w"> </span>Exited<span class="w"> </span><span class="o">(</span><span class="m">0</span><span class="o">)</span><span class="w"> </span><span class="m">12</span><span class="w"> </span>minutes<span class="w"> </span>ago<span class="w"> </span>stupefied_mcclintock +</code></pre></div> +<p>What you see above is a list of all containers that you ran. Notice that the <code>STATUS</code> column shows that these containers exited a few minutes ago. You're probably wondering if there is a way to run more than just one command in a container. Let's try that now:</p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-8-1" name="__codelineno-8-1" href="#__codelineno-8-1"></a>docker run -it alpine /bin/sh +<a id="__codelineno-8-2" name="__codelineno-8-2" href="#__codelineno-8-2"></a>/ # ls +<a id="__codelineno-8-3" name="__codelineno-8-3" href="#__codelineno-8-3"></a>bin dev etc home lib linuxrc media mnt proc root run sbin sys tmp usr var +<a id="__codelineno-8-4" name="__codelineno-8-4" href="#__codelineno-8-4"></a>/ # uname -a +<a id="__codelineno-8-5" name="__codelineno-8-5" href="#__codelineno-8-5"></a>Linux 97916e8cb5dc 4.4.27-moby #1 SMP Wed Oct 26 14:01:48 UTC 2016 x86_64 Linux +</code></pre></div> +<p>Running the <code>run</code> command with the <code>-it</code> flags attaches us to an interactive <a href="https://fr.wikipedia.org/wiki/Tty_(Unix)" target="_blank">tty</a> in the container. Now you can run as many commands in the container as you want. Take some time to run your favorite commands.</p> +<div class="admonition tip"> +<p class="admonition-title">Tip</p> +<p><code>run -it</code> is a very useful command to debug at the lowest level a container. </p> +</div> +<p>That concludes a whirlwind tour of the <code>docker run</code> command which would most likely be the command you'll use most often. It makes sense to spend some time getting comfortable with it. To find out more about <code>run</code>, use <code>docker run --help</code> to see a list of all flags it supports. As you proceed further, we'll see a few more variants of docker run.</p> +<h2 id="12-terminology">1.2 Terminology</h2> +<p>In the last section, you saw a lot of Docker-specific jargon which might be confusing to some. So before you go further, let's clarify some terminology that is used frequently in the Docker ecosystem. </p> +<ul> +<li><strong>Images</strong> - The file system and configuration of our application which are used to create containers. To find out more about a Docker image, run <code>docker inspect alpine</code>. In the demo above, you used the <code>docker pull</code> command to download the alpine image. When you executed the command <code>docker run hello-world</code>, it also did a <code>docker pull</code> behind the scenes to download the hello-world image. </li> +<li><strong>Containers</strong> - Running instances of Docker images — containers run the actual applications. A container includes an application and all of its dependencies. It shares the kernel with other containers, and runs as an isolated process in user space on the host OS. You created a container using <code>docker run</code> which you did using the alpine image that you downloaded. A list of running containers can be seen using the <code>docker ps</code> command. </li> +<li><strong>Docker daemon</strong> - The background service running on the host that manages building, running and distributing Docker containers. </li> +<li><strong>Docker client</strong> - The command line tool that allows the user to interact with the Docker daemon. </li> +<li><strong>Docker Store</strong> - A <a href="https://store.docker.com/" target="_blank">registry</a> of Docker images, where you can find trusted and enterprise ready containers, plugins, and Docker editions. You'll be using this later in this tutorial. </li> +</ul> +<h2 id="20-webapps-with-docker">2.0 Webapps with Docker</h2> +<p>Great! So you have now looked at <code>docker run</code>, played with a Docker container and also got the hang of some terminology. Armed with all this knowledge, you are now ready to get to the real stuff — deploying web applications with Docker.</p> +<h2 id="21-run-a-static-website-in-a-container">2.1 Run a static website in a container</h2> +<div class="admonition note"> +<p class="admonition-title">Note</p> +<p>Code for this section is in this repo in the <a href="https://github.com/docker/labs/tree/master/beginner/static-site" target="_blank">static-site directory</a></p> +</div> +<p>Let's start by taking baby-steps. First, we'll use Docker to run a static website in a container. +The website is based on an existing image. We'll pull a Docker image from Docker Store, run the container, and see how easy it is to set up a web server.</p> +<p>The image that you are going to use is a single-page website that was already created for this demo and is available on the Docker Store as <a href="https://hub.docker.com/r/dockersamples/static-site" target="_blank">dockersamples/static-site</a>.<br /> +You can download and run the image directly in one go using <code>docker run</code> as follows.<br /> +<div class="highlight"><pre><span></span><code><a id="__codelineno-9-1" name="__codelineno-9-1" href="#__codelineno-9-1"></a>docker<span class="w"> </span>run<span class="w"> </span>-d<span class="w"> </span>dockersamples/static-site +</code></pre></div></p> +<div class="admonition note"> +<p class="admonition-title">Note</p> +<p>The current version of this image doesn't run without the -d flag. The -d flag enables detached mode, which detaches the running container from the terminal/shell and returns your prompt after the container starts. We are debugging the problem with this image but for now, use -d even for this first example. </p> +</div> +<div class="admonition tip"> +<p class="admonition-title">Tip</p> +<p><code>-d</code> is a very useful option. </p> +</div> +<p>So, what happens when you run this command? </p> +<p>Since the image doesn't exist on your Docker host, the Docker daemon first fetches it from the registry and then runs it as a container.</p> +<p>Now that the server is running, do you see the website? What port is it running on? And more importantly, how do you access the container directly from our host machine? </p> +<p>Actually, you probably won't be able to answer any of these questions yet! ☺ In this case, the client didn't tell the Docker Engine to publish any of the ports, so you need to re-run the <code>docker run</code> command to add this instruction.</p> +<p>Let's re-run the command with some new flags to publish ports and pass your name to the container to customize the message displayed. We'll use the -d option again to run the container in detached mode.</p> +<p>First, stop the container that you have just launched. In order to do this, we need the container ID.</p> +<p>Since we ran the container in detached mode, we don't have to launch another terminal to do this. Run <code>docker ps</code> to view the running containers.</p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-10-1" name="__codelineno-10-1" href="#__codelineno-10-1"></a>docker<span class="w"> </span>ps +<a id="__codelineno-10-2" name="__codelineno-10-2" href="#__codelineno-10-2"></a>CONTAINER<span class="w"> </span>ID<span class="w"> </span>IMAGE<span class="w"> </span>COMMAND<span class="w"> </span>CREATED<span class="w"> </span>STATUS<span class="w"> </span>PORTS<span class="w"> </span>NAMES +<a id="__codelineno-10-3" name="__codelineno-10-3" href="#__codelineno-10-3"></a>a7a0e504ca3e<span class="w"> </span>dockersamples/static-site<span class="w"> </span><span class="s2">"/bin/sh -c 'cd /usr/"</span><span class="w"> </span><span class="m">28</span><span class="w"> </span>seconds<span class="w"> </span>ago<span class="w"> </span>Up<span class="w"> </span><span class="m">26</span><span class="w"> </span>seconds<span class="w"> </span><span class="m">80</span>/tcp,<span class="w"> </span><span class="m">443</span>/tcp<span class="w"> </span>stupefied_mahavira +</code></pre></div> +<p>Check out the <code>CONTAINER ID</code> column. You will need to use this <code>CONTAINER ID</code> value, a long sequence of characters, to identify the container you want to stop, and then to remove it. The example below provides the CONTAINER ID on our system; you should use the value that you see in your terminal.</p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-11-1" name="__codelineno-11-1" href="#__codelineno-11-1"></a>docker<span class="w"> </span>stop<span class="w"> </span>a7a0e504ca3e +<a id="__codelineno-11-2" name="__codelineno-11-2" href="#__codelineno-11-2"></a>docker<span class="w"> </span>rm<span class="w"> </span>a7a0e504ca3e +</code></pre></div> +<div class="admonition note"> +<p class="admonition-title">Note</p> +<p>A cool feature is that you do not need to specify the entire <code>CONTAINER ID</code>. You can just specify a few starting characters and if it is unique among all the containers that you have launched, the Docker client will intelligently pick it up.</p> +</div> +<p>Now, let's launch a container in detached mode as shown below:</p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-12-1" name="__codelineno-12-1" href="#__codelineno-12-1"></a>docker<span class="w"> </span>run<span class="w"> </span>--name<span class="w"> </span>static-site<span class="w"> </span>-e<span class="w"> </span><span class="nv">AUTHOR</span><span class="o">=</span><span class="s2">"Enter Your Name Here"</span><span class="w"> </span>-d<span class="w"> </span>-P<span class="w"> </span>dockersamples/static-site +<a id="__codelineno-12-2" name="__codelineno-12-2" href="#__codelineno-12-2"></a> +<a id="__codelineno-12-3" name="__codelineno-12-3" href="#__codelineno-12-3"></a>e61d12292d69556eabe2a44c16cbd54486b2527e2ce4f95438e504afb7b02810 +</code></pre></div> +<p>In the above command: </p> +<ul> +<li><code>-d</code> will create a container with the process detached from our terminal </li> +<li><code>-P</code> will publish all the exposed container ports to random ports on the Docker host </li> +<li><code>-e</code> is how you pass environment variables to the container. </li> +<li><code>--name</code> allows you to specify a container name </li> +<li><code>AUTHOR</code> is the environment variable name and <code>Your Name</code> is the value that you can pass. </li> +</ul> +<p>Now you can see the ports by running the docker port command.</p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-13-1" name="__codelineno-13-1" href="#__codelineno-13-1"></a>docker<span class="w"> </span>port<span class="w"> </span>static-site +<a id="__codelineno-13-2" name="__codelineno-13-2" href="#__codelineno-13-2"></a><span class="m">443</span>/tcp<span class="w"> </span>-><span class="w"> </span><span class="m">0</span>.0.0.0:32772 +<a id="__codelineno-13-3" name="__codelineno-13-3" href="#__codelineno-13-3"></a><span class="m">80</span>/tcp<span class="w"> </span>-><span class="w"> </span><span class="m">0</span>.0.0.0:32773 +</code></pre></div> +<p>You can open your freshly created website on <code>http://localhost:[YOUR_PORT_FOR 80/tcp]</code>. For our example this is <code>http://localhost:32773</code>.</p> +<p>You can now open <code>http://localhost:[YOUR_PORT_FOR 80/tcp]</code> to see your site live! For our example, this is: <code>http://192.168.99.100:32773</code>.</p> +<p>You can also run a second webserver at the same time, specifying a custom host port mapping to the container's webserver.</p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-14-1" name="__codelineno-14-1" href="#__codelineno-14-1"></a>docker<span class="w"> </span>run<span class="w"> </span>--name<span class="w"> </span>static-site-2<span class="w"> </span>-e<span class="w"> </span><span class="nv">AUTHOR</span><span class="o">=</span><span class="s2">"Enter Your Name Here"</span><span class="w"> </span>-d<span class="w"> </span>-p<span class="w"> </span><span class="m">8888</span>:80<span class="w"> </span>dockersamples/static-site +</code></pre></div> +<p>To deploy this on a real server you would just need to install Docker, and run the above <code>docker</code> command (as in this case you can see the <code>AUTHOR</code> is Docker which we passed as an environment variable).</p> +<p>Now that you've seen how to run a webserver inside a Docker container, how do you create your own Docker image? This is the question we'll explore in the next section. </p> +<p>But first, let's stop and remove the containers since you won't be using them anymore.</p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-15-1" name="__codelineno-15-1" href="#__codelineno-15-1"></a>docker<span class="w"> </span>stop<span class="w"> </span>static-site +<a id="__codelineno-15-2" name="__codelineno-15-2" href="#__codelineno-15-2"></a>docker<span class="w"> </span>rm<span class="w"> </span>static-site +</code></pre></div> +<p>Let's use a shortcut to remove the second site:</p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-16-1" name="__codelineno-16-1" href="#__codelineno-16-1"></a>docker<span class="w"> </span>rm<span class="w"> </span>-f<span class="w"> </span>static-site-2 +</code></pre></div> +<div class="admonition tip"> +<p class="admonition-title">Tip</p> +<p>rm -f is a very useful option</p> +</div> +<p>Run <code>docker ps</code> to make sure the containers are gone.</p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-17-1" name="__codelineno-17-1" href="#__codelineno-17-1"></a>docker<span class="w"> </span>ps +<a id="__codelineno-17-2" name="__codelineno-17-2" href="#__codelineno-17-2"></a>CONTAINER<span class="w"> </span>ID<span class="w"> </span>IMAGE<span class="w"> </span>COMMAND<span class="w"> </span>CREATED<span class="w"> </span>STATUS<span class="w"> </span>PORTS<span class="w"> </span>NAMES +</code></pre></div> +<h2 id="22-docker-images">2.2 Docker Images</h2> +<p>In this section, let's dive deeper into what Docker images are. You will build your own image, use that image to run an application locally, and finally, push some of your own images to Docker Cloud.</p> +<p>Docker images are the basis of containers. In the previous example, you pulled the <code>dockersamples/static-site</code> image from the. registry and asked the Docker client to run a container based on that image. To see the list of images that are available locally on your system, run the docker images command. </p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-18-1" name="__codelineno-18-1" href="#__codelineno-18-1"></a>docker<span class="w"> </span>images +<a id="__codelineno-18-2" name="__codelineno-18-2" href="#__codelineno-18-2"></a>REPOSITORY<span class="w"> </span>TAG<span class="w"> </span>IMAGE<span class="w"> </span>ID<span class="w"> </span>CREATED<span class="w"> </span>SIZE +<a id="__codelineno-18-3" name="__codelineno-18-3" href="#__codelineno-18-3"></a>dockersamples/static-site<span class="w"> </span>latest<span class="w"> </span>92a386b6e686<span class="w"> </span><span class="m">2</span><span class="w"> </span>hours<span class="w"> </span>ago<span class="w"> </span><span class="m">190</span>.5<span class="w"> </span>MB +<a id="__codelineno-18-4" name="__codelineno-18-4" href="#__codelineno-18-4"></a>nginx<span class="w"> </span>latest<span class="w"> </span>af4b3d7d5401<span class="w"> </span><span class="m">3</span><span class="w"> </span>hours<span class="w"> </span>ago<span class="w"> </span><span class="m">190</span>.5<span class="w"> </span>MB +<a id="__codelineno-18-5" name="__codelineno-18-5" href="#__codelineno-18-5"></a>python<span class="w"> </span><span class="m">2</span>.7<span class="w"> </span>1c32174fd534<span class="w"> </span><span class="m">14</span><span class="w"> </span>hours<span class="w"> </span>ago<span class="w"> </span><span class="m">676</span>.8<span class="w"> </span>MB +<a id="__codelineno-18-6" name="__codelineno-18-6" href="#__codelineno-18-6"></a>postgres<span class="w"> </span><span class="m">9</span>.4<span class="w"> </span>88d845ac7a88<span class="w"> </span><span class="m">14</span><span class="w"> </span>hours<span class="w"> </span>ago<span class="w"> </span><span class="m">263</span>.6<span class="w"> </span>MB +<a id="__codelineno-18-7" name="__codelineno-18-7" href="#__codelineno-18-7"></a>containous/traefik<span class="w"> </span>latest<span class="w"> </span>27b4e0c6b2fd<span class="w"> </span><span class="m">4</span><span class="w"> </span>days<span class="w"> </span>ago<span class="w"> </span><span class="m">20</span>.75<span class="w"> </span>MB +<a id="__codelineno-18-8" name="__codelineno-18-8" href="#__codelineno-18-8"></a>node<span class="w"> </span><span class="m">0</span>.10<span class="w"> </span>42426a5cba5f<span class="w"> </span><span class="m">6</span><span class="w"> </span>days<span class="w"> </span>ago<span class="w"> </span><span class="m">633</span>.7<span class="w"> </span>MB +<a id="__codelineno-18-9" name="__codelineno-18-9" href="#__codelineno-18-9"></a>redis<span class="w"> </span>latest<span class="w"> </span>4f5f397d4b7c<span class="w"> </span><span class="m">7</span><span class="w"> </span>days<span class="w"> </span>ago<span class="w"> </span><span class="m">177</span>.5<span class="w"> </span>MB +<a id="__codelineno-18-10" name="__codelineno-18-10" href="#__codelineno-18-10"></a>mongo<span class="w"> </span>latest<span class="w"> </span>467eb21035a8<span class="w"> </span><span class="m">7</span><span class="w"> </span>days<span class="w"> </span>ago<span class="w"> </span><span class="m">309</span>.7<span class="w"> </span>MB +<a id="__codelineno-18-11" name="__codelineno-18-11" href="#__codelineno-18-11"></a>alpine<span class="w"> </span><span class="m">3</span>.3<span class="w"> </span>70c557e50ed6<span class="w"> </span><span class="m">8</span><span class="w"> </span>days<span class="w"> </span>ago<span class="w"> </span><span class="m">4</span>.794<span class="w"> </span>MB +<a id="__codelineno-18-12" name="__codelineno-18-12" href="#__codelineno-18-12"></a>java<span class="w"> </span><span class="m">7</span><span class="w"> </span>21f6ce84e43c<span class="w"> </span><span class="m">8</span><span class="w"> </span>days<span class="w"> </span>ago<span class="w"> </span><span class="m">587</span>.7<span class="w"> </span>MB +</code></pre></div> +<p>Above is a list of images that I've pulled from the registry and those I've created myself (we'll shortly see how). You will have a different list of images on your machine. The <code>TAG</code> refers to a particular snapshot of the image and the <code>ID</code> is the corresponding unique identifier for that image.</p> +<p>For simplicity, you can think of an image akin to a git repository - images can be <a href="https://docs.docker.com/engine/reference/commandline/commit/" target="_blank">committed</a> with changes and have multiple. versions. When you do not provide a specific version number, the client defaults to latest.</p> +<p>For example you could pull a specific version of <code>ubuntu</code> image as follows:</p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-19-1" name="__codelineno-19-1" href="#__codelineno-19-1"></a>docker<span class="w"> </span>pull<span class="w"> </span>ubuntu:12.04 +</code></pre></div> +<p>If you do not specify the version number of the image then, as mentioned, the Docker client will default to a version named <code>latest</code>.</p> +<p>So for example, the <code>docker pull</code> command given below will pull an image named <code>ubuntu:latest</code> :</p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-20-1" name="__codelineno-20-1" href="#__codelineno-20-1"></a>docker<span class="w"> </span>pull<span class="w"> </span>ubuntu +</code></pre></div> +<p>To get a new Docker image you can either get it from a registry (such as the Docker Store) or create your own. There are hundreds of thousands of images available on <a href="https://store.docker.com/" target="_blank">Docker Store</a>. You can also search for images directly from the command line using <code>docker search</code>.</p> +<p>An important distinction with regard to images is between <em>base images</em> and <em>child images</em>. </p> +<ul> +<li>Base images are images that have no parent images, usually images with an OS like ubuntu, alpine or debian.</li> +<li>Child images are images that build on base images and add additional functionality. +Another key concept is the idea of official images and user images. (Both of which can be base images or child images.)</li> +<li>Official images are Docker sanctioned images. Docker, Inc. sponsors a dedicated team that is responsible for reviewing and publishing all Official Repositories content. This team works in collaboration with upstream software maintainers, security experts, and the broader Docker community. These are not prefixed by an organization or user name. In the list of images above, the <code>python</code>, <code>node</code>, <code>alpine</code> and <code>nginx</code> images are official (base) images. To find out more about them, check out the <a href="https://docs.docker.com/docker-hub/official_repos/" target="_blank">Official Images Documentation</a>. </li> +<li>User images are images created and shared by users like you. They build on base images and add additional functionality. Typically these are formatted as <code>user/image-name</code>. The <code>user</code> value in the image name is your Docker Store user or organization name.</li> +</ul> +<h2 id="23-create-your-first-image">2.3 Create your first image</h2> +<p>Now that you have a better understanding of images, it's time to create your own. Our main objective here is to create an image that sandboxes a small <a href="https://flask.palletsprojects.com" target="_blank">Flask</a> application.</p> +<p>The goal of this exercise is to create a Docker image which will run a Flask app.</p> +<p>We'll do this by first pulling together the components for a random cat picture generator built with Python Flask, then dockerizing it by writing a <em>Dockerfile</em>. Finally, we'll build the image, and then run it. </p> +<h2 id="231-create-a-python-flask-app-that-displays-random-cat-pix">2.3.1 Create a Python Flask app that displays random cat pix.</h2> +<p>For the purposes of this workshop, we've created a fun little Python Flask app that displays a random cat <code>.gif</code> every time it is loaded - because, you know, who doesn't like cats?</p> +<p>Start by creating a directory called <code>flask-app</code> where we'll create the following files: </p> +<ul> +<li>app.py </li> +<li>requirements.txt </li> +<li>templates/index.html </li> +<li>Dockerfile</li> +</ul> +<p>Make sure to <code>cd flask-app</code> before you start creating the files, because you don't want to start adding a whole bunch of other random files to your image.</p> +<h3 id="apppy">app.py</h3> +<p>Create the app.py with the following content:</p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-21-1" name="__codelineno-21-1" href="#__codelineno-21-1"></a><span class="kn">from</span><span class="w"> </span><span class="nn">flask</span><span class="w"> </span><span class="kn">import</span> <span class="n">Flask</span><span class="p">,</span> <span class="n">render_template</span> +<a id="__codelineno-21-2" name="__codelineno-21-2" href="#__codelineno-21-2"></a><span class="kn">import</span><span class="w"> </span><span class="nn">random</span> +<a id="__codelineno-21-3" name="__codelineno-21-3" href="#__codelineno-21-3"></a> +<a id="__codelineno-21-4" name="__codelineno-21-4" href="#__codelineno-21-4"></a><span class="n">app</span> <span class="o">=</span> <span class="n">Flask</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span> +<a id="__codelineno-21-5" name="__codelineno-21-5" href="#__codelineno-21-5"></a> +<a id="__codelineno-21-6" name="__codelineno-21-6" href="#__codelineno-21-6"></a><span class="c1"># list of cat images</span> +<a id="__codelineno-21-7" name="__codelineno-21-7" href="#__codelineno-21-7"></a><span class="n">images</span> <span class="o">=</span> <span class="p">[</span> +<a id="__codelineno-21-8" name="__codelineno-21-8" href="#__codelineno-21-8"></a><span class="s2">"https://c.tenor.com/GTcT7HODLRgAAAAM/smiling-cat-creepy-cat.gif"</span><span class="p">,</span> +<a id="__codelineno-21-9" name="__codelineno-21-9" href="#__codelineno-21-9"></a><span class="s2">"https://media0.giphy.com/media/10dU7AN7xsi1I4/giphy.webp?cid=ecf05e47gk63rd81vzlot57qmebr7drtgf6a3khmzvjsdtu7&rid=giphy.webp&ct=g"</span><span class="p">,</span> +<a id="__codelineno-21-10" name="__codelineno-21-10" href="#__codelineno-21-10"></a><span class="s2">"https://media0.giphy.com/media/S6VGjvmFRu5Qk/giphy.webp?cid=ecf05e478yofpawrhffnnvb3sgjkos96vyfo5mtqhds35as6&rid=giphy.webp&ct=g"</span><span class="p">,</span> +<a id="__codelineno-21-11" name="__codelineno-21-11" href="#__codelineno-21-11"></a><span class="s2">"https://media3.giphy.com/media/JIX9t2j0ZTN9S/200w.webp?cid=ecf05e47gk63rd81vzlot57qmebr7drtgf6a3khmzvjsdtu7&rid=200w.webp&ct=g"</span> +<a id="__codelineno-21-12" name="__codelineno-21-12" href="#__codelineno-21-12"></a> <span class="p">]</span> +<a id="__codelineno-21-13" name="__codelineno-21-13" href="#__codelineno-21-13"></a> +<a id="__codelineno-21-14" name="__codelineno-21-14" href="#__codelineno-21-14"></a><span class="nd">@app</span><span class="o">.</span><span class="n">route</span><span class="p">(</span><span class="s1">'/'</span><span class="p">)</span> +<a id="__codelineno-21-15" name="__codelineno-21-15" href="#__codelineno-21-15"></a><span class="k">def</span><span class="w"> </span><span class="nf">index</span><span class="p">():</span> +<a id="__codelineno-21-16" name="__codelineno-21-16" href="#__codelineno-21-16"></a> <span class="n">url</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">(</span><span class="n">images</span><span class="p">)</span> +<a id="__codelineno-21-17" name="__codelineno-21-17" href="#__codelineno-21-17"></a> <span class="k">return</span> <span class="n">render_template</span><span class="p">(</span><span class="s1">'index.html'</span><span class="p">,</span> <span class="n">url</span><span class="o">=</span><span class="n">url</span><span class="p">)</span> +<a id="__codelineno-21-18" name="__codelineno-21-18" href="#__codelineno-21-18"></a> +<a id="__codelineno-21-19" name="__codelineno-21-19" href="#__codelineno-21-19"></a><span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">"__main__"</span><span class="p">:</span> +<a id="__codelineno-21-20" name="__codelineno-21-20" href="#__codelineno-21-20"></a> <span class="n">app</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">host</span><span class="o">=</span><span class="s2">"0.0.0.0"</span><span class="p">)</span> +</code></pre></div> +<h3 id="requirementstxt">requirements.txt</h3> +<p>In order to install the Python modules required for our app, we need to create a file called requirements.txt and add the following line to that file:</p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-22-1" name="__codelineno-22-1" href="#__codelineno-22-1"></a>Flask==3.1.0 +</code></pre></div> +<h3 id="templatesindexhtml">templates/index.html</h3> +<p>Create a directory called <code>templates</code> and create an index.html file in that directory with the following content in it:</p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-23-1" name="__codelineno-23-1" href="#__codelineno-23-1"></a><span class="p"><</span><span class="nt">html</span><span class="p">></span> +<a id="__codelineno-23-2" name="__codelineno-23-2" href="#__codelineno-23-2"></a> <span class="p"><</span><span class="nt">head</span><span class="p">></span> +<a id="__codelineno-23-3" name="__codelineno-23-3" href="#__codelineno-23-3"></a> <span class="p"><</span><span class="nt">style</span> <span class="na">type</span><span class="o">=</span><span class="s">"text/css"</span><span class="p">></span> +<a id="__codelineno-23-4" name="__codelineno-23-4" href="#__codelineno-23-4"></a><span class="w"> </span><span class="nt">body</span><span class="w"> </span><span class="p">{</span> +<a id="__codelineno-23-5" name="__codelineno-23-5" href="#__codelineno-23-5"></a><span class="w"> </span><span class="k">background</span><span class="p">:</span><span class="w"> </span><span class="kc">black</span><span class="p">;</span> +<a id="__codelineno-23-6" name="__codelineno-23-6" href="#__codelineno-23-6"></a><span class="w"> </span><span class="k">color</span><span class="p">:</span><span class="w"> </span><span class="kc">white</span><span class="p">;</span> +<a id="__codelineno-23-7" name="__codelineno-23-7" href="#__codelineno-23-7"></a><span class="w"> </span><span class="p">}</span> +<a id="__codelineno-23-8" name="__codelineno-23-8" href="#__codelineno-23-8"></a><span class="w"> </span><span class="nt">div</span><span class="p">.</span><span class="nc">container</span><span class="w"> </span><span class="p">{</span> +<a id="__codelineno-23-9" name="__codelineno-23-9" href="#__codelineno-23-9"></a><span class="w"> </span><span class="k">max-width</span><span class="p">:</span><span class="w"> </span><span class="mi">500</span><span class="kt">px</span><span class="p">;</span> +<a id="__codelineno-23-10" name="__codelineno-23-10" href="#__codelineno-23-10"></a><span class="w"> </span><span class="k">margin</span><span class="p">:</span><span class="w"> </span><span class="mi">100</span><span class="kt">px</span><span class="w"> </span><span class="kc">auto</span><span class="p">;</span> +<a id="__codelineno-23-11" name="__codelineno-23-11" href="#__codelineno-23-11"></a><span class="w"> </span><span class="k">border</span><span class="p">:</span><span class="w"> </span><span class="mi">20</span><span class="kt">px</span><span class="w"> </span><span class="kc">solid</span><span class="w"> </span><span class="kc">white</span><span class="p">;</span> +<a id="__codelineno-23-12" name="__codelineno-23-12" href="#__codelineno-23-12"></a><span class="w"> </span><span class="k">padding</span><span class="p">:</span><span class="w"> </span><span class="mi">10</span><span class="kt">px</span><span class="p">;</span> +<a id="__codelineno-23-13" name="__codelineno-23-13" href="#__codelineno-23-13"></a><span class="w"> </span><span class="k">text-align</span><span class="p">:</span><span class="w"> </span><span class="kc">center</span><span class="p">;</span> +<a id="__codelineno-23-14" name="__codelineno-23-14" href="#__codelineno-23-14"></a><span class="w"> </span><span class="p">}</span> +<a id="__codelineno-23-15" name="__codelineno-23-15" href="#__codelineno-23-15"></a><span class="w"> </span><span class="nt">h4</span><span class="w"> </span><span class="p">{</span> +<a id="__codelineno-23-16" name="__codelineno-23-16" href="#__codelineno-23-16"></a><span class="w"> </span><span class="k">text-transform</span><span class="p">:</span><span class="w"> </span><span class="kc">uppercase</span><span class="p">;</span> +<a id="__codelineno-23-17" name="__codelineno-23-17" href="#__codelineno-23-17"></a><span class="w"> </span><span class="p">}</span> +<a id="__codelineno-23-18" name="__codelineno-23-18" href="#__codelineno-23-18"></a><span class="w"> </span><span class="p"></</span><span class="nt">style</span><span class="p">></span> +<a id="__codelineno-23-19" name="__codelineno-23-19" href="#__codelineno-23-19"></a> <span class="p"></</span><span class="nt">head</span><span class="p">></span> +<a id="__codelineno-23-20" name="__codelineno-23-20" href="#__codelineno-23-20"></a> <span class="p"><</span><span class="nt">body</span><span class="p">></span> +<a id="__codelineno-23-21" name="__codelineno-23-21" href="#__codelineno-23-21"></a> <span class="p"><</span><span class="nt">div</span> <span class="na">class</span><span class="o">=</span><span class="s">"container"</span><span class="p">></span> +<a id="__codelineno-23-22" name="__codelineno-23-22" href="#__codelineno-23-22"></a> <span class="p"><</span><span class="nt">h4</span><span class="p">></span>Cat Gif of the day<span class="p"></</span><span class="nt">h4</span><span class="p">></span> +<a id="__codelineno-23-23" name="__codelineno-23-23" href="#__codelineno-23-23"></a> <span class="p"><</span><span class="nt">img</span> <span class="na">src</span><span class="o">=</span><span class="s">"{{url}}"</span> <span class="p">/></span> +<a id="__codelineno-23-24" name="__codelineno-23-24" href="#__codelineno-23-24"></a> <span class="p"><</span><span class="nt">p</span><span class="p">><</span><span class="nt">small</span><span class="p">></span>Courtesy: <span class="p"><</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">"http://www.buzzfeed.com/copyranter/the-best-cat-gif-post-in-the-history-of-cat-gifs"</span><span class="p">></span>Buzzfeed<span class="p"></</span><span class="nt">a</span><span class="p">></</span><span class="nt">small</span><span class="p">></</span><span class="nt">p</span><span class="p">></span> +<a id="__codelineno-23-25" name="__codelineno-23-25" href="#__codelineno-23-25"></a> <span class="p"></</span><span class="nt">div</span><span class="p">></span> +<a id="__codelineno-23-26" name="__codelineno-23-26" href="#__codelineno-23-26"></a> <span class="p"></</span><span class="nt">body</span><span class="p">></span> +<a id="__codelineno-23-27" name="__codelineno-23-27" href="#__codelineno-23-27"></a><span class="p"></</span><span class="nt">html</span><span class="p">></span> +</code></pre></div> +<h2 id="232-write-a-dockerfile">2.3.2 Write a Dockerfile</h2> +<p>We want to create a Docker image with this web app. As mentioned above, all user images are based on a base image.<br /> +Since our application is written in Python, we will build our own Python image based on <a href="https://hub.docker.com/_/alpine" target="_blank">Alpine</a>. +We'll do that using a Dockerfile.</p> +<p>A <a href="https://docs.docker.com/engine/reference/builder/" target="_blank">Dockerfile</a> is a text file that contains a list of commands that the Docker daemon calls while creating an image. The Dockerfile contains all the information that Docker needs to know to run the app — a base Docker image to run from, location of your project code, any dependencies it has, and what commands to run at start-up.<br /> +It is a simple way to automate the image creation process. The best part is that the commands you write in a Dockerfile are almost identical to their equivalent Linux commands. This means you don't really have to learn new syntax to create your own Dockerfiles.</p> +<p>1 - Create a file called Dockerfile, and add content to it as described below.</p> +<p>We'll start by specifying our base image, using the <code>FROM</code> keyword. We are using alpine:3.21.0, a lightweight Linux distribution that helps keep our container small and efficient:</p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-24-1" name="__codelineno-24-1" href="#__codelineno-24-1"></a><span class="k">FROM</span><span class="w"> </span><span class="s">alpine:3.21.0</span> +</code></pre></div> +<p>2 - Next, we need to install Python 3, pip, and other system dependencies required for our application. The apk add command is used to install packages in Alpine Linux. We use --no-cache to prevent unnecessary image bloat. Add the following <a href="https://docs.docker.com/engine/reference/builder/#run" target="_blank">RUN</a> command:</p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-25-1" name="__codelineno-25-1" href="#__codelineno-25-1"></a><span class="k">RUN</span><span class="w"> </span>apk<span class="w"> </span>add<span class="w"> </span>--no-cache<span class="w"> </span>build-base<span class="w"> </span>libffi-dev<span class="w"> </span>openssl-dev<span class="w"> </span>py3-pip<span class="w"> </span>python3 +</code></pre></div> +<p>3 - Now, we set the working directory inside the container. This ensures that all subsequent commands run within this directory:</p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-26-1" name="__codelineno-26-1" href="#__codelineno-26-1"></a><span class="k">WORKDIR</span><span class="w"> </span><span class="s">/usr/src/app</span> +</code></pre></div> +<p>4 - To create an isolated Python environment, we set up a virtual environment inside our container. This helps prevent conflicts between system-wide and project-specific dependencies:</p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-27-1" name="__codelineno-27-1" href="#__codelineno-27-1"></a><span class="k">RUN</span><span class="w"> </span>python3<span class="w"> </span>-m<span class="w"> </span>venv<span class="w"> </span>venv +</code></pre></div> +<p>5 - To ensure that all commands within the container use the virtual environment by default, we modify the PATH environment variable:</p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-28-1" name="__codelineno-28-1" href="#__codelineno-28-1"></a><span class="k">ENV</span><span class="w"> </span><span class="nv">PATH</span><span class="o">=</span><span class="s2">"/usr/src/app/venv/bin:</span><span class="nv">$PATH</span><span class="s2">"</span> +</code></pre></div> +<p>6 - Next, we copy the application's dependencies file (requirements.txt) into the container and install the necessary Python packages. We also upgrade pip to the latest version to ensure compatibility:</p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-29-1" name="__codelineno-29-1" href="#__codelineno-29-1"></a><span class="k">COPY</span><span class="w"> </span>requirements.txt<span class="w"> </span>./ +<a id="__codelineno-29-2" name="__codelineno-29-2" href="#__codelineno-29-2"></a><span class="k">RUN</span><span class="w"> </span>pip<span class="w"> </span>install<span class="w"> </span>--no-cache-dir<span class="w"> </span>--upgrade<span class="w"> </span>pip<span class="w"> </span><span class="o">&&</span><span class="w"> </span>pip<span class="w"> </span>install<span class="w"> </span>--no-cache-dir<span class="w"> </span>-r<span class="w"> </span>requirements.txt +</code></pre></div> +<p>7 - Copy the files you have created earlier into our image by using <a href="https://docs.docker.com/engine/reference/builder/#copy" target="_blank">COPY</a> command.</p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-30-1" name="__codelineno-30-1" href="#__codelineno-30-1"></a><span class="k">COPY</span><span class="w"> </span>app.py<span class="w"> </span>./ +<a id="__codelineno-30-2" name="__codelineno-30-2" href="#__codelineno-30-2"></a><span class="k">COPY</span><span class="w"> </span>templates/index.html<span class="w"> </span>./templates/ +</code></pre></div> +<p>8 - Since our Flask application runs on port 5000, we specify that this port should be exposed. This does not automatically publish the port but serves as documentation and can be used by orchestration tools:</p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-31-1" name="__codelineno-31-1" href="#__codelineno-31-1"></a><span class="k">EXPOSE</span><span class="w"> </span><span class="s">5000</span> +</code></pre></div> +<p>9 - The last step is the command for running the application which is simply - <code>python ./app.py</code>. <br /> +Use the <a href="https://docs.docker.com/engine/reference/builder/#cmd" target="_blank">CMD</a> command to do that:</p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-32-1" name="__codelineno-32-1" href="#__codelineno-32-1"></a><span class="k">CMD</span><span class="w"> </span><span class="p">[</span><span class="s2">"python"</span><span class="p">,</span><span class="w"> </span><span class="s2">"/usr/src/app/app.py"</span><span class="p">]</span> +</code></pre></div> +<p>The primary purpose of <code>CMD</code> is to tell the container which command it should run by default when it is started.</p> +<p>10 - Verify your Dockerfile.</p> +<p>Our <code>Dockerfile</code> is now ready. This is how it looks:</p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-33-1" name="__codelineno-33-1" href="#__codelineno-33-1"></a><span class="c"># our base image</span> +<a id="__codelineno-33-2" name="__codelineno-33-2" href="#__codelineno-33-2"></a><span class="k">FROM</span><span class="w"> </span><span class="s">alpine:3.21.0</span> +<a id="__codelineno-33-3" name="__codelineno-33-3" href="#__codelineno-33-3"></a> +<a id="__codelineno-33-4" name="__codelineno-33-4" href="#__codelineno-33-4"></a><span class="c"># Install Python 3, pip, and system dependencies</span> +<a id="__codelineno-33-5" name="__codelineno-33-5" href="#__codelineno-33-5"></a><span class="k">RUN</span><span class="w"> </span>apk<span class="w"> </span>add<span class="w"> </span>--no-cache<span class="w"> </span>build-base<span class="w"> </span>libffi-dev<span class="w"> </span>openssl-dev<span class="w"> </span>py3-pip<span class="w"> </span>python3 +<a id="__codelineno-33-6" name="__codelineno-33-6" href="#__codelineno-33-6"></a> +<a id="__codelineno-33-7" name="__codelineno-33-7" href="#__codelineno-33-7"></a><span class="c"># Set the working directory</span> +<a id="__codelineno-33-8" name="__codelineno-33-8" href="#__codelineno-33-8"></a><span class="k">WORKDIR</span><span class="w"> </span><span class="s">/usr/src/app</span> +<a id="__codelineno-33-9" name="__codelineno-33-9" href="#__codelineno-33-9"></a> +<a id="__codelineno-33-10" name="__codelineno-33-10" href="#__codelineno-33-10"></a><span class="c"># Create and activate a virtual environment</span> +<a id="__codelineno-33-11" name="__codelineno-33-11" href="#__codelineno-33-11"></a><span class="k">RUN</span><span class="w"> </span>python3<span class="w"> </span>-m<span class="w"> </span>venv<span class="w"> </span>venv +<a id="__codelineno-33-12" name="__codelineno-33-12" href="#__codelineno-33-12"></a> +<a id="__codelineno-33-13" name="__codelineno-33-13" href="#__codelineno-33-13"></a><span class="c"># Use the virtual environment for all commands</span> +<a id="__codelineno-33-14" name="__codelineno-33-14" href="#__codelineno-33-14"></a><span class="k">ENV</span><span class="w"> </span><span class="nv">PATH</span><span class="o">=</span><span class="s2">"/usr/src/app/venv/bin:</span><span class="nv">$PATH</span><span class="s2">"</span> +<a id="__codelineno-33-15" name="__codelineno-33-15" href="#__codelineno-33-15"></a> +<a id="__codelineno-33-16" name="__codelineno-33-16" href="#__codelineno-33-16"></a><span class="c"># Copy and install dependencies</span> +<a id="__codelineno-33-17" name="__codelineno-33-17" href="#__codelineno-33-17"></a><span class="k">COPY</span><span class="w"> </span>requirements.txt<span class="w"> </span>./ +<a id="__codelineno-33-18" name="__codelineno-33-18" href="#__codelineno-33-18"></a><span class="k">RUN</span><span class="w"> </span>pip<span class="w"> </span>install<span class="w"> </span>--no-cache-dir<span class="w"> </span>--upgrade<span class="w"> </span>pip<span class="w"> </span><span class="o">&&</span><span class="w"> </span>pip<span class="w"> </span>install<span class="w"> </span>--no-cache-dir<span class="w"> </span>-r<span class="w"> </span>requirements.txt +<a id="__codelineno-33-19" name="__codelineno-33-19" href="#__codelineno-33-19"></a> +<a id="__codelineno-33-20" name="__codelineno-33-20" href="#__codelineno-33-20"></a><span class="c"># Copy application files</span> +<a id="__codelineno-33-21" name="__codelineno-33-21" href="#__codelineno-33-21"></a><span class="k">COPY</span><span class="w"> </span>app.py<span class="w"> </span>./ +<a id="__codelineno-33-22" name="__codelineno-33-22" href="#__codelineno-33-22"></a><span class="k">COPY</span><span class="w"> </span>templates/index.html<span class="w"> </span>./templates/ +<a id="__codelineno-33-23" name="__codelineno-33-23" href="#__codelineno-33-23"></a> +<a id="__codelineno-33-24" name="__codelineno-33-24" href="#__codelineno-33-24"></a><span class="c"># Expose the application port</span> +<a id="__codelineno-33-25" name="__codelineno-33-25" href="#__codelineno-33-25"></a><span class="k">EXPOSE</span><span class="w"> </span><span class="s">5000</span> +<a id="__codelineno-33-26" name="__codelineno-33-26" href="#__codelineno-33-26"></a> +<a id="__codelineno-33-27" name="__codelineno-33-27" href="#__codelineno-33-27"></a><span class="c"># Run the application inside the virtual environment</span> +<a id="__codelineno-33-28" name="__codelineno-33-28" href="#__codelineno-33-28"></a><span class="k">CMD</span><span class="w"> </span><span class="p">[</span><span class="s2">"python"</span><span class="p">,</span><span class="w"> </span><span class="s2">"/usr/src/app/app.py"</span><span class="p">]</span> +</code></pre></div> +<h2 id="233-build-the-image">2.3.3 Build the image</h2> +<p>Now that you have your <code>Dockerfile</code>, you can build your image.<br /> +The <code>docker build</code> command does the heavy-lifting of creating a docker image from a <code>Dockerfile</code>.</p> +<p>When you run the <code>docker build</code> command given below, make sure to replace <code><YOUR_USERNAME></code> with your username.<br /> +This username should be the same one you created when registering on <a href="https://cloud.docker.com/" target="_blank">Docker Cloud</a>. If you haven't done that yet, please go ahead and create an account.</p> +<p>The <code>docker build</code> command is quite simple - it takes an optional tag name with the <code>-t</code> flag, and the location of the directory containing the <code>Dockerfile</code> - the . indicates the current directory:</p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-34-1" name="__codelineno-34-1" href="#__codelineno-34-1"></a>docker<span class="w"> </span>build<span class="w"> </span>-t<span class="w"> </span><YOUR_USERNAME>/myfirstapp<span class="w"> </span>. +</code></pre></div> +<p>If you don't have the <code>alpine:3.21.0</code> image, the client will first pull the image and then create your image. Therefore, your output on running the command will look different from mine. If everything went well, your image should be ready! Run <code>docker images</code> and see if your image (<code><YOUR_USERNAME>/myfirstapp</code>) shows.</p> +<h2 id="234-run-your-image">2.3.4 Run your image</h2> +<p>The next step in this section is to run the image and see if it actually works.</p> +<p><div class="highlight"><pre><span></span><code><a id="__codelineno-35-1" name="__codelineno-35-1" href="#__codelineno-35-1"></a>docker<span class="w"> </span>run<span class="w"> </span>-p<span class="w"> </span><span class="m">8888</span>:5000<span class="w"> </span>--name<span class="w"> </span>myfirstapp<span class="w"> </span>YOUR_USERNAME/myfirstapp +<a id="__codelineno-35-2" name="__codelineno-35-2" href="#__codelineno-35-2"></a><span class="w"> </span>*<span class="w"> </span>Serving<span class="w"> </span>Flask<span class="w"> </span>app<span class="w"> </span><span class="s1">'app'</span> +<a id="__codelineno-35-3" name="__codelineno-35-3" href="#__codelineno-35-3"></a><span class="w"> </span>*<span class="w"> </span>Debug<span class="w"> </span>mode:<span class="w"> </span>off +<a id="__codelineno-35-4" name="__codelineno-35-4" href="#__codelineno-35-4"></a>WARNING:<span class="w"> </span>This<span class="w"> </span>is<span class="w"> </span>a<span class="w"> </span>development<span class="w"> </span>server.<span class="w"> </span>Do<span class="w"> </span>not<span class="w"> </span>use<span class="w"> </span>it<span class="w"> </span><span class="k">in</span><span class="w"> </span>a<span class="w"> </span>production<span class="w"> </span>deployment.<span class="w"> </span>Use<span class="w"> </span>a<span class="w"> </span>production<span class="w"> </span>WSGI<span class="w"> </span>server<span class="w"> </span>instead. +<a id="__codelineno-35-5" name="__codelineno-35-5" href="#__codelineno-35-5"></a><span class="w"> </span>*<span class="w"> </span>Running<span class="w"> </span>on<span class="w"> </span>all<span class="w"> </span>addresses<span class="w"> </span><span class="o">(</span><span class="m">0</span>.0.0.0<span class="o">)</span> +<a id="__codelineno-35-6" name="__codelineno-35-6" href="#__codelineno-35-6"></a><span class="w"> </span>*<span class="w"> </span>Running<span class="w"> </span>on<span class="w"> </span>http://127.0.0.1:5000 +<a id="__codelineno-35-7" name="__codelineno-35-7" href="#__codelineno-35-7"></a><span class="w"> </span>*<span class="w"> </span>Running<span class="w"> </span>on<span class="w"> </span>http://172.17.0.2:5000 +<a id="__codelineno-35-8" name="__codelineno-35-8" href="#__codelineno-35-8"></a>Press<span class="w"> </span>CTRL+C<span class="w"> </span>to<span class="w"> </span>quit +</code></pre></div> +Head over to <a href="http://localhost:8888" target="_blank">http://localhost:8888</a> and your app should be live. Note If you are using Docker Machine, you may need to open up another terminal and determine the container ip address using <code>docker-machine ip default</code>.</p> +<p><img alt="gif_cat" src="../assets/Gif-cat.png" /></p> +<p>Hit the Refresh button in the web browser to see a few more cat images.</p> +<div class="admonition check"> +<p class="admonition-title">Check</p> +<p>Show us your running flask-app !</p> +</div> +<h2 id="234-dockerfile-commands-summary">2.3.4 Dockerfile commands summary</h2> +<p>Here's a quick summary of the few basic commands we used in our Dockerfile.</p> +<ul> +<li><code>FROM</code> starts the Dockerfile. It is a requirement that the Dockerfile must start with the <code>FROM</code> command. Images are created in layers, which means you can use another image as the base image for your own. The <code>FROM</code> command defines your base layer. As arguments, it takes the name of the image. Optionally, you can add the Docker Cloud username of the maintainer and image version, in the format <code>username/imagename:version</code>.</li> +<li><code>RUN</code> is used to build up the Image you're creating. For each <code>RUN</code> command, Docker will run the command then create a new layer of the image. This way you can roll back your image to previous states easily. The syntax for a <code>RUN</code> instruction is to place the full text of the shell command after the <code>RUN</code> (e.g., <code>RUN mkdir /user/local/foo</code>). This will automatically run in a /bin/sh shell. You can define a different shell like this: <code>RUN /bin/bash -c 'mkdir /user/local/foo</code>'</li> +<li><code>COPY</code> copies local files into the container.</li> +<li><code>CMD</code> defines the commands that will run on the Image at start-up. Unlike a <code>RUN</code>, this does not create a new layer for the Image, but simply runs the command. There can only be one <code>CMD</code> per a Dockerfile/Image. If you need to run multiple commands, the best way to do that is to have the <code>CMD</code> run a script. <code>CMD</code> requires that you tell it where to run the command, unlike <code>RUN</code>. So example <code>CMD</code> commands would be:</li> +</ul> +<div class="highlight"><pre><span></span><code><a id="__codelineno-36-1" name="__codelineno-36-1" href="#__codelineno-36-1"></a><span class="w"> </span>CMD<span class="w"> </span><span class="o">[</span><span class="s2">"python"</span>,<span class="w"> </span><span class="s2">"./app.py"</span><span class="o">]</span> +<a id="__codelineno-36-2" name="__codelineno-36-2" href="#__codelineno-36-2"></a><span class="w"> </span>CMD<span class="w"> </span><span class="o">[</span><span class="s2">"/bin/bash"</span>,<span class="w"> </span><span class="s2">"echo"</span>,<span class="w"> </span><span class="s2">"Hello World"</span><span class="o">]</span> +</code></pre></div> +<ul> +<li><code>EXPOSE</code> creates a hint for users of an image which ports provide services. It is included in the information which can be retrieved via <code>docker inspect <container-id></code>.</li> +</ul> +<div class="admonition note"> +<p class="admonition-title">Note</p> +<p>The <code>EXPOSE</code> command <strong>does not actually make any ports accessible to the host!</strong> Instead, this requires publishing ports by means of the <code>-p</code> flag when using <code>docker run</code>. </p> +</div> +<div class="admonition note"> +<p class="admonition-title">Note</p> +<p>If you want to learn more about Dockerfiles, check out <a href="https://docs.docker.com/develop/develop-images/dockerfile_best-practices/" target="_blank">Best practices for writing Dockerfiles</a>.</p> +</div> +<p>(source: <a href="https://github.com/docker/labs/tree/master/beginner">https://github.com/docker/labs/tree/master/beginner</a>)</p> +<p>Now that you know how to run docker container and create Dockerfiles let’s move on to the practical part.</p> + + + </article> + </div> + </div> + + </main> + + <footer class="md-footer"> + + <nav class="md-footer__inner md-grid" aria-label="Footer"> + + + <a href="../ch1-discover-docker-tp/" class="md-footer__link md-footer__link--prev" aria-label="Previous: TP part 01 - Docker" rel="prev"> + <div class="md-footer__button md-icon"> + <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> + </div> + <div class="md-footer__title"> + <div class="md-ellipsis"> + <span class="md-footer__direction"> + Previous + </span> + TP part 01 - Docker + </div> + </div> + </a> + + + </nav> + + <div class="md-footer-meta md-typeset"> + <div class="md-footer-meta__inner md-grid"> + <div class="md-copyright"> + + + Made with + <a href="https://squidfunk.github.io/mkdocs-material/" target="_blank" rel="noopener"> + Material for MkDocs + </a> + +</div> + + </div> + </div> +</footer> + + </div> + <div class="md-dialog" data-md-component="dialog"> + <div class="md-dialog__inner md-typeset"></div> + </div> + <script id="__config" type="application/json">{"base": "..", "features": ["navigation.tabs", "navigation.instant"], "search": "../assets/javascripts/workers/search.2a1c317c.min.js", "translations": {"clipboard.copied": "Copied to clipboard", "clipboard.copy": "Copy to clipboard", "search.config.lang": "en", "search.config.pipeline": "trimmer, stopWordFilter", "search.config.separator": "[\\s\\-]+", "search.placeholder": "Search", "search.result.more.one": "1 more on this page", "search.result.more.other": "# more on this page", "search.result.none": "No matching documents", "search.result.one": "1 matching document", "search.result.other": "# matching documents", "search.result.placeholder": "Type to start searching", "search.result.term.missing": "Missing", "select.version.title": "Select version"}}</script> + + + <script src="../assets/javascripts/bundle.a6c66575.min.js"></script> + + + </body> +</html> \ No newline at end of file diff --git a/public/ch1-discover-docker-tp/index.html b/public/ch1-discover-docker-tp/index.html new file mode 100644 index 0000000..9391074 --- /dev/null +++ b/public/ch1-discover-docker-tp/index.html @@ -0,0 +1,1187 @@ + +<!doctype html> +<html lang="en" class="no-js"> + <head> + + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width,initial-scale=1"> + + + + <link rel="icon" href="../assets/images/favicon.png"> + <meta name="generator" content="mkdocs-1.3.0, mkdocs-material-8.2.15"> + + + + <title>TP part 01 - Docker - Devops</title> + + + + <link rel="stylesheet" href="../assets/stylesheets/main.c382b1dc.min.css"> + + + <link rel="stylesheet" href="../assets/stylesheets/palette.cc9b2e1e.min.css"> + + + + <meta name="theme-color" content="#e92063"> + + + + + + + + + <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> + <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,700,700i%7CRoboto+Mono:400,400i,700,700i&display=fallback"> + <style>:root{--md-text-font:"Roboto";--md-code-font:"Roboto Mono"}</style> + + + + <script>__md_scope=new URL("..",location),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script> + + + + + + </head> + + + + + + + + <body dir="ltr" data-md-color-scheme="" data-md-color-primary="pink" data-md-color-accent=""> + + + + <input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off"> + <input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off"> + <label class="md-overlay" for="__drawer"></label> + <div data-md-component="skip"> + + + <a href="#discover-docker" class="md-skip"> + Skip to content + </a> + + </div> + <div data-md-component="announce"> + + </div> + + + + +<header class="md-header" data-md-component="header"> + <nav class="md-header__inner md-grid" aria-label="Header"> + <a href=".." title="Devops" class="md-header__button md-logo" aria-label="Devops" data-md-component="logo"> + + <img src="../assets/logo.png" alt="logo"> + + </a> + <label class="md-header__button md-icon" for="__drawer"> + <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M3 6h18v2H3V6m0 5h18v2H3v-2m0 5h18v2H3v-2Z"/></svg> + </label> + <div class="md-header__title" data-md-component="header-title"> + <div class="md-header__ellipsis"> + <div class="md-header__topic"> + <span class="md-ellipsis"> + Devops + </span> + </div> + <div class="md-header__topic" data-md-component="header-topic"> + <span class="md-ellipsis"> + + TP part 01 - Docker + + </span> + </div> + </div> + </div> + + + + <label class="md-header__button md-icon" for="__search"> + <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5Z"/></svg> + </label> + <div class="md-search" data-md-component="search" role="dialog"> + <label class="md-search__overlay" for="__search"></label> + <div class="md-search__inner" role="search"> + <form class="md-search__form" name="search"> + <input type="text" class="md-search__input" name="query" aria-label="Search" placeholder="Search" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck="false" data-md-component="search-query" required> + <label class="md-search__icon md-icon" for="__search"> + <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5Z"/></svg> + <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> + </label> + <nav class="md-search__options" aria-label="Search"> + + <button type="reset" class="md-search__icon md-icon" aria-label="Clear" tabindex="-1"> + <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19 6.41 17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12 19 6.41Z"/></svg> + </button> + </nav> + + </form> + <div class="md-search__output"> + <div class="md-search__scrollwrap" data-md-scrollfix> + <div class="md-search-result" data-md-component="search-result"> + <div class="md-search-result__meta"> + Initializing search + </div> + <ol class="md-search-result__list"></ol> + </div> + </div> + </div> + </div> +</div> + + + </nav> + +</header> + + <div class="md-container" data-md-component="container"> + + + + + +<nav class="md-tabs" aria-label="Tabs" data-md-component="tabs"> + <div class="md-tabs__inner md-grid"> + <ul class="md-tabs__list"> + + + + + + + + + + + + <li class="md-tabs__item"> + <a href="./" class="md-tabs__link md-tabs__link--active"> + TP + </a> + </li> + + + + + + + + + + + + <li class="md-tabs__item"> + <a href="../ch1-discover-docker-td/" class="md-tabs__link"> + TD + </a> + </li> + + + + </ul> + </div> +</nav> + + + + <main class="md-main" data-md-component="main"> + <div class="md-main__inner md-grid"> + + + + <div class="md-sidebar md-sidebar--primary" data-md-component="sidebar" data-md-type="navigation" > + <div class="md-sidebar__scrollwrap"> + <div class="md-sidebar__inner"> + + + + + +<nav class="md-nav md-nav--primary md-nav--lifted" aria-label="Navigation" data-md-level="0"> + <label class="md-nav__title" for="__drawer"> + <a href=".." title="Devops" class="md-nav__button md-logo" aria-label="Devops" data-md-component="logo"> + + <img src="../assets/logo.png" alt="logo"> + + </a> + Devops + </label> + + <ul class="md-nav__list" data-md-scrollfix> + + + + + + + + + + + + <li class="md-nav__item md-nav__item--active md-nav__item--nested"> + + + <input class="md-nav__toggle md-toggle" data-md-toggle="__nav_1" type="checkbox" id="__nav_1" checked> + + + + + <label class="md-nav__link" for="__nav_1"> + TP + <span class="md-nav__icon md-icon"></span> + </label> + + <nav class="md-nav" aria-label="TP" data-md-level="1"> + <label class="md-nav__title" for="__nav_1"> + <span class="md-nav__icon md-icon"></span> + TP + </label> + <ul class="md-nav__list" data-md-scrollfix> + + + + + + + + + <li class="md-nav__item md-nav__item--active"> + + <input class="md-nav__toggle md-toggle" data-md-toggle="toc" type="checkbox" id="__toc"> + + + + + + <label class="md-nav__link md-nav__link--active" for="__toc"> + TP part 01 - Docker + <span class="md-nav__icon md-icon"></span> + </label> + + <a href="./" class="md-nav__link md-nav__link--active"> + TP part 01 - Docker + </a> + + + +<nav class="md-nav md-nav--secondary" aria-label="Table of contents"> + + + + + + + <label class="md-nav__title" for="__toc"> + <span class="md-nav__icon md-icon"></span> + Table of contents + </label> + <ul class="md-nav__list" data-md-component="toc" data-md-scrollfix> + + <li class="md-nav__item"> + <a href="#goals" class="md-nav__link"> + Goals + </a> + + <nav class="md-nav" aria-label="Goals"> + <ul class="md-nav__list"> + + <li class="md-nav__item"> + <a href="#good-practice" class="md-nav__link"> + Good practice + </a> + +</li> + + <li class="md-nav__item"> + <a href="#target-application" class="md-nav__link"> + Target application + </a> + +</li> + + <li class="md-nav__item"> + <a href="#base-images" class="md-nav__link"> + Base images + </a> + +</li> + + </ul> + </nav> + +</li> + + <li class="md-nav__item"> + <a href="#database" class="md-nav__link"> + Database + </a> + + <nav class="md-nav" aria-label="Database"> + <ul class="md-nav__list"> + + <li class="md-nav__item"> + <a href="#basics" class="md-nav__link"> + Basics + </a> + +</li> + + <li class="md-nav__item"> + <a href="#init-database" class="md-nav__link"> + Init database + </a> + +</li> + + <li class="md-nav__item"> + <a href="#persist-data" class="md-nav__link"> + Persist data + </a> + +</li> + + </ul> + </nav> + +</li> + + <li class="md-nav__item"> + <a href="#backend-api" class="md-nav__link"> + Backend API + </a> + + <nav class="md-nav" aria-label="Backend API"> + <ul class="md-nav__list"> + + <li class="md-nav__item"> + <a href="#basics_1" class="md-nav__link"> + Basics + </a> + +</li> + + <li class="md-nav__item"> + <a href="#multistage-build" class="md-nav__link"> + Multistage build + </a> + + <nav class="md-nav" aria-label="Multistage build"> + <ul class="md-nav__list"> + + <li class="md-nav__item"> + <a href="#backend-simple-api" class="md-nav__link"> + Backend simple api + </a> + +</li> + + <li class="md-nav__item"> + <a href="#backend-api_1" class="md-nav__link"> + Backend API + </a> + +</li> + + </ul> + </nav> + +</li> + + </ul> + </nav> + +</li> + + <li class="md-nav__item"> + <a href="#http-server" class="md-nav__link"> + Http server + </a> + + <nav class="md-nav" aria-label="Http server"> + <ul class="md-nav__list"> + + <li class="md-nav__item"> + <a href="#basics_2" class="md-nav__link"> + Basics + </a> + +</li> + + <li class="md-nav__item"> + <a href="#choose-an-appropriate-base-image" class="md-nav__link"> + Choose an appropriate base image. + </a> + +</li> + + <li class="md-nav__item"> + <a href="#configuration" class="md-nav__link"> + Configuration + </a> + +</li> + + <li class="md-nav__item"> + <a href="#reverse-proxy" class="md-nav__link"> + Reverse proxy + </a> + +</li> + + </ul> + </nav> + +</li> + + <li class="md-nav__item"> + <a href="#link-application" class="md-nav__link"> + Link application + </a> + + <nav class="md-nav" aria-label="Link application"> + <ul class="md-nav__list"> + + <li class="md-nav__item"> + <a href="#docker-compose" class="md-nav__link"> + Docker-compose + </a> + +</li> + + </ul> + </nav> + +</li> + + <li class="md-nav__item"> + <a href="#publish" class="md-nav__link"> + Publish + </a> + +</li> + + </ul> + +</nav> + + </li> + + + + + </ul> + </nav> + </li> + + + + + + + + + + + + <li class="md-nav__item md-nav__item--nested"> + + + <input class="md-nav__toggle md-toggle" data-md-toggle="__nav_2" type="checkbox" id="__nav_2" > + + + + + <label class="md-nav__link" for="__nav_2"> + TD + <span class="md-nav__icon md-icon"></span> + </label> + + <nav class="md-nav" aria-label="TD" data-md-level="1"> + <label class="md-nav__title" for="__nav_2"> + <span class="md-nav__icon md-icon"></span> + TD + </label> + <ul class="md-nav__list" data-md-scrollfix> + + + + + + + <li class="md-nav__item"> + <a href="../ch1-discover-docker-td/" class="md-nav__link"> + TD part 01 - Docker + </a> + </li> + + + + + </ul> + </nav> + </li> + + + + </ul> +</nav> + </div> + </div> + </div> + + + + <div class="md-sidebar md-sidebar--secondary" data-md-component="sidebar" data-md-type="toc" > + <div class="md-sidebar__scrollwrap"> + <div class="md-sidebar__inner"> + + +<nav class="md-nav md-nav--secondary" aria-label="Table of contents"> + + + + + + + <label class="md-nav__title" for="__toc"> + <span class="md-nav__icon md-icon"></span> + Table of contents + </label> + <ul class="md-nav__list" data-md-component="toc" data-md-scrollfix> + + <li class="md-nav__item"> + <a href="#goals" class="md-nav__link"> + Goals + </a> + + <nav class="md-nav" aria-label="Goals"> + <ul class="md-nav__list"> + + <li class="md-nav__item"> + <a href="#good-practice" class="md-nav__link"> + Good practice + </a> + +</li> + + <li class="md-nav__item"> + <a href="#target-application" class="md-nav__link"> + Target application + </a> + +</li> + + <li class="md-nav__item"> + <a href="#base-images" class="md-nav__link"> + Base images + </a> + +</li> + + </ul> + </nav> + +</li> + + <li class="md-nav__item"> + <a href="#database" class="md-nav__link"> + Database + </a> + + <nav class="md-nav" aria-label="Database"> + <ul class="md-nav__list"> + + <li class="md-nav__item"> + <a href="#basics" class="md-nav__link"> + Basics + </a> + +</li> + + <li class="md-nav__item"> + <a href="#init-database" class="md-nav__link"> + Init database + </a> + +</li> + + <li class="md-nav__item"> + <a href="#persist-data" class="md-nav__link"> + Persist data + </a> + +</li> + + </ul> + </nav> + +</li> + + <li class="md-nav__item"> + <a href="#backend-api" class="md-nav__link"> + Backend API + </a> + + <nav class="md-nav" aria-label="Backend API"> + <ul class="md-nav__list"> + + <li class="md-nav__item"> + <a href="#basics_1" class="md-nav__link"> + Basics + </a> + +</li> + + <li class="md-nav__item"> + <a href="#multistage-build" class="md-nav__link"> + Multistage build + </a> + + <nav class="md-nav" aria-label="Multistage build"> + <ul class="md-nav__list"> + + <li class="md-nav__item"> + <a href="#backend-simple-api" class="md-nav__link"> + Backend simple api + </a> + +</li> + + <li class="md-nav__item"> + <a href="#backend-api_1" class="md-nav__link"> + Backend API + </a> + +</li> + + </ul> + </nav> + +</li> + + </ul> + </nav> + +</li> + + <li class="md-nav__item"> + <a href="#http-server" class="md-nav__link"> + Http server + </a> + + <nav class="md-nav" aria-label="Http server"> + <ul class="md-nav__list"> + + <li class="md-nav__item"> + <a href="#basics_2" class="md-nav__link"> + Basics + </a> + +</li> + + <li class="md-nav__item"> + <a href="#choose-an-appropriate-base-image" class="md-nav__link"> + Choose an appropriate base image. + </a> + +</li> + + <li class="md-nav__item"> + <a href="#configuration" class="md-nav__link"> + Configuration + </a> + +</li> + + <li class="md-nav__item"> + <a href="#reverse-proxy" class="md-nav__link"> + Reverse proxy + </a> + +</li> + + </ul> + </nav> + +</li> + + <li class="md-nav__item"> + <a href="#link-application" class="md-nav__link"> + Link application + </a> + + <nav class="md-nav" aria-label="Link application"> + <ul class="md-nav__list"> + + <li class="md-nav__item"> + <a href="#docker-compose" class="md-nav__link"> + Docker-compose + </a> + +</li> + + </ul> + </nav> + +</li> + + <li class="md-nav__item"> + <a href="#publish" class="md-nav__link"> + Publish + </a> + +</li> + + </ul> + +</nav> + </div> + </div> + </div> + + + <div class="md-content" data-md-component="content"> + <article class="md-content__inner md-typeset"> + + + + +<h1 id="discover-docker">Discover Docker</h1> +<div class="admonition check"> +<p class="admonition-title">Check</p> +<p>Checkpoint: call us to check your results (don’t stay blocked on a checkpoint if we are busy, we can check ⅔ checkpoints at the same time). </p> +</div> +<div class="admonition question"> +<p class="admonition-title">Question</p> +<p>Point to document/report.</p> +</div> +<div class="admonition tip"> +<p class="admonition-title">Tip</p> +<p>Interesting information.</p> +</div> +<h2 id="goals">Goals</h2> +<h3 id="good-practice">Good practice</h3> +<p>Do not forget to document what you do along the steps, the documentation provided will be evaluated as your report. +Create an appropriate file structure, 1 folder per image.</p> +<h3 id="target-application">Target application</h3> +<p>3-tiers application:</p> +<ul> +<li>HTTP server</li> +<li>Backend API</li> +<li>Database</li> +</ul> +<p>For each of those applications, we will follow the same process: choose the appropriate docker base image, create and configure this image, put our application specifics inside and at some point have it running. Our final goal is to have a 3-tier web API running.</p> +<h3 id="base-images">Base images</h3> +<ul> +<li><a href="https://hub.docker.com/_/httpd" target="_blank">HTTP server</a></li> +<li><a href="https://hub.docker.com/_/openjdk" target="_blank">Backend API</a></li> +<li><a href="https://hub.docker.com/_/postgres" target="_blank">Database</a></li> +</ul> +<h2 id="database">Database</h2> +<h3 id="basics">Basics</h3> +<p>We will use the image: postgres:17.2-alpine.</p> +<p>Let’s have a simple postgres server running, here is what would be a minimal Dockerfile:</p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="k">FROM</span><span class="w"> </span><span class="s">postgres:17.2-alpine</span> +<a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a> +<a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a><span class="k">ENV</span><span class="w"> </span><span class="nv">POSTGRES_DB</span><span class="o">=</span>db<span class="w"> </span><span class="se">\</span> +<a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a><span class="w"> </span><span class="nv">POSTGRES_USER</span><span class="o">=</span>usr<span class="w"> </span><span class="se">\</span> +<a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a><span class="w"> </span><span class="nv">POSTGRES_PASSWORD</span><span class="o">=</span><span class="nb">pwd</span> +</code></pre></div> +<p>Build this image and start a container properly.</p> +<p>Your Postgres DB should be up and running. Check that everything is running smoothly with the docker command of your choice.<br /> +Don’t forget to name your docker image and container.</p> +<div class="admonition tip"> +<p class="admonition-title">Tip</p> +<p>If you have difficulties go back to part <strong>2.3.3 Build the image</strong> and <strong>2.3.4 Run your image</strong> on TD01 - Docker (<a href="http://school.pages.takima.io/devops-resources/ch1-discover-docker-td/" target="_blank">TD 1 Discover Docker</a>).</p> +</div> +<p>Re-run your database with <a href="https://hub.docker.com/_/adminer/" target="_blank">adminer</a>. Don't forget <code>--network app-network</code> to enable adminer/database communication. +We use <code>-–network</code> instead of <code>-–link</code> because the latter is deprecated.</p> +<div class="admonition tip"> +<p class="admonition-title">Tip</p> +<p>Don't forget to create your network</p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a><span class="w"> </span>docker<span class="w"> </span>network<span class="w"> </span>create<span class="w"> </span>app-network +</code></pre></div> +</div> +<p>Also, does it seem right to have passwords written in plain text in a file? You may rather define those environment parameters when running the image using the flag <code>-e</code>.</p> +<div class="admonition question"> +<p class="admonition-title">Question</p> +<p>1-1 For which reason is it better to run the container with a flag <code>-e</code> to give the environment variables rather than put them directly in the Dockerfile?</p> +</div> +<p>It would be nice to have our database structure initialized with the docker image as well as some initial data. Any sql scripts found in <code>/docker-entrypoint-initdb.d</code> will be executed in alphabetical order, therefore let’s add a couple scripts to our image: </p> +<div class="admonition tip"> +<p class="admonition-title">Tip</p> +<p>Don't forget to restart the adminer: </p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-2-1" name="__codelineno-2-1" href="#__codelineno-2-1"></a><span class="w"> </span>docker<span class="w"> </span>run<span class="w"> </span><span class="se">\</span> +<a id="__codelineno-2-2" name="__codelineno-2-2" href="#__codelineno-2-2"></a><span class="w"> </span>-p<span class="w"> </span><span class="s2">"8090:8080"</span><span class="w"> </span><span class="se">\</span> +<a id="__codelineno-2-3" name="__codelineno-2-3" href="#__codelineno-2-3"></a><span class="w"> </span>--net<span class="o">=</span>app-network<span class="w"> </span><span class="se">\</span> +<a id="__codelineno-2-4" name="__codelineno-2-4" href="#__codelineno-2-4"></a><span class="w"> </span>--name<span class="o">=</span>adminer<span class="w"> </span><span class="se">\</span> +<a id="__codelineno-2-5" name="__codelineno-2-5" href="#__codelineno-2-5"></a><span class="w"> </span>-d<span class="w"> </span><span class="se">\</span> +<a id="__codelineno-2-6" name="__codelineno-2-6" href="#__codelineno-2-6"></a><span class="w"> </span>adminer +</code></pre></div> +</div> +<h3 id="init-database">Init database</h3> +<p><strong>01-CreateScheme.sql</strong> +<div class="highlight"><pre><span></span><code><a id="__codelineno-3-1" name="__codelineno-3-1" href="#__codelineno-3-1"></a><span class="k">CREATE</span><span class="w"> </span><span class="k">TABLE</span><span class="w"> </span><span class="k">public</span><span class="p">.</span><span class="n">departments</span> +<a id="__codelineno-3-2" name="__codelineno-3-2" href="#__codelineno-3-2"></a><span class="p">(</span> +<a id="__codelineno-3-3" name="__codelineno-3-3" href="#__codelineno-3-3"></a><span class="w"> </span><span class="n">id</span><span class="w"> </span><span class="nb">SERIAL</span><span class="w"> </span><span class="k">PRIMARY</span><span class="w"> </span><span class="k">KEY</span><span class="p">,</span> +<a id="__codelineno-3-4" name="__codelineno-3-4" href="#__codelineno-3-4"></a><span class="w"> </span><span class="n">name</span><span class="w"> </span><span class="nb">VARCHAR</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span><span class="w"> </span><span class="k">NOT</span><span class="w"> </span><span class="k">NULL</span> +<a id="__codelineno-3-5" name="__codelineno-3-5" href="#__codelineno-3-5"></a><span class="p">);</span> +<a id="__codelineno-3-6" name="__codelineno-3-6" href="#__codelineno-3-6"></a> +<a id="__codelineno-3-7" name="__codelineno-3-7" href="#__codelineno-3-7"></a><span class="k">CREATE</span><span class="w"> </span><span class="k">TABLE</span><span class="w"> </span><span class="k">public</span><span class="p">.</span><span class="n">students</span> +<a id="__codelineno-3-8" name="__codelineno-3-8" href="#__codelineno-3-8"></a><span class="p">(</span> +<a id="__codelineno-3-9" name="__codelineno-3-9" href="#__codelineno-3-9"></a><span class="w"> </span><span class="n">id</span><span class="w"> </span><span class="nb">SERIAL</span><span class="w"> </span><span class="k">PRIMARY</span><span class="w"> </span><span class="k">KEY</span><span class="p">,</span> +<a id="__codelineno-3-10" name="__codelineno-3-10" href="#__codelineno-3-10"></a><span class="w"> </span><span class="n">department_id</span><span class="w"> </span><span class="nb">INT</span><span class="w"> </span><span class="k">NOT</span><span class="w"> </span><span class="k">NULL</span><span class="w"> </span><span class="k">REFERENCES</span><span class="w"> </span><span class="n">departments</span><span class="w"> </span><span class="p">(</span><span class="n">id</span><span class="p">),</span> +<a id="__codelineno-3-11" name="__codelineno-3-11" href="#__codelineno-3-11"></a><span class="w"> </span><span class="n">first_name</span><span class="w"> </span><span class="nb">VARCHAR</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span><span class="w"> </span><span class="k">NOT</span><span class="w"> </span><span class="k">NULL</span><span class="p">,</span> +<a id="__codelineno-3-12" name="__codelineno-3-12" href="#__codelineno-3-12"></a><span class="w"> </span><span class="n">last_name</span><span class="w"> </span><span class="nb">VARCHAR</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span><span class="w"> </span><span class="k">NOT</span><span class="w"> </span><span class="k">NULL</span> +<a id="__codelineno-3-13" name="__codelineno-3-13" href="#__codelineno-3-13"></a><span class="p">);</span> +</code></pre></div> +<strong>02-InsertData.sql</strong> +<div class="highlight"><pre><span></span><code><a id="__codelineno-4-1" name="__codelineno-4-1" href="#__codelineno-4-1"></a><span class="k">INSERT</span><span class="w"> </span><span class="k">INTO</span><span class="w"> </span><span class="n">departments</span><span class="w"> </span><span class="p">(</span><span class="n">name</span><span class="p">)</span><span class="w"> </span><span class="k">VALUES</span><span class="w"> </span><span class="p">(</span><span class="s1">'IRC'</span><span class="p">);</span> +<a id="__codelineno-4-2" name="__codelineno-4-2" href="#__codelineno-4-2"></a><span class="k">INSERT</span><span class="w"> </span><span class="k">INTO</span><span class="w"> </span><span class="n">departments</span><span class="w"> </span><span class="p">(</span><span class="n">name</span><span class="p">)</span><span class="w"> </span><span class="k">VALUES</span><span class="w"> </span><span class="p">(</span><span class="s1">'ETI'</span><span class="p">);</span> +<a id="__codelineno-4-3" name="__codelineno-4-3" href="#__codelineno-4-3"></a><span class="k">INSERT</span><span class="w"> </span><span class="k">INTO</span><span class="w"> </span><span class="n">departments</span><span class="w"> </span><span class="p">(</span><span class="n">name</span><span class="p">)</span><span class="w"> </span><span class="k">VALUES</span><span class="w"> </span><span class="p">(</span><span class="s1">'CGP'</span><span class="p">);</span> +<a id="__codelineno-4-4" name="__codelineno-4-4" href="#__codelineno-4-4"></a> +<a id="__codelineno-4-5" name="__codelineno-4-5" href="#__codelineno-4-5"></a> +<a id="__codelineno-4-6" name="__codelineno-4-6" href="#__codelineno-4-6"></a><span class="k">INSERT</span><span class="w"> </span><span class="k">INTO</span><span class="w"> </span><span class="n">students</span><span class="w"> </span><span class="p">(</span><span class="n">department_id</span><span class="p">,</span><span class="w"> </span><span class="n">first_name</span><span class="p">,</span><span class="w"> </span><span class="n">last_name</span><span class="p">)</span><span class="w"> </span><span class="k">VALUES</span><span class="w"> </span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="s1">'Eli'</span><span class="p">,</span><span class="w"> </span><span class="s1">'Copter'</span><span class="p">);</span> +<a id="__codelineno-4-7" name="__codelineno-4-7" href="#__codelineno-4-7"></a><span class="k">INSERT</span><span class="w"> </span><span class="k">INTO</span><span class="w"> </span><span class="n">students</span><span class="w"> </span><span class="p">(</span><span class="n">department_id</span><span class="p">,</span><span class="w"> </span><span class="n">first_name</span><span class="p">,</span><span class="w"> </span><span class="n">last_name</span><span class="p">)</span><span class="w"> </span><span class="k">VALUES</span><span class="w"> </span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="s1">'Emma'</span><span class="p">,</span><span class="w"> </span><span class="s1">'Carena'</span><span class="p">);</span> +<a id="__codelineno-4-8" name="__codelineno-4-8" href="#__codelineno-4-8"></a><span class="k">INSERT</span><span class="w"> </span><span class="k">INTO</span><span class="w"> </span><span class="n">students</span><span class="w"> </span><span class="p">(</span><span class="n">department_id</span><span class="p">,</span><span class="w"> </span><span class="n">first_name</span><span class="p">,</span><span class="w"> </span><span class="n">last_name</span><span class="p">)</span><span class="w"> </span><span class="k">VALUES</span><span class="w"> </span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="s1">'Jack'</span><span class="p">,</span><span class="w"> </span><span class="s1">'Uzzi'</span><span class="p">);</span> +<a id="__codelineno-4-9" name="__codelineno-4-9" href="#__codelineno-4-9"></a><span class="k">INSERT</span><span class="w"> </span><span class="k">INTO</span><span class="w"> </span><span class="n">students</span><span class="w"> </span><span class="p">(</span><span class="n">department_id</span><span class="p">,</span><span class="w"> </span><span class="n">first_name</span><span class="p">,</span><span class="w"> </span><span class="n">last_name</span><span class="p">)</span><span class="w"> </span><span class="k">VALUES</span><span class="w"> </span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="s1">'Aude'</span><span class="p">,</span><span class="w"> </span><span class="s1">'Javel'</span><span class="p">);</span> +</code></pre></div></p> +<p>Rebuild your image and check that your scripts have been executed at startup and that the data is present in your container.</p> +<div class="admonition tip"> +<p class="admonition-title">Tip</p> +<p>When we talk about <code>/docker-entrypoint-initdb.d</code> it means inside the container, so you have to copy your directory's content and the container’s directory.</p> +</div> +<h3 id="persist-data">Persist data</h3> +<p>You may have noticed that if your database container gets destroyed then all your data is reset, a database must persist data durably. Use volumes to persist data on the host disk.</p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-5-1" name="__codelineno-5-1" href="#__codelineno-5-1"></a>-v<span class="w"> </span>/my/own/datadir:/var/lib/postgresql/data +</code></pre></div> +<p>Check that data survives when your container gets destroyed.</p> +<div class="admonition link"> +<p class="admonition-title">Link</p> +<p><a href="https://docs.docker.com/storage/volumes/" target="_blank">Docker volumes</a></p> +</div> +<div class="admonition question"> +<p class="admonition-title">Question</p> +<p>1-2 Why do we need a volume to be attached to our postgres container?</p> +</div> +<div class="admonition question"> +<p class="admonition-title">Question</p> +<p>1-3 Document your database container essentials: commands and Dockerfile.</p> +</div> +<h2 id="backend-api">Backend API</h2> +<h3 id="basics_1">Basics</h3> +<p>For starters, we will simply run a Java hello-world class in our containers, only after will we be running a jar. In both cases, choose the proper image keeping in mind that <strong>we only need a Java runtime</strong>. </p> +<p>Here is a complex <strong>Java Hello World</strong> implementation:</p> +<p><strong>Main.java</strong> +<div class="highlight"><pre><span></span><code><a id="__codelineno-6-1" name="__codelineno-6-1" href="#__codelineno-6-1"></a><span class="kd">public</span><span class="w"> </span><span class="kd">class</span> <span class="nc">Main</span><span class="w"> </span><span class="p">{</span> +<a id="__codelineno-6-2" name="__codelineno-6-2" href="#__codelineno-6-2"></a> +<a id="__codelineno-6-3" name="__codelineno-6-3" href="#__codelineno-6-3"></a><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="n">String</span><span class="o">[]</span><span class="w"> </span><span class="n">args</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> +<a id="__codelineno-6-4" name="__codelineno-6-4" href="#__codelineno-6-4"></a><span class="w"> </span><span class="n">System</span><span class="p">.</span><span class="na">out</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="s">"Hello World!"</span><span class="p">);</span> +<a id="__codelineno-6-5" name="__codelineno-6-5" href="#__codelineno-6-5"></a><span class="w"> </span><span class="p">}</span> +<a id="__codelineno-6-6" name="__codelineno-6-6" href="#__codelineno-6-6"></a><span class="p">}</span> +</code></pre></div></p> +<p>1- Compile with your target Java: <code>javac Main.java</code>. <br /> +2- Write dockerfile.<br /> +<div class="highlight"><pre><span></span><code><a id="__codelineno-7-1" name="__codelineno-7-1" href="#__codelineno-7-1"></a>FROM<span class="w"> </span><span class="c1"># TODO: Choose a java JRE</span> +<a id="__codelineno-7-2" name="__codelineno-7-2" href="#__codelineno-7-2"></a><span class="c1"># TODO: Add the compiled java (aka bytecode, aka .class)</span> +<a id="__codelineno-7-3" name="__codelineno-7-3" href="#__codelineno-7-3"></a><span class="c1"># TODO: Run the Java with: “java Main” command.</span> +</code></pre></div> +3- Now, to launch app you have to do the same thing that Basic step 1.</p> +<p>Here you have a first glimpse of your backend application.</p> +<p>In the next step we will simply enrich the build (using maven instead of a minimalistic javac) and execute a jar instead of a simple .class. </p> +<p>→ If it’s a success you must see “Hello Word” in your console.</p> +<h3 id="multistage-build">Multistage build</h3> +<p>In the previous section we were building Java code on our machine to have it running on a docker container. Wouldn’t it be great to have Docker handle the build as well? You probably noticed that the default openjdk docker images contain... Well... a JDK! Create a multistage build using the <a href="https://docs.docker.com/develop/develop-images/multistage-build/" target="_blank">Multistage</a>.</p> +<p>Your Dockerfile should look like this:</p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-8-1" name="__codelineno-8-1" href="#__codelineno-8-1"></a><span class="k">FROM</span><span class="w"> </span><span class="s">eclipse-temurin:21-jdk-alpine</span> +<a id="__codelineno-8-2" name="__codelineno-8-2" href="#__codelineno-8-2"></a><span class="c"># Build Main.java with JDK</span> +<a id="__codelineno-8-3" name="__codelineno-8-3" href="#__codelineno-8-3"></a><span class="c"># TODO : in next steps (not now)</span> +<a id="__codelineno-8-4" name="__codelineno-8-4" href="#__codelineno-8-4"></a> +<a id="__codelineno-8-5" name="__codelineno-8-5" href="#__codelineno-8-5"></a><span class="k">FROM</span><span class="w"> </span><span class="s">eclipse-temurin:21-jre-alpine</span> +<a id="__codelineno-8-6" name="__codelineno-8-6" href="#__codelineno-8-6"></a><span class="c"># Copy resource from previous stage</span> +<a id="__codelineno-8-7" name="__codelineno-8-7" href="#__codelineno-8-7"></a><span class="k">COPY</span><span class="w"> </span>--from<span class="o">=</span><span class="m">0</span><span class="w"> </span>/usr/src/Main.class<span class="w"> </span>. +<a id="__codelineno-8-8" name="__codelineno-8-8" href="#__codelineno-8-8"></a><span class="c"># Run java code with the JRE</span> +<a id="__codelineno-8-9" name="__codelineno-8-9" href="#__codelineno-8-9"></a><span class="c"># TODO : in next steps (not now)</span> +</code></pre></div> +<p>Don’t fill the Dockerfile now, we will have to do it in the next steps.</p> +<h4 id="backend-simple-api">Backend simple api</h4> +<p>We will deploy a Springboot application providing a simple API with a single greeting endpoint. </p> +<p>Create your Springboot application on: <a href="https://start.spring.io/#!type=maven-project&language=java&platformVersion=3.4.2&packaging=jar&jvmVersion=21&groupId=fr.takima.training&artifactId=simpleapi&name=simpleapi&description=Demo%20project%20for%20Spring%20Boot&packageName=fr.takima.training.simpleapi&dependencies=web" target="_blank">Spring Initializer</a>.</p> +<p>Use the following config:</p> +<ul> +<li>Project: Maven </li> +<li>Language: Java 21 </li> +<li>Spring Boot: 3.4.2</li> +<li>Packaging: Jar </li> +<li>Dependencies: <strong>Spring Web</strong></li> +</ul> +<p>Generate the project and give it a simple GreetingController class: </p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-9-1" name="__codelineno-9-1" href="#__codelineno-9-1"></a><span class="kn">package</span><span class="w"> </span><span class="nn">fr.takima.training.simpleapi.controller</span><span class="p">;</span> +<a id="__codelineno-9-2" name="__codelineno-9-2" href="#__codelineno-9-2"></a> +<a id="__codelineno-9-3" name="__codelineno-9-3" href="#__codelineno-9-3"></a><span class="kn">import</span><span class="w"> </span><span class="nn">org.springframework.web.bind.annotation.*</span><span class="p">;</span> +<a id="__codelineno-9-4" name="__codelineno-9-4" href="#__codelineno-9-4"></a> +<a id="__codelineno-9-5" name="__codelineno-9-5" href="#__codelineno-9-5"></a><span class="kn">import</span><span class="w"> </span><span class="nn">java.util.concurrent.atomic.AtomicLong</span><span class="p">;</span> +<a id="__codelineno-9-6" name="__codelineno-9-6" href="#__codelineno-9-6"></a> +<a id="__codelineno-9-7" name="__codelineno-9-7" href="#__codelineno-9-7"></a><span class="nd">@RestController</span> +<a id="__codelineno-9-8" name="__codelineno-9-8" href="#__codelineno-9-8"></a><span class="kd">public</span><span class="w"> </span><span class="kd">class</span> <span class="nc">GreetingController</span><span class="w"> </span><span class="p">{</span> +<a id="__codelineno-9-9" name="__codelineno-9-9" href="#__codelineno-9-9"></a> +<a id="__codelineno-9-10" name="__codelineno-9-10" href="#__codelineno-9-10"></a><span class="w"> </span><span class="kd">private</span><span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="kd">final</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">template</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"Hello, %s!"</span><span class="p">;</span> +<a id="__codelineno-9-11" name="__codelineno-9-11" href="#__codelineno-9-11"></a><span class="w"> </span><span class="kd">private</span><span class="w"> </span><span class="kd">final</span><span class="w"> </span><span class="n">AtomicLong</span><span class="w"> </span><span class="n">counter</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">AtomicLong</span><span class="p">();</span> +<a id="__codelineno-9-12" name="__codelineno-9-12" href="#__codelineno-9-12"></a> +<a id="__codelineno-9-13" name="__codelineno-9-13" href="#__codelineno-9-13"></a><span class="w"> </span><span class="nd">@GetMapping</span><span class="p">(</span><span class="s">"/"</span><span class="p">)</span> +<a id="__codelineno-9-14" name="__codelineno-9-14" href="#__codelineno-9-14"></a><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="n">Greeting</span><span class="w"> </span><span class="nf">greeting</span><span class="p">(</span><span class="nd">@RequestParam</span><span class="p">(</span><span class="n">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"name"</span><span class="p">,</span><span class="w"> </span><span class="n">defaultValue</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"World"</span><span class="p">)</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">name</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> +<a id="__codelineno-9-15" name="__codelineno-9-15" href="#__codelineno-9-15"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Greeting</span><span class="p">(</span><span class="n">counter</span><span class="p">.</span><span class="na">incrementAndGet</span><span class="p">(),</span><span class="w"> </span><span class="n">String</span><span class="p">.</span><span class="na">format</span><span class="p">(</span><span class="n">template</span><span class="p">,</span><span class="w"> </span><span class="n">name</span><span class="p">));</span> +<a id="__codelineno-9-16" name="__codelineno-9-16" href="#__codelineno-9-16"></a><span class="w"> </span><span class="p">}</span> +<a id="__codelineno-9-17" name="__codelineno-9-17" href="#__codelineno-9-17"></a> +<a id="__codelineno-9-18" name="__codelineno-9-18" href="#__codelineno-9-18"></a><span class="w"> </span><span class="kd">record</span> <span class="nc">Greeting</span><span class="p">(</span><span class="kt">long</span><span class="w"> </span><span class="n">id</span><span class="p">,</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">content</span><span class="p">)</span><span class="w"> </span><span class="p">{}</span> +<a id="__codelineno-9-19" name="__codelineno-9-19" href="#__codelineno-9-19"></a> +<a id="__codelineno-9-20" name="__codelineno-9-20" href="#__codelineno-9-20"></a><span class="p">}</span> +</code></pre></div> +<p>You can now build and start your application, of course you will need maven and a jdk-21. </p> +<p>How convenient would it be to have a virtual container to build and run our simplistic API? </p> +<p>Oh wait, we have docker, here is how you could build and run your application with Docker: </p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-10-1" name="__codelineno-10-1" href="#__codelineno-10-1"></a><span class="c"># Build stage</span> +<a id="__codelineno-10-2" name="__codelineno-10-2" href="#__codelineno-10-2"></a><span class="k">FROM</span><span class="w"> </span><span class="s">eclipse-temurin:21-jdk-alpine</span><span class="w"> </span><span class="k">AS</span><span class="w"> </span><span class="s">myapp-build</span> +<a id="__codelineno-10-3" name="__codelineno-10-3" href="#__codelineno-10-3"></a><span class="k">ENV</span><span class="w"> </span><span class="nv">MYAPP_HOME</span><span class="o">=</span>/opt/myapp +<a id="__codelineno-10-4" name="__codelineno-10-4" href="#__codelineno-10-4"></a><span class="k">WORKDIR</span><span class="w"> </span><span class="s">$MYAPP_HOME</span> +<a id="__codelineno-10-5" name="__codelineno-10-5" href="#__codelineno-10-5"></a> +<a id="__codelineno-10-6" name="__codelineno-10-6" href="#__codelineno-10-6"></a><span class="k">RUN</span><span class="w"> </span>apk<span class="w"> </span>add<span class="w"> </span>--no-cache<span class="w"> </span>maven +<a id="__codelineno-10-7" name="__codelineno-10-7" href="#__codelineno-10-7"></a> +<a id="__codelineno-10-8" name="__codelineno-10-8" href="#__codelineno-10-8"></a><span class="k">COPY</span><span class="w"> </span>pom.xml<span class="w"> </span>. +<a id="__codelineno-10-9" name="__codelineno-10-9" href="#__codelineno-10-9"></a><span class="k">COPY</span><span class="w"> </span>src<span class="w"> </span>./src +<a id="__codelineno-10-10" name="__codelineno-10-10" href="#__codelineno-10-10"></a><span class="k">RUN</span><span class="w"> </span>mvn<span class="w"> </span>package<span class="w"> </span>-DskipTests +<a id="__codelineno-10-11" name="__codelineno-10-11" href="#__codelineno-10-11"></a> +<a id="__codelineno-10-12" name="__codelineno-10-12" href="#__codelineno-10-12"></a><span class="c"># Run stage</span> +<a id="__codelineno-10-13" name="__codelineno-10-13" href="#__codelineno-10-13"></a><span class="k">FROM</span><span class="w"> </span><span class="s">eclipse-temurin:21-jre-alpine</span> +<a id="__codelineno-10-14" name="__codelineno-10-14" href="#__codelineno-10-14"></a><span class="k">ENV</span><span class="w"> </span><span class="nv">MYAPP_HOME</span><span class="o">=</span>/opt/myapp +<a id="__codelineno-10-15" name="__codelineno-10-15" href="#__codelineno-10-15"></a><span class="k">WORKDIR</span><span class="w"> </span><span class="s">$MYAPP_HOME</span> +<a id="__codelineno-10-16" name="__codelineno-10-16" href="#__codelineno-10-16"></a><span class="k">COPY</span><span class="w"> </span>--from<span class="o">=</span>myapp-build<span class="w"> </span><span class="nv">$MYAPP_HOME</span>/target/*.jar<span class="w"> </span><span class="nv">$MYAPP_HOME</span>/myapp.jar +<a id="__codelineno-10-17" name="__codelineno-10-17" href="#__codelineno-10-17"></a> +<a id="__codelineno-10-18" name="__codelineno-10-18" href="#__codelineno-10-18"></a><span class="k">ENTRYPOINT</span><span class="w"> </span><span class="p">[</span><span class="s2">"java"</span><span class="p">,</span><span class="w"> </span><span class="s2">"-jar"</span><span class="p">,</span><span class="w"> </span><span class="s2">"myapp.jar"</span><span class="p">]</span> +</code></pre></div> +<div class="admonition question"> +<p class="admonition-title">Question</p> +<p>1-4 Why do we need a multistage build? And explain each step of this dockerfile.</p> +</div> +<div class="admonition check"> +<p class="admonition-title">Check</p> +<p>A working Springboot application with a simple HelloWorld endpoint.</p> +</div> +<p>Did you notice that maven downloads all libraries on every image build?<br /> +You can contribute to saving the planet caching libraries when maven pom file has not been changed by running the goal: <code>mvn dependency:go-offline</code>.</p> +<h4 id="backend-api_1">Backend API</h4> +<p>Let’s now build and run the backend API connected to the database. You can get the zipped source code here: <a href="https://github.com/takima-training/simple-api-student" target="_blank">simple-api</a>. +You can <strong>replace only</strong> your <code>src</code> directory and the <code>pom.xml</code> file with the ones available in the repository.</p> +<p>Adjust the configuration in <code>simple-api/src/main/resources/application.yml</code> (this is the application configuration). +How to access the database container from your backend application? Use the deprecated <code>--link</code> or create a <code>docker network</code>.</p> +<p>Once everything is properly bound, you should be able to access your application API, for example on: <a href="http://localhost:8080/departments/IRC/students" target="_blank">/departments/IRC/students</a>.</p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-11-1" name="__codelineno-11-1" href="#__codelineno-11-1"></a><span class="p">[</span> +<a id="__codelineno-11-2" name="__codelineno-11-2" href="#__codelineno-11-2"></a><span class="w"> </span><span class="p">{</span> +<a id="__codelineno-11-3" name="__codelineno-11-3" href="#__codelineno-11-3"></a><span class="w"> </span><span class="nt">"id"</span><span class="p">:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span> +<a id="__codelineno-11-4" name="__codelineno-11-4" href="#__codelineno-11-4"></a><span class="w"> </span><span class="nt">"firstname"</span><span class="p">:</span><span class="w"> </span><span class="s2">"Eli"</span><span class="p">,</span> +<a id="__codelineno-11-5" name="__codelineno-11-5" href="#__codelineno-11-5"></a><span class="w"> </span><span class="nt">"lastname"</span><span class="p">:</span><span class="w"> </span><span class="s2">"Copter"</span><span class="p">,</span> +<a id="__codelineno-11-6" name="__codelineno-11-6" href="#__codelineno-11-6"></a><span class="w"> </span><span class="nt">"department"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span> +<a id="__codelineno-11-7" name="__codelineno-11-7" href="#__codelineno-11-7"></a><span class="w"> </span><span class="nt">"id"</span><span class="p">:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span> +<a id="__codelineno-11-8" name="__codelineno-11-8" href="#__codelineno-11-8"></a><span class="w"> </span><span class="nt">"name"</span><span class="p">:</span><span class="w"> </span><span class="s2">"IRC"</span> +<a id="__codelineno-11-9" name="__codelineno-11-9" href="#__codelineno-11-9"></a><span class="w"> </span><span class="p">}</span> +<a id="__codelineno-11-10" name="__codelineno-11-10" href="#__codelineno-11-10"></a><span class="w"> </span><span class="p">}</span> +<a id="__codelineno-11-11" name="__codelineno-11-11" href="#__codelineno-11-11"></a><span class="p">]</span> +</code></pre></div> +<p>Explore your API other endpoints, have a look at the controllers in the source code.</p> +<div class="admonition check"> +<p class="admonition-title">Check</p> +<p>A simple web API on top of your database.</p> +</div> +<h2 id="http-server">Http server</h2> +<h3 id="basics_2">Basics</h3> +<h3 id="choose-an-appropriate-base-image">Choose an appropriate base image.</h3> +<p>Create a simple landing page: <code>index.html</code> and put it inside your container.</p> +<p>It should be enough for now, start your container and check that everything is working as expected.</p> +<p>Here are commands that you may want to try to do so:</p> +<ul> +<li>docker stats</li> +<li>docker inspect</li> +<li>docker logs</li> +</ul> +<div class="admonition link"> +<p class="admonition-title">Link</p> +<ul> +<li><a href="https://hub.docker.com/_/httpd" target="_blank">Httpd</a></li> +<li><a href="https://httpd.apache.org/docs/2.4/getting-started.html" target="_blank">Getting Started</a> </li> +</ul> +</div> +<h3 id="configuration">Configuration</h3> +<p>You are using the default apache configuration, and it will be enough for now, you use yours by copying it in your image.</p> +<p>Use <code>docker exec</code> to retrieve this default configuration from your running container <code>/usr/local/apache2/conf/httpd.conf</code>.</p> +<div class="admonition note"> +<p class="admonition-title">Note</p> +<p>You can also use <code>docker cp</code>.</p> +</div> +<h3 id="reverse-proxy">Reverse proxy</h3> +<p>We will configure the http server as a simple reverse proxy server in front of our application, this server could be used to deliver a front-end application, to configure SSL or to handle load balancing. </p> +<p>So this can be quite useful even though in our case we will keep things simple. </p> +<p>Here is the documentation: <a href="https://httpd.apache.org/docs/2.4/en/howto/reverse_proxy.html" target="_blank">Reverse Proxy</a>.</p> +<p>Add the following to the configuration, and you should be all set:</p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-12-1" name="__codelineno-12-1" href="#__codelineno-12-1"></a><VirtualHost<span class="w"> </span>*:80> +<a id="__codelineno-12-2" name="__codelineno-12-2" href="#__codelineno-12-2"></a>ProxyPreserveHost<span class="w"> </span>On +<a id="__codelineno-12-3" name="__codelineno-12-3" href="#__codelineno-12-3"></a>ProxyPass<span class="w"> </span>/<span class="w"> </span>http://YOUR_BACKEND_LINK:8080/ +<a id="__codelineno-12-4" name="__codelineno-12-4" href="#__codelineno-12-4"></a>ProxyPassReverse<span class="w"> </span>/<span class="w"> </span>http://YOUR_BACKEND_LINK:8080/ +<a id="__codelineno-12-5" name="__codelineno-12-5" href="#__codelineno-12-5"></a></VirtualHost> +<a id="__codelineno-12-6" name="__codelineno-12-6" href="#__codelineno-12-6"></a>LoadModule<span class="w"> </span>proxy_module<span class="w"> </span>modules/mod_proxy.so +<a id="__codelineno-12-7" name="__codelineno-12-7" href="#__codelineno-12-7"></a>LoadModule<span class="w"> </span>proxy_http_module<span class="w"> </span>modules/mod_proxy_http.so +</code></pre></div> +<div class="admonition question"> +<p class="admonition-title">Question</p> +<p>1-5 Why do we need a reverse proxy?</p> +</div> +<div class="admonition check"> +<p class="admonition-title">Check</p> +<p>Checkpoint: a working application through a reverse proxy.</p> +</div> +<h2 id="link-application">Link application</h2> +<h3 id="docker-compose">Docker-compose</h3> +<p>1- Install <a href="https://docs.docker.com/compose/install/" target="_blank">docker-compose</a> if the <code>docker compose</code> command does not work .</p> +<p>You may have noticed that this can be quite painful to orchestrate manually the start, stop and rebuild of our containers. Thankfully, a useful tool called <a href="https://docs.docker.com/compose/" target="_blank">docker-compose</a> comes in handy in those situations. </p> +<p>2- Let’s create a docker-compose.yml file with the following structure to define and drive our containers:<br /> +<div class="highlight"><pre><span></span><code><a id="__codelineno-13-1" name="__codelineno-13-1" href="#__codelineno-13-1"></a><span class="nt">services</span><span class="p">:</span> +<a id="__codelineno-13-2" name="__codelineno-13-2" href="#__codelineno-13-2"></a><span class="w"> </span><span class="nt">backend</span><span class="p">:</span> +<a id="__codelineno-13-3" name="__codelineno-13-3" href="#__codelineno-13-3"></a><span class="w"> </span><span class="nt">build</span><span class="p">:</span> +<a id="__codelineno-13-4" name="__codelineno-13-4" href="#__codelineno-13-4"></a><span class="w"> </span><span class="c1">#TODO</span> +<a id="__codelineno-13-5" name="__codelineno-13-5" href="#__codelineno-13-5"></a><span class="w"> </span><span class="nt">networks</span><span class="p">:</span> +<a id="__codelineno-13-6" name="__codelineno-13-6" href="#__codelineno-13-6"></a><span class="w"> </span><span class="c1">#TODO</span> +<a id="__codelineno-13-7" name="__codelineno-13-7" href="#__codelineno-13-7"></a><span class="w"> </span><span class="nt">depends_on</span><span class="p">:</span> +<a id="__codelineno-13-8" name="__codelineno-13-8" href="#__codelineno-13-8"></a><span class="w"> </span><span class="c1">#TODO</span> +<a id="__codelineno-13-9" name="__codelineno-13-9" href="#__codelineno-13-9"></a> +<a id="__codelineno-13-10" name="__codelineno-13-10" href="#__codelineno-13-10"></a><span class="w"> </span><span class="nt">database</span><span class="p">:</span> +<a id="__codelineno-13-11" name="__codelineno-13-11" href="#__codelineno-13-11"></a><span class="w"> </span><span class="nt">build</span><span class="p">:</span> +<a id="__codelineno-13-12" name="__codelineno-13-12" href="#__codelineno-13-12"></a><span class="w"> </span><span class="c1">#TODO</span> +<a id="__codelineno-13-13" name="__codelineno-13-13" href="#__codelineno-13-13"></a><span class="w"> </span><span class="nt">networks</span><span class="p">:</span> +<a id="__codelineno-13-14" name="__codelineno-13-14" href="#__codelineno-13-14"></a><span class="w"> </span><span class="c1">#TODO</span> +<a id="__codelineno-13-15" name="__codelineno-13-15" href="#__codelineno-13-15"></a> +<a id="__codelineno-13-16" name="__codelineno-13-16" href="#__codelineno-13-16"></a><span class="w"> </span><span class="nt">httpd</span><span class="p">:</span> +<a id="__codelineno-13-17" name="__codelineno-13-17" href="#__codelineno-13-17"></a><span class="w"> </span><span class="nt">build</span><span class="p">:</span> +<a id="__codelineno-13-18" name="__codelineno-13-18" href="#__codelineno-13-18"></a><span class="w"> </span><span class="c1">#TODO</span> +<a id="__codelineno-13-19" name="__codelineno-13-19" href="#__codelineno-13-19"></a><span class="w"> </span><span class="nt">ports</span><span class="p">:</span> +<a id="__codelineno-13-20" name="__codelineno-13-20" href="#__codelineno-13-20"></a><span class="w"> </span><span class="c1">#TODO</span> +<a id="__codelineno-13-21" name="__codelineno-13-21" href="#__codelineno-13-21"></a><span class="w"> </span><span class="nt">networks</span><span class="p">:</span> +<a id="__codelineno-13-22" name="__codelineno-13-22" href="#__codelineno-13-22"></a><span class="w"> </span><span class="c1">#TODO</span> +<a id="__codelineno-13-23" name="__codelineno-13-23" href="#__codelineno-13-23"></a><span class="w"> </span><span class="nt">depends_on</span><span class="p">:</span> +<a id="__codelineno-13-24" name="__codelineno-13-24" href="#__codelineno-13-24"></a><span class="w"> </span><span class="c1">#TODO</span> +<a id="__codelineno-13-25" name="__codelineno-13-25" href="#__codelineno-13-25"></a> +<a id="__codelineno-13-26" name="__codelineno-13-26" href="#__codelineno-13-26"></a><span class="nt">networks</span><span class="p">:</span> +<a id="__codelineno-13-27" name="__codelineno-13-27" href="#__codelineno-13-27"></a><span class="w"> </span><span class="c1">#TODO</span> +<a id="__codelineno-13-28" name="__codelineno-13-28" href="#__codelineno-13-28"></a> +<a id="__codelineno-13-29" name="__codelineno-13-29" href="#__codelineno-13-29"></a><span class="nt">volumes</span><span class="p">:</span> +<a id="__codelineno-13-30" name="__codelineno-13-30" href="#__codelineno-13-30"></a><span class="w"> </span><span class="c1">#TODO</span> +</code></pre></div></p> +<p>The docker-compose will handle the three containers for us. </p> +<p>The file above is a basic example of structure, <strong>you need to add more parameters</strong> and think about the <strong>cleanest and most optimized approach</strong> like you would do in a company (for example: env variables, volumes, restart policies and processes segregation).</p> +<p>Once your containers are orchestrated as services by docker-compose you should have a perfectly running application, make sure you can access your API on <a href="http://localhost/" target="_blank">localhost</a>. </p> +<div class="admonition note"> +<p class="admonition-title">Note</p> +<p>The ports of both your backend and database should not be opened to your host machine.</p> +</div> +<div class="admonition question"> +<p class="admonition-title">Question</p> +<p>1-6 Why is <strong>docker-compose</strong> so important?</p> +</div> +<div class="admonition question"> +<p class="admonition-title">Question</p> +<p>1-7 Document docker-compose most important commands.</p> +</div> +<div class="admonition question"> +<p class="admonition-title">Question</p> +<p>1-8 Document your docker-compose file.</p> +</div> +<div class="admonition check"> +<p class="admonition-title">Check</p> +<p>A working 3-tier application running with docker-compose.</p> +</div> +<h2 id="publish">Publish</h2> +<p>Your docker images are stored locally, let’s publish them, so they can be used by other team members or on other machines. </p> +<p>You will need a <a href="https://hub.docker.com/" target="_blank">Docker Hub</a> account. </p> +<p>1- Connect to your freshly created account with <code>docker login</code>.</p> +<p>2- Tag your image. For now, we have been only using the latest tag, now that we want to publish it, let’s add some meaningful version information to our images. </p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-14-1" name="__codelineno-14-1" href="#__codelineno-14-1"></a>docker<span class="w"> </span>tag<span class="w"> </span>my-database<span class="w"> </span>USERNAME/my-database:1.0 +</code></pre></div> +<p>3- Then push your image to dockerhub:</p> +<div class="highlight"><pre><span></span><code><a id="__codelineno-15-1" name="__codelineno-15-1" href="#__codelineno-15-1"></a>docker<span class="w"> </span>push<span class="w"> </span>USERNAME/my-database:1.0<span class="w"> </span> +</code></pre></div> +<p><strong>Dockerhub</strong> is not the only docker image registry, and you can also self-host your images (this is obviously the choice of most companies). </p> +<p>Once you publish your images to dockerhub, you will see them in your account: having some documentation for your image would be quite useful if you want to use those later.</p> +<div class="admonition question"> +<p class="admonition-title">Question</p> +<p>1-9 Document your publication commands and published images in dockerhub.</p> +</div> +<div class="admonition question"> +<p class="admonition-title">Question</p> +<p>1-10 Why do we put our images into an online repo?</p> +</div> +<p align="center">© Takima 2025</p> + + + </article> + </div> + </div> + + </main> + + <footer class="md-footer"> + + <nav class="md-footer__inner md-grid" aria-label="Footer"> + + + + <a href="../ch1-discover-docker-td/" class="md-footer__link md-footer__link--next" aria-label="Next: TD part 01 - Docker" rel="next"> + <div class="md-footer__title"> + <div class="md-ellipsis"> + <span class="md-footer__direction"> + Next + </span> + TD part 01 - Docker + </div> + </div> + <div class="md-footer__button md-icon"> + <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg> + </div> + </a> + + </nav> + + <div class="md-footer-meta md-typeset"> + <div class="md-footer-meta__inner md-grid"> + <div class="md-copyright"> + + + Made with + <a href="https://squidfunk.github.io/mkdocs-material/" target="_blank" rel="noopener"> + Material for MkDocs + </a> + +</div> + + </div> + </div> +</footer> + + </div> + <div class="md-dialog" data-md-component="dialog"> + <div class="md-dialog__inner md-typeset"></div> + </div> + <script id="__config" type="application/json">{"base": "..", "features": ["navigation.tabs", "navigation.instant"], "search": "../assets/javascripts/workers/search.2a1c317c.min.js", "translations": {"clipboard.copied": "Copied to clipboard", "clipboard.copy": "Copy to clipboard", "search.config.lang": "en", "search.config.pipeline": "trimmer, stopWordFilter", "search.config.separator": "[\\s\\-]+", "search.placeholder": "Search", "search.result.more.one": "1 more on this page", "search.result.more.other": "# more on this page", "search.result.none": "No matching documents", "search.result.one": "1 matching document", "search.result.other": "# matching documents", "search.result.placeholder": "Type to start searching", "search.result.term.missing": "Missing", "select.version.title": "Select version"}}</script> + + + <script src="../assets/javascripts/bundle.a6c66575.min.js"></script> + + + </body> +</html> \ No newline at end of file diff --git a/public/cheatsheet/index.html b/public/cheatsheet/index.html index 0645199..39a3dcf 100644 --- a/public/cheatsheet/index.html +++ b/public/cheatsheet/index.html @@ -157,25 +157,31 @@ - <li class="md-tabs__item"> - <a href=".." class="md-tabs__link"> - Devops in Action - Guide - </a> - </li> + + + + <li class="md-tabs__item"> + <a href="../ch1-discover-docker-tp/" class="md-tabs__link"> + TP + </a> + </li> + - - - <li class="md-tabs__item"> - <a href="./" class="md-tabs__link md-tabs__link--active"> - Cheatsheet - </a> - </li> + + + + <li class="md-tabs__item"> + <a href="../ch1-discover-docker-td/" class="md-tabs__link"> + TD + </a> + </li> + </ul> @@ -216,13 +222,46 @@ + + <li class="md-nav__item md-nav__item--nested"> + + + <input class="md-nav__toggle md-toggle" data-md-toggle="__nav_1" type="checkbox" id="__nav_1" > + + + + + <label class="md-nav__link" for="__nav_1"> + TP + <span class="md-nav__icon md-icon"></span> + </label> + + <nav class="md-nav" aria-label="TP" data-md-level="1"> + <label class="md-nav__title" for="__nav_1"> + <span class="md-nav__icon md-icon"></span> + TP + </label> + <ul class="md-nav__list" data-md-scrollfix> + + + + + + <li class="md-nav__item"> - <a href=".." class="md-nav__link"> - Devops in Action - Guide + <a href="../ch1-discover-docker-tp/" class="md-nav__link"> + TP part 01 - Docker </a> </li> + + + </ul> + </nav> + </li> + + @@ -230,21 +269,44 @@ - - - <li class="md-nav__item md-nav__item--active"> + + <li class="md-nav__item md-nav__item--nested"> - <input class="md-nav__toggle md-toggle" data-md-toggle="toc" type="checkbox" id="__toc"> + <input class="md-nav__toggle md-toggle" data-md-toggle="__nav_2" type="checkbox" id="__nav_2" > - - <a href="./" class="md-nav__link md-nav__link--active"> - Cheatsheet - </a> + <label class="md-nav__link" for="__nav_2"> + TD + <span class="md-nav__icon md-icon"></span> + </label> + + <nav class="md-nav" aria-label="TD" data-md-level="1"> + <label class="md-nav__title" for="__nav_2"> + <span class="md-nav__icon md-icon"></span> + TD + </label> + <ul class="md-nav__list" data-md-scrollfix> + + + + + + + <li class="md-nav__item"> + <a href="../ch1-discover-docker-td/" class="md-nav__link"> + TD part 01 - Docker + </a> + </li> + + + + + </ul> + </nav> </li> @@ -269,6 +331,21 @@ + <label class="md-nav__title" for="__toc"> + <span class="md-nav__icon md-icon"></span> + Table of contents + </label> + <ul class="md-nav__list" data-md-component="toc" data-md-scrollfix> + + <li class="md-nav__item"> + <a href="#docker-docker-compose" class="md-nav__link"> + Docker & docker-compose + </a> + +</li> + + </ul> + </nav> </div> </div> @@ -282,6 +359,7 @@ <h1 id="cheatsheet">Cheatsheet</h1> +<h2 id="docker-docker-compose">Docker & docker-compose</h2> </article> @@ -292,26 +370,6 @@ <footer class="md-footer"> - <nav class="md-footer__inner md-grid" aria-label="Footer"> - - - <a href=".." class="md-footer__link md-footer__link--prev" aria-label="Previous: Devops in Action - Guide" rel="prev"> - <div class="md-footer__button md-icon"> - <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> - </div> - <div class="md-footer__title"> - <div class="md-ellipsis"> - <span class="md-footer__direction"> - Previous - </span> - Devops in Action - Guide - </div> - </div> - </a> - - - </nav> - <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class="md-copyright"> diff --git a/public/index.html b/public/index.html index 8f6b1cb..f0b342d 100644 --- a/public/index.html +++ b/public/index.html @@ -155,15 +155,17 @@ - - - <li class="md-tabs__item"> - <a href="." class="md-tabs__link md-tabs__link--active"> - Devops in Action - Guide - </a> - </li> + + + + <li class="md-tabs__item"> + <a href="ch1-discover-docker-tp/" class="md-tabs__link"> + TP + </a> + </li> + @@ -171,11 +173,15 @@ - <li class="md-tabs__item"> - <a href="cheatsheet/" class="md-tabs__link"> - Cheatsheet - </a> - </li> + + + + <li class="md-tabs__item"> + <a href="ch1-discover-docker-td/" class="md-tabs__link"> + TD + </a> + </li> + </ul> @@ -215,21 +221,44 @@ - - - <li class="md-nav__item md-nav__item--active"> + + <li class="md-nav__item md-nav__item--nested"> - <input class="md-nav__toggle md-toggle" data-md-toggle="toc" type="checkbox" id="__toc"> + <input class="md-nav__toggle md-toggle" data-md-toggle="__nav_1" type="checkbox" id="__nav_1" > - - <a href="." class="md-nav__link md-nav__link--active"> - Devops in Action - Guide - </a> + <label class="md-nav__link" for="__nav_1"> + TP + <span class="md-nav__icon md-icon"></span> + </label> + + <nav class="md-nav" aria-label="TP" data-md-level="1"> + <label class="md-nav__title" for="__nav_1"> + <span class="md-nav__icon md-icon"></span> + TP + </label> + <ul class="md-nav__list" data-md-scrollfix> + + + + + + + <li class="md-nav__item"> + <a href="ch1-discover-docker-tp/" class="md-nav__link"> + TP part 01 - Docker + </a> + </li> + + + + + </ul> + </nav> </li> @@ -241,13 +270,46 @@ + + <li class="md-nav__item md-nav__item--nested"> + + + <input class="md-nav__toggle md-toggle" data-md-toggle="__nav_2" type="checkbox" id="__nav_2" > + + + + + <label class="md-nav__link" for="__nav_2"> + TD + <span class="md-nav__icon md-icon"></span> + </label> + + <nav class="md-nav" aria-label="TD" data-md-level="1"> + <label class="md-nav__title" for="__nav_2"> + <span class="md-nav__icon md-icon"></span> + TD + </label> + <ul class="md-nav__list" data-md-scrollfix> + + + + + + <li class="md-nav__item"> - <a href="cheatsheet/" class="md-nav__link"> - Cheatsheet + <a href="ch1-discover-docker-td/" class="md-nav__link"> + TD part 01 - Docker </a> </li> + + + </ul> + </nav> + </li> + + </ul> </nav> @@ -282,9 +344,15 @@ <h1 id="devops-in-action-guide">Devops in Action - Guide</h1> -<p>For each step you have a td to discover the subject and a tp to put it into practice.<br /> -The tps follow each other and the goal is to make you start from a local application and get to an application delivered in production and accessible to all.<br /> -For that we will give you each a server and a java application.</p> +<p>For each step you have a TD to discover the subject and a TP to put it into practice.<br /> +The TPs follow each other and the goal is to make you start from a local application and get to an application delivered in production and accessible to all.<br /> +For that we will give you each a server and a Java application.</p> +<p><strong>Part 1 - Docker session</strong></p> +<p><a href="ch1-discover-docker-td/">Docker TDs are available here</a> <br /> +<a href="ch1-discover-docker-tp/">Docker TPs are available here</a> <br /> +<a href="https://docs.google.com/presentation/d/1YRcnfqvw6rWccCdiAPWjDcV7R2ThiGRswUB-CV1O9ug/edit?usp=sharing">Docker slides are available here</a></p> +<p><strong>Please read the indications carefully, most of the time what you need is in front of your eyes!</strong></p> +<p align="center">© Takima 2025</p> </article> @@ -295,26 +363,6 @@ For that we will give you each a server and a java application.</p> <footer class="md-footer"> - <nav class="md-footer__inner md-grid" aria-label="Footer"> - - - - <a href="cheatsheet/" class="md-footer__link md-footer__link--next" aria-label="Next: Cheatsheet" rel="next"> - <div class="md-footer__title"> - <div class="md-ellipsis"> - <span class="md-footer__direction"> - Next - </span> - Cheatsheet - </div> - </div> - <div class="md-footer__button md-icon"> - <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg> - </div> - </a> - - </nav> - <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class="md-copyright"> diff --git a/public/search/search_index.json b/public/search/search_index.json index 341d80e..73a3df5 100644 --- a/public/search/search_index.json +++ b/public/search/search_index.json @@ -1 +1 @@ -{"config":{"indexing":"full","lang":["en"],"min_search_length":3,"prebuild_index":false,"separator":"[\\s\\-]+"},"docs":[{"location":"","text":"Devops in Action - Guide For each step you have a td to discover the subject and a tp to put it into practice. The tps follow each other and the goal is to make you start from a local application and get to an application delivered in production and accessible to all. For that we will give you each a server and a java application.","title":"Devops in Action - Guide"},{"location":"#devops-in-action-guide","text":"For each step you have a td to discover the subject and a tp to put it into practice. The tps follow each other and the goal is to make you start from a local application and get to an application delivered in production and accessible to all. For that we will give you each a server and a java application.","title":"Devops in Action - Guide"},{"location":"cheatsheet/","text":"Cheatsheet","title":"Cheatsheet"},{"location":"cheatsheet/#cheatsheet","text":"","title":"Cheatsheet"}]} \ No newline at end of file +{"config":{"indexing":"full","lang":["en"],"min_search_length":3,"prebuild_index":false,"separator":"[\\s\\-]+"},"docs":[{"location":"","text":"Devops in Action - Guide For each step you have a TD to discover the subject and a TP to put it into practice. The TPs follow each other and the goal is to make you start from a local application and get to an application delivered in production and accessible to all. For that we will give you each a server and a Java application. Part 1 - Docker session Docker TDs are available here Docker TPs are available here Docker slides are available here Please read the indications carefully, most of the time what you need is in front of your eyes! \u00a9 Takima 2025","title":"Devops in Action - Guide"},{"location":"#devops-in-action-guide","text":"For each step you have a TD to discover the subject and a TP to put it into practice. The TPs follow each other and the goal is to make you start from a local application and get to an application delivered in production and accessible to all. For that we will give you each a server and a Java application. Part 1 - Docker session Docker TDs are available here Docker TPs are available here Docker slides are available here Please read the indications carefully, most of the time what you need is in front of your eyes! \u00a9 Takima 2025","title":"Devops in Action - Guide"},{"location":"ch1-discover-docker-td/","text":"Discover Docker Check Checkpoint: call us to check your results (don\u2019t stay blocked on a checkpoint if we are busy, we can check \u2154 checkpoints at the same time) Question Point to document/report Tip Interesting information Setup Prerequisites There are no specific skills needed for this tutorial beyond a basic comfort with the command line and using a text editor. Prior experience in developing web applications will be helpful but is not required. As you proceed further along the tutorial, we'll make use of https://cloud.docker.com/. Setting up your computer Getting all the tooling setup on your computer can be a daunting task, but getting Docker up and running on your favorite OS has become very easy. The getting started guide on Docker has detailed instructions for setting up Docker on Mac , Linux and Windows If you're using Docker for Windows make sure you have shared your drive. Important note If you're using an older version of Windows or MacOS you may need to use Docker Machine instead. All commands work in either bash or Powershell on Windows Once you are done installing Docker, test your Docker installation by running the following: docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 03f4658f8b78: Pull complete a3ed95caeb02: Pull complete Digest: sha256:8be990ef2aeb16dbcb9271ddfe2610fa6658d13f6dfb8bc72074cc1ca36966a7 Status: Downloaded newer image for hello-world:latest Hello from Docker. ... This message shows that your installation appears to be working correctly. Running your first container Now that you have everything setup, it's time to get our hands dirty. In this section, you are going to run an Alpine Linux container (a lightweight linux distribution) on your system and get a taste of the docker run command. To get started, let's run the following in our terminal: docker pull alpine Note Depending on how you've installed docker on your system, you might see a permission denied error after running the above command. Try the commands from the Getting Started tutorial to verify your installation . If you're on Linux, you may need to prefix your docker commands with sudo . Alternatively you can create a docker group to get rid of this issue. The pull command fetches the alpine image from the Docker registry and saves it in our system. You can use the docker images command to see a list of all images on your system. docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE alpine latest c51f86c28340 4 weeks ago 1 .109 MB hello-world latest 690ed74de00f 5 months ago 960 B 1.1 Docker Run Great! Let's now run a Docker container based on this image. To do that you are going to use the docker run command. docker run alpine ls -l total 48 drwxr-xr-x 2 root root 4096 Mar 2 16:20 bin drwxr-xr-x 5 root root 360 Mar 18 09:47 dev drwxr-xr-x 13 root root 4096 Mar 18 09:47 etc drwxr-xr-x 2 root root 4096 Mar 2 16:20 home drwxr-xr-x 5 root root 4096 Mar 2 16:20 lib ...... ...... What happened? Behind the scenes, a lot of stuff happened. When you call run : 1. The Docker client contacts the Docker daemon. The Docker daemon checks local store if the image (alpine in this case) is available locally, and if not, downloads it from Docker Store. (Since we have issued docker pull alpine before, the download step is not necessary) The Docker daemon creates the container and then runs a command in that container. The Docker daemon streams the output of the command to the Docker client When you run docker run alpine , you provided a command ( ls -l ), so Docker started the command specified and you saw the listing. Let's try something more exciting. docker run alpine echo \"hello from alpine\" hello from alpine OK, that's some actual output. In this case, the Docker client dutifully ran the echo command in our alpine container and then exited it. If you've noticed, all of that happened pretty quickly. Imagine booting up a virtual machine, running a command and then killing it. Now you know why they say containers are fast! Try another command. docker run alpine /bin/sh Wait, nothing happened! Is that a bug? Well, no. These interactive shells will exit after running any scripted commands, unless they are run in an interactive terminal - so for this example to not exit, you need to docker run -it alpine /bin/sh . You are now inside the container shell and you can try out a few commands like ls -l , uname -a and others. Exit out of the container by giving the exit command. Ok, now it's time to see the docker ps command. The docker ps command shows you all containers that are currently running. docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES Since no containers are running, you see a blank line. Let's try a more useful variant: docker ps -a docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 36171a5da744 alpine \"/bin/sh\" 5 minutes ago Exited ( 0 ) 2 minutes ago fervent_newton a6a9d46d0b2f alpine \"echo 'hello from alp\" 6 minutes ago Exited ( 0 ) 6 minutes ago lonely_kilby ff0a5c3750b9 alpine \"ls -l\" 8 minutes ago Exited ( 0 ) 8 minutes ago elated_ramanujan c317d0a9e3d2 hello-world \"/hello\" 34 seconds ago Exited ( 0 ) 12 minutes ago stupefied_mcclintock What you see above is a list of all containers that you ran. Notice that the STATUS column shows that these containers exited a few minutes ago. You're probably wondering if there is a way to run more than just one command in a container. Let's try that now: docker run -it alpine /bin/sh / # ls bin dev etc home lib linuxrc media mnt proc root run sbin sys tmp usr var / # uname -a Linux 97916e8cb5dc 4.4.27-moby #1 SMP Wed Oct 26 14:01:48 UTC 2016 x86_64 Linux Running the run command with the -it flags attaches us to an interactive tty in the container. Now you can run as many commands in the container as you want. Take some time to run your favorite commands. Tip run -it is a very useful command to debug at the lowest level a container. That concludes a whirlwind tour of the docker run command which would most likely be the command you'll use most often. It makes sense to spend some time getting comfortable with it. To find out more about run , use docker run --help to see a list of all flags it supports. As you proceed further, we'll see a few more variants of docker run. 1.2 Terminology In the last section, you saw a lot of Docker-specific jargon which might be confusing to some. So before you go further, let's clarify some terminology that is used frequently in the Docker ecosystem. Images - The file system and configuration of our application which are used to create containers. To find out more about a Docker image, run docker inspect alpine . In the demo above, you used the docker pull command to download the alpine image. When you executed the command docker run hello-world , it also did a docker pull behind the scenes to download the hello-world image. Containers - Running instances of Docker images \u2014 containers run the actual applications. A container includes an application and all of its dependencies. It shares the kernel with other containers, and runs as an isolated process in user space on the host OS. You created a container using docker run which you did using the alpine image that you downloaded. A list of running containers can be seen using the docker ps command. Docker daemon - The background service running on the host that manages building, running and distributing Docker containers. Docker client - The command line tool that allows the user to interact with the Docker daemon. Docker Store - A registry of Docker images, where you can find trusted and enterprise ready containers, plugins, and Docker editions. You'll be using this later in this tutorial. 2.0 Webapps with Docker Great! So you have now looked at docker run , played with a Docker container and also got the hang of some terminology. Armed with all this knowledge, you are now ready to get to the real stuff \u2014 deploying web applications with Docker. 2.1 Run a static website in a container Note Code for this section is in this repo in the static-site directory Let's start by taking baby-steps. First, we'll use Docker to run a static website in a container. The website is based on an existing image. We'll pull a Docker image from Docker Store, run the container, and see how easy it is to set up a web server. The image that you are going to use is a single-page website that was already created for this demo and is available on the Docker Store as dockersamples/static-site . You can download and run the image directly in one go using docker run as follows. docker run -d dockersamples/static-site Note The current version of this image doesn't run without the -d flag. The -d flag enables detached mode, which detaches the running container from the terminal/shell and returns your prompt after the container starts. We are debugging the problem with this image but for now, use -d even for this first example. Tip -d is a very useful option. So, what happens when you run this command? Since the image doesn't exist on your Docker host, the Docker daemon first fetches it from the registry and then runs it as a container. Now that the server is running, do you see the website? What port is it running on? And more importantly, how do you access the container directly from our host machine? Actually, you probably won't be able to answer any of these questions yet! \u263a In this case, the client didn't tell the Docker Engine to publish any of the ports, so you need to re-run the docker run command to add this instruction. Let's re-run the command with some new flags to publish ports and pass your name to the container to customize the message displayed. We'll use the -d option again to run the container in detached mode. First, stop the container that you have just launched. In order to do this, we need the container ID. Since we ran the container in detached mode, we don't have to launch another terminal to do this. Run docker ps to view the running containers. docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a7a0e504ca3e dockersamples/static-site \"/bin/sh -c 'cd /usr/\" 28 seconds ago Up 26 seconds 80 /tcp, 443 /tcp stupefied_mahavira Check out the CONTAINER ID column. You will need to use this CONTAINER ID value, a long sequence of characters, to identify the container you want to stop, and then to remove it. The example below provides the CONTAINER ID on our system; you should use the value that you see in your terminal. docker stop a7a0e504ca3e docker rm a7a0e504ca3e Note A cool feature is that you do not need to specify the entire CONTAINER ID . You can just specify a few starting characters and if it is unique among all the containers that you have launched, the Docker client will intelligently pick it up. Now, let's launch a container in detached mode as shown below: docker run --name static-site -e AUTHOR = \"Enter Your Name Here\" -d -P dockersamples/static-site e61d12292d69556eabe2a44c16cbd54486b2527e2ce4f95438e504afb7b02810 In the above command: -d will create a container with the process detached from our terminal -P will publish all the exposed container ports to random ports on the Docker host -e is how you pass environment variables to the container. --name allows you to specify a container name AUTHOR is the environment variable name and Your Name is the value that you can pass. Now you can see the ports by running the docker port command. docker port static-site 443 /tcp -> 0 .0.0.0:32772 80 /tcp -> 0 .0.0.0:32773 You can open your freshly created website on http://localhost:[YOUR_PORT_FOR 80/tcp] . For our example this is http://localhost:32773 . You can now open http://localhost:[YOUR_PORT_FOR 80/tcp] to see your site live! For our example, this is: http://192.168.99.100:32773 . You can also run a second webserver at the same time, specifying a custom host port mapping to the container's webserver. docker run --name static-site-2 -e AUTHOR = \"Enter Your Name Here\" -d -p 8888 :80 dockersamples/static-site To deploy this on a real server you would just need to install Docker, and run the above docker command (as in this case you can see the AUTHOR is Docker which we passed as an environment variable). Now that you've seen how to run a webserver inside a Docker container, how do you create your own Docker image? This is the question we'll explore in the next section. But first, let's stop and remove the containers since you won't be using them anymore. docker stop static-site docker rm static-site Let's use a shortcut to remove the second site: docker rm -f static-site-2 Tip rm -f is a very useful option Run docker ps to make sure the containers are gone. docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2.2 Docker Images In this section, let's dive deeper into what Docker images are. You will build your own image, use that image to run an application locally, and finally, push some of your own images to Docker Cloud. Docker images are the basis of containers. In the previous example, you pulled the dockersamples/static-site image from the. registry and asked the Docker client to run a container based on that image. To see the list of images that are available locally on your system, run the docker images command. docker images REPOSITORY TAG IMAGE ID CREATED SIZE dockersamples/static-site latest 92a386b6e686 2 hours ago 190 .5 MB nginx latest af4b3d7d5401 3 hours ago 190 .5 MB python 2 .7 1c32174fd534 14 hours ago 676 .8 MB postgres 9 .4 88d845ac7a88 14 hours ago 263 .6 MB containous/traefik latest 27b4e0c6b2fd 4 days ago 20 .75 MB node 0 .10 42426a5cba5f 6 days ago 633 .7 MB redis latest 4f5f397d4b7c 7 days ago 177 .5 MB mongo latest 467eb21035a8 7 days ago 309 .7 MB alpine 3 .3 70c557e50ed6 8 days ago 4 .794 MB java 7 21f6ce84e43c 8 days ago 587 .7 MB Above is a list of images that I've pulled from the registry and those I've created myself (we'll shortly see how). You will have a different list of images on your machine. The TAG refers to a particular snapshot of the image and the ID is the corresponding unique identifier for that image. For simplicity, you can think of an image akin to a git repository - images can be committed with changes and have multiple. versions. When you do not provide a specific version number, the client defaults to latest. For example you could pull a specific version of ubuntu image as follows: docker pull ubuntu:12.04 If you do not specify the version number of the image then, as mentioned, the Docker client will default to a version named latest . So for example, the docker pull command given below will pull an image named ubuntu:latest : docker pull ubuntu To get a new Docker image you can either get it from a registry (such as the Docker Store) or create your own. There are hundreds of thousands of images available on Docker Store . You can also search for images directly from the command line using docker search . An important distinction with regard to images is between base images and child images . Base images are images that have no parent images, usually images with an OS like ubuntu, alpine or debian. Child images are images that build on base images and add additional functionality. Another key concept is the idea of official images and user images. (Both of which can be base images or child images.) Official images are Docker sanctioned images. Docker, Inc. sponsors a dedicated team that is responsible for reviewing and publishing all Official Repositories content. This team works in collaboration with upstream software maintainers, security experts, and the broader Docker community. These are not prefixed by an organization or user name. In the list of images above, the python , node , alpine and nginx images are official (base) images. To find out more about them, check out the Official Images Documentation . User images are images created and shared by users like you. They build on base images and add additional functionality. Typically these are formatted as user/image-name . The user value in the image name is your Docker Store user or organization name. 2.3 Create your first image Now that you have a better understanding of images, it's time to create your own. Our main objective here is to create an image that sandboxes a small Flask application. The goal of this exercise is to create a Docker image which will run a Flask app. We'll do this by first pulling together the components for a random cat picture generator built with Python Flask, then dockerizing it by writing a Dockerfile . Finally, we'll build the image, and then run it. 2.3.1 Create a Python Flask app that displays random cat pix. For the purposes of this workshop, we've created a fun little Python Flask app that displays a random cat .gif every time it is loaded - because, you know, who doesn't like cats? Start by creating a directory called flask-app where we'll create the following files: app.py requirements.txt templates/index.html Dockerfile Make sure to cd flask-app before you start creating the files, because you don't want to start adding a whole bunch of other random files to your image. app.py Create the app.py with the following content: from flask import Flask , render_template import random app = Flask ( __name__ ) # list of cat images images = [ \"https://c.tenor.com/GTcT7HODLRgAAAAM/smiling-cat-creepy-cat.gif\" , \"https://media0.giphy.com/media/10dU7AN7xsi1I4/giphy.webp?cid=ecf05e47gk63rd81vzlot57qmebr7drtgf6a3khmzvjsdtu7&rid=giphy.webp&ct=g\" , \"https://media0.giphy.com/media/S6VGjvmFRu5Qk/giphy.webp?cid=ecf05e478yofpawrhffnnvb3sgjkos96vyfo5mtqhds35as6&rid=giphy.webp&ct=g\" , \"https://media3.giphy.com/media/JIX9t2j0ZTN9S/200w.webp?cid=ecf05e47gk63rd81vzlot57qmebr7drtgf6a3khmzvjsdtu7&rid=200w.webp&ct=g\" ] @app . route ( '/' ) def index (): url = random . choice ( images ) return render_template ( 'index.html' , url = url ) if __name__ == \"__main__\" : app . run ( host = \"0.0.0.0\" ) requirements.txt In order to install the Python modules required for our app, we need to create a file called requirements.txt and add the following line to that file: Flask==3.1.0 templates/index.html Create a directory called templates and create an index.html file in that directory with the following content in it: < html > < head > < style type = \"text/css\" > body { background : black ; color : white ; } div . container { max-width : 500 px ; margin : 100 px auto ; border : 20 px solid white ; padding : 10 px ; text-align : center ; } h4 { text-transform : uppercase ; } </ style > </ head > < body > < div class = \"container\" > < h4 > Cat Gif of the day </ h4 > < img src = \"{{url}}\" /> < p >< small > Courtesy: < a href = \"http://www.buzzfeed.com/copyranter/the-best-cat-gif-post-in-the-history-of-cat-gifs\" > Buzzfeed </ a ></ small ></ p > </ div > </ body > </ html > 2.3.2 Write a Dockerfile We want to create a Docker image with this web app. As mentioned above, all user images are based on a base image. Since our application is written in Python, we will build our own Python image based on Alpine . We'll do that using a Dockerfile. A Dockerfile is a text file that contains a list of commands that the Docker daemon calls while creating an image. The Dockerfile contains all the information that Docker needs to know to run the app \u2014 a base Docker image to run from, location of your project code, any dependencies it has, and what commands to run at start-up. It is a simple way to automate the image creation process. The best part is that the commands you write in a Dockerfile are almost identical to their equivalent Linux commands. This means you don't really have to learn new syntax to create your own Dockerfiles. 1 - Create a file called Dockerfile, and add content to it as described below. We'll start by specifying our base image, using the FROM keyword. We are using alpine:3.21.0, a lightweight Linux distribution that helps keep our container small and efficient: FROM alpine:3.21.0 2 - Next, we need to install Python 3, pip, and other system dependencies required for our application. The apk add command is used to install packages in Alpine Linux. We use --no-cache to prevent unnecessary image bloat. Add the following RUN command: RUN apk add --no-cache build-base libffi-dev openssl-dev py3-pip python3 3 - Now, we set the working directory inside the container. This ensures that all subsequent commands run within this directory: WORKDIR /usr/src/app 4 - To create an isolated Python environment, we set up a virtual environment inside our container. This helps prevent conflicts between system-wide and project-specific dependencies: RUN python3 -m venv venv 5 - To ensure that all commands within the container use the virtual environment by default, we modify the PATH environment variable: ENV PATH = \"/usr/src/app/venv/bin: $PATH \" 6 - Next, we copy the application's dependencies file (requirements.txt) into the container and install the necessary Python packages. We also upgrade pip to the latest version to ensure compatibility: COPY requirements.txt ./ RUN pip install --no-cache-dir --upgrade pip && pip install --no-cache-dir -r requirements.txt 7 - Copy the files you have created earlier into our image by using COPY command. COPY app.py ./ COPY templates/index.html ./templates/ 8 - Since our Flask application runs on port 5000, we specify that this port should be exposed. This does not automatically publish the port but serves as documentation and can be used by orchestration tools: EXPOSE 5000 9 - The last step is the command for running the application which is simply - python ./app.py . Use the CMD command to do that: CMD [ \"python\" , \"/usr/src/app/app.py\" ] The primary purpose of CMD is to tell the container which command it should run by default when it is started. 10 - Verify your Dockerfile. Our Dockerfile is now ready. This is how it looks: # our base image FROM alpine:3.21.0 # Install Python 3, pip, and system dependencies RUN apk add --no-cache build-base libffi-dev openssl-dev py3-pip python3 # Set the working directory WORKDIR /usr/src/app # Create and activate a virtual environment RUN python3 -m venv venv # Use the virtual environment for all commands ENV PATH = \"/usr/src/app/venv/bin: $PATH \" # Copy and install dependencies COPY requirements.txt ./ RUN pip install --no-cache-dir --upgrade pip && pip install --no-cache-dir -r requirements.txt # Copy application files COPY app.py ./ COPY templates/index.html ./templates/ # Expose the application port EXPOSE 5000 # Run the application inside the virtual environment CMD [ \"python\" , \"/usr/src/app/app.py\" ] 2.3.3 Build the image Now that you have your Dockerfile , you can build your image. The docker build command does the heavy-lifting of creating a docker image from a Dockerfile . When you run the docker build command given below, make sure to replace <YOUR_USERNAME> with your username. This username should be the same one you created when registering on Docker Cloud . If you haven't done that yet, please go ahead and create an account. The docker build command is quite simple - it takes an optional tag name with the -t flag, and the location of the directory containing the Dockerfile - the . indicates the current directory: docker build -t <YOUR_USERNAME>/myfirstapp . If you don't have the alpine:3.21.0 image, the client will first pull the image and then create your image. Therefore, your output on running the command will look different from mine. If everything went well, your image should be ready! Run docker images and see if your image ( <YOUR_USERNAME>/myfirstapp ) shows. 2.3.4 Run your image The next step in this section is to run the image and see if it actually works. docker run -p 8888 :5000 --name myfirstapp YOUR_USERNAME/myfirstapp * Serving Flask app 'app' * Debug mode: off WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on all addresses ( 0 .0.0.0 ) * Running on http://127.0.0.1:5000 * Running on http://172.17.0.2:5000 Press CTRL+C to quit Head over to http://localhost:8888 and your app should be live. Note If you are using Docker Machine, you may need to open up another terminal and determine the container ip address using docker-machine ip default . Hit the Refresh button in the web browser to see a few more cat images. Check Show us your running flask-app ! 2.3.4 Dockerfile commands summary Here's a quick summary of the few basic commands we used in our Dockerfile. FROM starts the Dockerfile. It is a requirement that the Dockerfile must start with the FROM command. Images are created in layers, which means you can use another image as the base image for your own. The FROM command defines your base layer. As arguments, it takes the name of the image. Optionally, you can add the Docker Cloud username of the maintainer and image version, in the format username/imagename:version . RUN is used to build up the Image you're creating. For each RUN command, Docker will run the command then create a new layer of the image. This way you can roll back your image to previous states easily. The syntax for a RUN instruction is to place the full text of the shell command after the RUN (e.g., RUN mkdir /user/local/foo ). This will automatically run in a /bin/sh shell. You can define a different shell like this: RUN /bin/bash -c 'mkdir /user/local/foo ' COPY copies local files into the container. CMD defines the commands that will run on the Image at start-up. Unlike a RUN , this does not create a new layer for the Image, but simply runs the command. There can only be one CMD per a Dockerfile/Image. If you need to run multiple commands, the best way to do that is to have the CMD run a script. CMD requires that you tell it where to run the command, unlike RUN . So example CMD commands would be: CMD [ \"python\" , \"./app.py\" ] CMD [ \"/bin/bash\" , \"echo\" , \"Hello World\" ] EXPOSE creates a hint for users of an image which ports provide services. It is included in the information which can be retrieved via docker inspect <container-id> . Note The EXPOSE command does not actually make any ports accessible to the host! Instead, this requires publishing ports by means of the -p flag when using docker run . Note If you want to learn more about Dockerfiles, check out Best practices for writing Dockerfiles . (source: https://github.com/docker/labs/tree/master/beginner ) Now that you know how to run docker container and create Dockerfiles let\u2019s move on to the practical part.","title":"TD part 01 - Docker"},{"location":"ch1-discover-docker-td/#discover-docker","text":"Check Checkpoint: call us to check your results (don\u2019t stay blocked on a checkpoint if we are busy, we can check \u2154 checkpoints at the same time) Question Point to document/report Tip Interesting information","title":"Discover Docker"},{"location":"ch1-discover-docker-td/#setup","text":"","title":"Setup"},{"location":"ch1-discover-docker-td/#prerequisites","text":"There are no specific skills needed for this tutorial beyond a basic comfort with the command line and using a text editor. Prior experience in developing web applications will be helpful but is not required. As you proceed further along the tutorial, we'll make use of https://cloud.docker.com/.","title":"Prerequisites"},{"location":"ch1-discover-docker-td/#setting-up-your-computer","text":"Getting all the tooling setup on your computer can be a daunting task, but getting Docker up and running on your favorite OS has become very easy. The getting started guide on Docker has detailed instructions for setting up Docker on Mac , Linux and Windows If you're using Docker for Windows make sure you have shared your drive. Important note If you're using an older version of Windows or MacOS you may need to use Docker Machine instead. All commands work in either bash or Powershell on Windows Once you are done installing Docker, test your Docker installation by running the following: docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 03f4658f8b78: Pull complete a3ed95caeb02: Pull complete Digest: sha256:8be990ef2aeb16dbcb9271ddfe2610fa6658d13f6dfb8bc72074cc1ca36966a7 Status: Downloaded newer image for hello-world:latest Hello from Docker. ... This message shows that your installation appears to be working correctly.","title":"Setting up your computer"},{"location":"ch1-discover-docker-td/#running-your-first-container","text":"Now that you have everything setup, it's time to get our hands dirty. In this section, you are going to run an Alpine Linux container (a lightweight linux distribution) on your system and get a taste of the docker run command. To get started, let's run the following in our terminal: docker pull alpine Note Depending on how you've installed docker on your system, you might see a permission denied error after running the above command. Try the commands from the Getting Started tutorial to verify your installation . If you're on Linux, you may need to prefix your docker commands with sudo . Alternatively you can create a docker group to get rid of this issue. The pull command fetches the alpine image from the Docker registry and saves it in our system. You can use the docker images command to see a list of all images on your system. docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE alpine latest c51f86c28340 4 weeks ago 1 .109 MB hello-world latest 690ed74de00f 5 months ago 960 B","title":"Running your first container"},{"location":"ch1-discover-docker-td/#11-docker-run","text":"Great! Let's now run a Docker container based on this image. To do that you are going to use the docker run command. docker run alpine ls -l total 48 drwxr-xr-x 2 root root 4096 Mar 2 16:20 bin drwxr-xr-x 5 root root 360 Mar 18 09:47 dev drwxr-xr-x 13 root root 4096 Mar 18 09:47 etc drwxr-xr-x 2 root root 4096 Mar 2 16:20 home drwxr-xr-x 5 root root 4096 Mar 2 16:20 lib ...... ...... What happened? Behind the scenes, a lot of stuff happened. When you call run : 1. The Docker client contacts the Docker daemon. The Docker daemon checks local store if the image (alpine in this case) is available locally, and if not, downloads it from Docker Store. (Since we have issued docker pull alpine before, the download step is not necessary) The Docker daemon creates the container and then runs a command in that container. The Docker daemon streams the output of the command to the Docker client When you run docker run alpine , you provided a command ( ls -l ), so Docker started the command specified and you saw the listing. Let's try something more exciting. docker run alpine echo \"hello from alpine\" hello from alpine OK, that's some actual output. In this case, the Docker client dutifully ran the echo command in our alpine container and then exited it. If you've noticed, all of that happened pretty quickly. Imagine booting up a virtual machine, running a command and then killing it. Now you know why they say containers are fast! Try another command. docker run alpine /bin/sh Wait, nothing happened! Is that a bug? Well, no. These interactive shells will exit after running any scripted commands, unless they are run in an interactive terminal - so for this example to not exit, you need to docker run -it alpine /bin/sh . You are now inside the container shell and you can try out a few commands like ls -l , uname -a and others. Exit out of the container by giving the exit command. Ok, now it's time to see the docker ps command. The docker ps command shows you all containers that are currently running. docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES Since no containers are running, you see a blank line. Let's try a more useful variant: docker ps -a docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 36171a5da744 alpine \"/bin/sh\" 5 minutes ago Exited ( 0 ) 2 minutes ago fervent_newton a6a9d46d0b2f alpine \"echo 'hello from alp\" 6 minutes ago Exited ( 0 ) 6 minutes ago lonely_kilby ff0a5c3750b9 alpine \"ls -l\" 8 minutes ago Exited ( 0 ) 8 minutes ago elated_ramanujan c317d0a9e3d2 hello-world \"/hello\" 34 seconds ago Exited ( 0 ) 12 minutes ago stupefied_mcclintock What you see above is a list of all containers that you ran. Notice that the STATUS column shows that these containers exited a few minutes ago. You're probably wondering if there is a way to run more than just one command in a container. Let's try that now: docker run -it alpine /bin/sh / # ls bin dev etc home lib linuxrc media mnt proc root run sbin sys tmp usr var / # uname -a Linux 97916e8cb5dc 4.4.27-moby #1 SMP Wed Oct 26 14:01:48 UTC 2016 x86_64 Linux Running the run command with the -it flags attaches us to an interactive tty in the container. Now you can run as many commands in the container as you want. Take some time to run your favorite commands. Tip run -it is a very useful command to debug at the lowest level a container. That concludes a whirlwind tour of the docker run command which would most likely be the command you'll use most often. It makes sense to spend some time getting comfortable with it. To find out more about run , use docker run --help to see a list of all flags it supports. As you proceed further, we'll see a few more variants of docker run.","title":"1.1 Docker Run"},{"location":"ch1-discover-docker-td/#12-terminology","text":"In the last section, you saw a lot of Docker-specific jargon which might be confusing to some. So before you go further, let's clarify some terminology that is used frequently in the Docker ecosystem. Images - The file system and configuration of our application which are used to create containers. To find out more about a Docker image, run docker inspect alpine . In the demo above, you used the docker pull command to download the alpine image. When you executed the command docker run hello-world , it also did a docker pull behind the scenes to download the hello-world image. Containers - Running instances of Docker images \u2014 containers run the actual applications. A container includes an application and all of its dependencies. It shares the kernel with other containers, and runs as an isolated process in user space on the host OS. You created a container using docker run which you did using the alpine image that you downloaded. A list of running containers can be seen using the docker ps command. Docker daemon - The background service running on the host that manages building, running and distributing Docker containers. Docker client - The command line tool that allows the user to interact with the Docker daemon. Docker Store - A registry of Docker images, where you can find trusted and enterprise ready containers, plugins, and Docker editions. You'll be using this later in this tutorial.","title":"1.2 Terminology"},{"location":"ch1-discover-docker-td/#20-webapps-with-docker","text":"Great! So you have now looked at docker run , played with a Docker container and also got the hang of some terminology. Armed with all this knowledge, you are now ready to get to the real stuff \u2014 deploying web applications with Docker.","title":"2.0 Webapps with Docker"},{"location":"ch1-discover-docker-td/#21-run-a-static-website-in-a-container","text":"Note Code for this section is in this repo in the static-site directory Let's start by taking baby-steps. First, we'll use Docker to run a static website in a container. The website is based on an existing image. We'll pull a Docker image from Docker Store, run the container, and see how easy it is to set up a web server. The image that you are going to use is a single-page website that was already created for this demo and is available on the Docker Store as dockersamples/static-site . You can download and run the image directly in one go using docker run as follows. docker run -d dockersamples/static-site Note The current version of this image doesn't run without the -d flag. The -d flag enables detached mode, which detaches the running container from the terminal/shell and returns your prompt after the container starts. We are debugging the problem with this image but for now, use -d even for this first example. Tip -d is a very useful option. So, what happens when you run this command? Since the image doesn't exist on your Docker host, the Docker daemon first fetches it from the registry and then runs it as a container. Now that the server is running, do you see the website? What port is it running on? And more importantly, how do you access the container directly from our host machine? Actually, you probably won't be able to answer any of these questions yet! \u263a In this case, the client didn't tell the Docker Engine to publish any of the ports, so you need to re-run the docker run command to add this instruction. Let's re-run the command with some new flags to publish ports and pass your name to the container to customize the message displayed. We'll use the -d option again to run the container in detached mode. First, stop the container that you have just launched. In order to do this, we need the container ID. Since we ran the container in detached mode, we don't have to launch another terminal to do this. Run docker ps to view the running containers. docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a7a0e504ca3e dockersamples/static-site \"/bin/sh -c 'cd /usr/\" 28 seconds ago Up 26 seconds 80 /tcp, 443 /tcp stupefied_mahavira Check out the CONTAINER ID column. You will need to use this CONTAINER ID value, a long sequence of characters, to identify the container you want to stop, and then to remove it. The example below provides the CONTAINER ID on our system; you should use the value that you see in your terminal. docker stop a7a0e504ca3e docker rm a7a0e504ca3e Note A cool feature is that you do not need to specify the entire CONTAINER ID . You can just specify a few starting characters and if it is unique among all the containers that you have launched, the Docker client will intelligently pick it up. Now, let's launch a container in detached mode as shown below: docker run --name static-site -e AUTHOR = \"Enter Your Name Here\" -d -P dockersamples/static-site e61d12292d69556eabe2a44c16cbd54486b2527e2ce4f95438e504afb7b02810 In the above command: -d will create a container with the process detached from our terminal -P will publish all the exposed container ports to random ports on the Docker host -e is how you pass environment variables to the container. --name allows you to specify a container name AUTHOR is the environment variable name and Your Name is the value that you can pass. Now you can see the ports by running the docker port command. docker port static-site 443 /tcp -> 0 .0.0.0:32772 80 /tcp -> 0 .0.0.0:32773 You can open your freshly created website on http://localhost:[YOUR_PORT_FOR 80/tcp] . For our example this is http://localhost:32773 . You can now open http://localhost:[YOUR_PORT_FOR 80/tcp] to see your site live! For our example, this is: http://192.168.99.100:32773 . You can also run a second webserver at the same time, specifying a custom host port mapping to the container's webserver. docker run --name static-site-2 -e AUTHOR = \"Enter Your Name Here\" -d -p 8888 :80 dockersamples/static-site To deploy this on a real server you would just need to install Docker, and run the above docker command (as in this case you can see the AUTHOR is Docker which we passed as an environment variable). Now that you've seen how to run a webserver inside a Docker container, how do you create your own Docker image? This is the question we'll explore in the next section. But first, let's stop and remove the containers since you won't be using them anymore. docker stop static-site docker rm static-site Let's use a shortcut to remove the second site: docker rm -f static-site-2 Tip rm -f is a very useful option Run docker ps to make sure the containers are gone. docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES","title":"2.1 Run a static website in a container"},{"location":"ch1-discover-docker-td/#22-docker-images","text":"In this section, let's dive deeper into what Docker images are. You will build your own image, use that image to run an application locally, and finally, push some of your own images to Docker Cloud. Docker images are the basis of containers. In the previous example, you pulled the dockersamples/static-site image from the. registry and asked the Docker client to run a container based on that image. To see the list of images that are available locally on your system, run the docker images command. docker images REPOSITORY TAG IMAGE ID CREATED SIZE dockersamples/static-site latest 92a386b6e686 2 hours ago 190 .5 MB nginx latest af4b3d7d5401 3 hours ago 190 .5 MB python 2 .7 1c32174fd534 14 hours ago 676 .8 MB postgres 9 .4 88d845ac7a88 14 hours ago 263 .6 MB containous/traefik latest 27b4e0c6b2fd 4 days ago 20 .75 MB node 0 .10 42426a5cba5f 6 days ago 633 .7 MB redis latest 4f5f397d4b7c 7 days ago 177 .5 MB mongo latest 467eb21035a8 7 days ago 309 .7 MB alpine 3 .3 70c557e50ed6 8 days ago 4 .794 MB java 7 21f6ce84e43c 8 days ago 587 .7 MB Above is a list of images that I've pulled from the registry and those I've created myself (we'll shortly see how). You will have a different list of images on your machine. The TAG refers to a particular snapshot of the image and the ID is the corresponding unique identifier for that image. For simplicity, you can think of an image akin to a git repository - images can be committed with changes and have multiple. versions. When you do not provide a specific version number, the client defaults to latest. For example you could pull a specific version of ubuntu image as follows: docker pull ubuntu:12.04 If you do not specify the version number of the image then, as mentioned, the Docker client will default to a version named latest . So for example, the docker pull command given below will pull an image named ubuntu:latest : docker pull ubuntu To get a new Docker image you can either get it from a registry (such as the Docker Store) or create your own. There are hundreds of thousands of images available on Docker Store . You can also search for images directly from the command line using docker search . An important distinction with regard to images is between base images and child images . Base images are images that have no parent images, usually images with an OS like ubuntu, alpine or debian. Child images are images that build on base images and add additional functionality. Another key concept is the idea of official images and user images. (Both of which can be base images or child images.) Official images are Docker sanctioned images. Docker, Inc. sponsors a dedicated team that is responsible for reviewing and publishing all Official Repositories content. This team works in collaboration with upstream software maintainers, security experts, and the broader Docker community. These are not prefixed by an organization or user name. In the list of images above, the python , node , alpine and nginx images are official (base) images. To find out more about them, check out the Official Images Documentation . User images are images created and shared by users like you. They build on base images and add additional functionality. Typically these are formatted as user/image-name . The user value in the image name is your Docker Store user or organization name.","title":"2.2 Docker Images"},{"location":"ch1-discover-docker-td/#23-create-your-first-image","text":"Now that you have a better understanding of images, it's time to create your own. Our main objective here is to create an image that sandboxes a small Flask application. The goal of this exercise is to create a Docker image which will run a Flask app. We'll do this by first pulling together the components for a random cat picture generator built with Python Flask, then dockerizing it by writing a Dockerfile . Finally, we'll build the image, and then run it.","title":"2.3 Create your first image"},{"location":"ch1-discover-docker-td/#231-create-a-python-flask-app-that-displays-random-cat-pix","text":"For the purposes of this workshop, we've created a fun little Python Flask app that displays a random cat .gif every time it is loaded - because, you know, who doesn't like cats? Start by creating a directory called flask-app where we'll create the following files: app.py requirements.txt templates/index.html Dockerfile Make sure to cd flask-app before you start creating the files, because you don't want to start adding a whole bunch of other random files to your image.","title":"2.3.1 Create a Python Flask app that displays random cat pix."},{"location":"ch1-discover-docker-td/#apppy","text":"Create the app.py with the following content: from flask import Flask , render_template import random app = Flask ( __name__ ) # list of cat images images = [ \"https://c.tenor.com/GTcT7HODLRgAAAAM/smiling-cat-creepy-cat.gif\" , \"https://media0.giphy.com/media/10dU7AN7xsi1I4/giphy.webp?cid=ecf05e47gk63rd81vzlot57qmebr7drtgf6a3khmzvjsdtu7&rid=giphy.webp&ct=g\" , \"https://media0.giphy.com/media/S6VGjvmFRu5Qk/giphy.webp?cid=ecf05e478yofpawrhffnnvb3sgjkos96vyfo5mtqhds35as6&rid=giphy.webp&ct=g\" , \"https://media3.giphy.com/media/JIX9t2j0ZTN9S/200w.webp?cid=ecf05e47gk63rd81vzlot57qmebr7drtgf6a3khmzvjsdtu7&rid=200w.webp&ct=g\" ] @app . route ( '/' ) def index (): url = random . choice ( images ) return render_template ( 'index.html' , url = url ) if __name__ == \"__main__\" : app . run ( host = \"0.0.0.0\" )","title":"app.py"},{"location":"ch1-discover-docker-td/#requirementstxt","text":"In order to install the Python modules required for our app, we need to create a file called requirements.txt and add the following line to that file: Flask==3.1.0","title":"requirements.txt"},{"location":"ch1-discover-docker-td/#templatesindexhtml","text":"Create a directory called templates and create an index.html file in that directory with the following content in it: < html > < head > < style type = \"text/css\" > body { background : black ; color : white ; } div . container { max-width : 500 px ; margin : 100 px auto ; border : 20 px solid white ; padding : 10 px ; text-align : center ; } h4 { text-transform : uppercase ; } </ style > </ head > < body > < div class = \"container\" > < h4 > Cat Gif of the day </ h4 > < img src = \"{{url}}\" /> < p >< small > Courtesy: < a href = \"http://www.buzzfeed.com/copyranter/the-best-cat-gif-post-in-the-history-of-cat-gifs\" > Buzzfeed </ a ></ small ></ p > </ div > </ body > </ html >","title":"templates/index.html"},{"location":"ch1-discover-docker-td/#232-write-a-dockerfile","text":"We want to create a Docker image with this web app. As mentioned above, all user images are based on a base image. Since our application is written in Python, we will build our own Python image based on Alpine . We'll do that using a Dockerfile. A Dockerfile is a text file that contains a list of commands that the Docker daemon calls while creating an image. The Dockerfile contains all the information that Docker needs to know to run the app \u2014 a base Docker image to run from, location of your project code, any dependencies it has, and what commands to run at start-up. It is a simple way to automate the image creation process. The best part is that the commands you write in a Dockerfile are almost identical to their equivalent Linux commands. This means you don't really have to learn new syntax to create your own Dockerfiles. 1 - Create a file called Dockerfile, and add content to it as described below. We'll start by specifying our base image, using the FROM keyword. We are using alpine:3.21.0, a lightweight Linux distribution that helps keep our container small and efficient: FROM alpine:3.21.0 2 - Next, we need to install Python 3, pip, and other system dependencies required for our application. The apk add command is used to install packages in Alpine Linux. We use --no-cache to prevent unnecessary image bloat. Add the following RUN command: RUN apk add --no-cache build-base libffi-dev openssl-dev py3-pip python3 3 - Now, we set the working directory inside the container. This ensures that all subsequent commands run within this directory: WORKDIR /usr/src/app 4 - To create an isolated Python environment, we set up a virtual environment inside our container. This helps prevent conflicts between system-wide and project-specific dependencies: RUN python3 -m venv venv 5 - To ensure that all commands within the container use the virtual environment by default, we modify the PATH environment variable: ENV PATH = \"/usr/src/app/venv/bin: $PATH \" 6 - Next, we copy the application's dependencies file (requirements.txt) into the container and install the necessary Python packages. We also upgrade pip to the latest version to ensure compatibility: COPY requirements.txt ./ RUN pip install --no-cache-dir --upgrade pip && pip install --no-cache-dir -r requirements.txt 7 - Copy the files you have created earlier into our image by using COPY command. COPY app.py ./ COPY templates/index.html ./templates/ 8 - Since our Flask application runs on port 5000, we specify that this port should be exposed. This does not automatically publish the port but serves as documentation and can be used by orchestration tools: EXPOSE 5000 9 - The last step is the command for running the application which is simply - python ./app.py . Use the CMD command to do that: CMD [ \"python\" , \"/usr/src/app/app.py\" ] The primary purpose of CMD is to tell the container which command it should run by default when it is started. 10 - Verify your Dockerfile. Our Dockerfile is now ready. This is how it looks: # our base image FROM alpine:3.21.0 # Install Python 3, pip, and system dependencies RUN apk add --no-cache build-base libffi-dev openssl-dev py3-pip python3 # Set the working directory WORKDIR /usr/src/app # Create and activate a virtual environment RUN python3 -m venv venv # Use the virtual environment for all commands ENV PATH = \"/usr/src/app/venv/bin: $PATH \" # Copy and install dependencies COPY requirements.txt ./ RUN pip install --no-cache-dir --upgrade pip && pip install --no-cache-dir -r requirements.txt # Copy application files COPY app.py ./ COPY templates/index.html ./templates/ # Expose the application port EXPOSE 5000 # Run the application inside the virtual environment CMD [ \"python\" , \"/usr/src/app/app.py\" ]","title":"2.3.2 Write a Dockerfile"},{"location":"ch1-discover-docker-td/#233-build-the-image","text":"Now that you have your Dockerfile , you can build your image. The docker build command does the heavy-lifting of creating a docker image from a Dockerfile . When you run the docker build command given below, make sure to replace <YOUR_USERNAME> with your username. This username should be the same one you created when registering on Docker Cloud . If you haven't done that yet, please go ahead and create an account. The docker build command is quite simple - it takes an optional tag name with the -t flag, and the location of the directory containing the Dockerfile - the . indicates the current directory: docker build -t <YOUR_USERNAME>/myfirstapp . If you don't have the alpine:3.21.0 image, the client will first pull the image and then create your image. Therefore, your output on running the command will look different from mine. If everything went well, your image should be ready! Run docker images and see if your image ( <YOUR_USERNAME>/myfirstapp ) shows.","title":"2.3.3 Build the image"},{"location":"ch1-discover-docker-td/#234-run-your-image","text":"The next step in this section is to run the image and see if it actually works. docker run -p 8888 :5000 --name myfirstapp YOUR_USERNAME/myfirstapp * Serving Flask app 'app' * Debug mode: off WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on all addresses ( 0 .0.0.0 ) * Running on http://127.0.0.1:5000 * Running on http://172.17.0.2:5000 Press CTRL+C to quit Head over to http://localhost:8888 and your app should be live. Note If you are using Docker Machine, you may need to open up another terminal and determine the container ip address using docker-machine ip default . Hit the Refresh button in the web browser to see a few more cat images. Check Show us your running flask-app !","title":"2.3.4 Run your image"},{"location":"ch1-discover-docker-td/#234-dockerfile-commands-summary","text":"Here's a quick summary of the few basic commands we used in our Dockerfile. FROM starts the Dockerfile. It is a requirement that the Dockerfile must start with the FROM command. Images are created in layers, which means you can use another image as the base image for your own. The FROM command defines your base layer. As arguments, it takes the name of the image. Optionally, you can add the Docker Cloud username of the maintainer and image version, in the format username/imagename:version . RUN is used to build up the Image you're creating. For each RUN command, Docker will run the command then create a new layer of the image. This way you can roll back your image to previous states easily. The syntax for a RUN instruction is to place the full text of the shell command after the RUN (e.g., RUN mkdir /user/local/foo ). This will automatically run in a /bin/sh shell. You can define a different shell like this: RUN /bin/bash -c 'mkdir /user/local/foo ' COPY copies local files into the container. CMD defines the commands that will run on the Image at start-up. Unlike a RUN , this does not create a new layer for the Image, but simply runs the command. There can only be one CMD per a Dockerfile/Image. If you need to run multiple commands, the best way to do that is to have the CMD run a script. CMD requires that you tell it where to run the command, unlike RUN . So example CMD commands would be: CMD [ \"python\" , \"./app.py\" ] CMD [ \"/bin/bash\" , \"echo\" , \"Hello World\" ] EXPOSE creates a hint for users of an image which ports provide services. It is included in the information which can be retrieved via docker inspect <container-id> . Note The EXPOSE command does not actually make any ports accessible to the host! Instead, this requires publishing ports by means of the -p flag when using docker run . Note If you want to learn more about Dockerfiles, check out Best practices for writing Dockerfiles . (source: https://github.com/docker/labs/tree/master/beginner ) Now that you know how to run docker container and create Dockerfiles let\u2019s move on to the practical part.","title":"2.3.4 Dockerfile commands summary"},{"location":"ch1-discover-docker-tp/","text":"Discover Docker Check Checkpoint: call us to check your results (don\u2019t stay blocked on a checkpoint if we are busy, we can check \u2154 checkpoints at the same time). Question Point to document/report. Tip Interesting information. Goals Good practice Do not forget to document what you do along the steps, the documentation provided will be evaluated as your report. Create an appropriate file structure, 1 folder per image. Target application 3-tiers application: HTTP server Backend API Database For each of those applications, we will follow the same process: choose the appropriate docker base image, create and configure this image, put our application specifics inside and at some point have it running. Our final goal is to have a 3-tier web API running. Base images HTTP server Backend API Database Database Basics We will use the image: postgres:17.2-alpine. Let\u2019s have a simple postgres server running, here is what would be a minimal Dockerfile: FROM postgres:17.2-alpine ENV POSTGRES_DB = db \\ POSTGRES_USER = usr \\ POSTGRES_PASSWORD = pwd Build this image and start a container properly. Your Postgres DB should be up and running. Check that everything is running smoothly with the docker command of your choice. Don\u2019t forget to name your docker image and container. Tip If you have difficulties go back to part 2.3.3 Build the image and 2.3.4 Run your image on TD01 - Docker ( TD 1 Discover Docker ). Re-run your database with adminer . Don't forget --network app-network to enable adminer/database communication. We use -\u2013network instead of -\u2013link because the latter is deprecated. Tip Don't forget to create your network docker network create app-network Also, does it seem right to have passwords written in plain text in a file? You may rather define those environment parameters when running the image using the flag -e . Question 1-1 For which reason is it better to run the container with a flag -e to give the environment variables rather than put them directly in the Dockerfile? It would be nice to have our database structure initialized with the docker image as well as some initial data. Any sql scripts found in /docker-entrypoint-initdb.d will be executed in alphabetical order, therefore let\u2019s add a couple scripts to our image: Tip Don't forget to restart the adminer: docker run \\ -p \"8090:8080\" \\ --net = app-network \\ --name = adminer \\ -d \\ adminer Init database 01-CreateScheme.sql CREATE TABLE public . departments ( id SERIAL PRIMARY KEY , name VARCHAR ( 20 ) NOT NULL ); CREATE TABLE public . students ( id SERIAL PRIMARY KEY , department_id INT NOT NULL REFERENCES departments ( id ), first_name VARCHAR ( 20 ) NOT NULL , last_name VARCHAR ( 20 ) NOT NULL ); 02-InsertData.sql INSERT INTO departments ( name ) VALUES ( 'IRC' ); INSERT INTO departments ( name ) VALUES ( 'ETI' ); INSERT INTO departments ( name ) VALUES ( 'CGP' ); INSERT INTO students ( department_id , first_name , last_name ) VALUES ( 1 , 'Eli' , 'Copter' ); INSERT INTO students ( department_id , first_name , last_name ) VALUES ( 2 , 'Emma' , 'Carena' ); INSERT INTO students ( department_id , first_name , last_name ) VALUES ( 2 , 'Jack' , 'Uzzi' ); INSERT INTO students ( department_id , first_name , last_name ) VALUES ( 3 , 'Aude' , 'Javel' ); Rebuild your image and check that your scripts have been executed at startup and that the data is present in your container. Tip When we talk about /docker-entrypoint-initdb.d it means inside the container, so you have to copy your directory's content and the container\u2019s directory. Persist data You may have noticed that if your database container gets destroyed then all your data is reset, a database must persist data durably. Use volumes to persist data on the host disk. -v /my/own/datadir:/var/lib/postgresql/data Check that data survives when your container gets destroyed. Link Docker volumes Question 1-2 Why do we need a volume to be attached to our postgres container? Question 1-3 Document your database container essentials: commands and Dockerfile. Backend API Basics For starters, we will simply run a Java hello-world class in our containers, only after will we be running a jar. In both cases, choose the proper image keeping in mind that we only need a Java runtime . Here is a complex Java Hello World implementation: Main.java public class Main { public static void main ( String [] args ) { System . out . println ( \"Hello World!\" ); } } 1- Compile with your target Java: javac Main.java . 2- Write dockerfile. FROM # TODO: Choose a java JRE # TODO: Add the compiled java (aka bytecode, aka .class) # TODO: Run the Java with: \u201cjava Main\u201d command. 3- Now, to launch app you have to do the same thing that Basic step 1. Here you have a first glimpse of your backend application. In the next step we will simply enrich the build (using maven instead of a minimalistic javac) and execute a jar instead of a simple .class. \u2192 If it\u2019s a success you must see \u201cHello Word\u201d in your console. Multistage build In the previous section we were building Java code on our machine to have it running on a docker container. Wouldn\u2019t it be great to have Docker handle the build as well? You probably noticed that the default openjdk docker images contain... Well... a JDK! Create a multistage build using the Multistage . Your Dockerfile should look like this: FROM eclipse-temurin:21-jdk-alpine # Build Main.java with JDK # TODO : in next steps (not now) FROM eclipse-temurin:21-jre-alpine # Copy resource from previous stage COPY --from = 0 /usr/src/Main.class . # Run java code with the JRE # TODO : in next steps (not now) Don\u2019t fill the Dockerfile now, we will have to do it in the next steps. Backend simple api We will deploy a Springboot application providing a simple API with a single greeting endpoint. Create your Springboot application on: Spring Initializer . Use the following config: Project: Maven Language: Java 21 Spring Boot: 3.4.2 Packaging: Jar Dependencies: Spring Web Generate the project and give it a simple GreetingController class: package fr.takima.training.simpleapi.controller ; import org.springframework.web.bind.annotation.* ; import java.util.concurrent.atomic.AtomicLong ; @RestController public class GreetingController { private static final String template = \"Hello, %s!\" ; private final AtomicLong counter = new AtomicLong (); @GetMapping ( \"/\" ) public Greeting greeting ( @RequestParam ( value = \"name\" , defaultValue = \"World\" ) String name ) { return new Greeting ( counter . incrementAndGet (), String . format ( template , name )); } record Greeting ( long id , String content ) {} } You can now build and start your application, of course you will need maven and a jdk-21. How convenient would it be to have a virtual container to build and run our simplistic API? Oh wait, we have docker, here is how you could build and run your application with Docker: # Build stage FROM eclipse-temurin:21-jdk-alpine AS myapp-build ENV MYAPP_HOME = /opt/myapp WORKDIR $MYAPP_HOME RUN apk add --no-cache maven COPY pom.xml . COPY src ./src RUN mvn package -DskipTests # Run stage FROM eclipse-temurin:21-jre-alpine ENV MYAPP_HOME = /opt/myapp WORKDIR $MYAPP_HOME COPY --from = myapp-build $MYAPP_HOME /target/*.jar $MYAPP_HOME /myapp.jar ENTRYPOINT [ \"java\" , \"-jar\" , \"myapp.jar\" ] Question 1-4 Why do we need a multistage build? And explain each step of this dockerfile. Check A working Springboot application with a simple HelloWorld endpoint. Did you notice that maven downloads all libraries on every image build? You can contribute to saving the planet caching libraries when maven pom file has not been changed by running the goal: mvn dependency:go-offline . Backend API Let\u2019s now build and run the backend API connected to the database. You can get the zipped source code here: simple-api . You can replace only your src directory and the pom.xml file with the ones available in the repository. Adjust the configuration in simple-api/src/main/resources/application.yml (this is the application configuration). How to access the database container from your backend application? Use the deprecated --link or create a docker network . Once everything is properly bound, you should be able to access your application API, for example on: /departments/IRC/students . [ { \"id\" : 1 , \"firstname\" : \"Eli\" , \"lastname\" : \"Copter\" , \"department\" : { \"id\" : 1 , \"name\" : \"IRC\" } } ] Explore your API other endpoints, have a look at the controllers in the source code. Check A simple web API on top of your database. Http server Basics Choose an appropriate base image. Create a simple landing page: index.html and put it inside your container. It should be enough for now, start your container and check that everything is working as expected. Here are commands that you may want to try to do so: docker stats docker inspect docker logs Link Httpd Getting Started Configuration You are using the default apache configuration, and it will be enough for now, you use yours by copying it in your image. Use docker exec to retrieve this default configuration from your running container /usr/local/apache2/conf/httpd.conf . Note You can also use docker cp . Reverse proxy We will configure the http server as a simple reverse proxy server in front of our application, this server could be used to deliver a front-end application, to configure SSL or to handle load balancing. So this can be quite useful even though in our case we will keep things simple. Here is the documentation: Reverse Proxy . Add the following to the configuration, and you should be all set: <VirtualHost *:80> ProxyPreserveHost On ProxyPass / http://YOUR_BACKEND_LINK:8080/ ProxyPassReverse / http://YOUR_BACKEND_LINK:8080/ </VirtualHost> LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_http_module modules/mod_proxy_http.so Question 1-5 Why do we need a reverse proxy? Check Checkpoint: a working application through a reverse proxy. Link application Docker-compose 1- Install docker-compose if the docker compose command does not work . You may have noticed that this can be quite painful to orchestrate manually the start, stop and rebuild of our containers. Thankfully, a useful tool called docker-compose comes in handy in those situations. 2- Let\u2019s create a docker-compose.yml file with the following structure to define and drive our containers: services : backend : build : #TODO networks : #TODO depends_on : #TODO database : build : #TODO networks : #TODO httpd : build : #TODO ports : #TODO networks : #TODO depends_on : #TODO networks : #TODO volumes : #TODO The docker-compose will handle the three containers for us. The file above is a basic example of structure, you need to add more parameters and think about the cleanest and most optimized approach like you would do in a company (for example: env variables, volumes, restart policies and processes segregation). Once your containers are orchestrated as services by docker-compose you should have a perfectly running application, make sure you can access your API on localhost . Note The ports of both your backend and database should not be opened to your host machine. Question 1-6 Why is docker-compose so important? Question 1-7 Document docker-compose most important commands. Question 1-8 Document your docker-compose file. Check A working 3-tier application running with docker-compose. Publish Your docker images are stored locally, let\u2019s publish them, so they can be used by other team members or on other machines. You will need a Docker Hub account. 1- Connect to your freshly created account with docker login . 2- Tag your image. For now, we have been only using the latest tag, now that we want to publish it, let\u2019s add some meaningful version information to our images. docker tag my-database USERNAME/my-database:1.0 3- Then push your image to dockerhub: docker push USERNAME/my-database:1.0 Dockerhub is not the only docker image registry, and you can also self-host your images (this is obviously the choice of most companies). Once you publish your images to dockerhub, you will see them in your account: having some documentation for your image would be quite useful if you want to use those later. Question 1-9 Document your publication commands and published images in dockerhub. Question 1-10 Why do we put our images into an online repo? \u00a9 Takima 2025","title":"TP part 01 - Docker"},{"location":"ch1-discover-docker-tp/#discover-docker","text":"Check Checkpoint: call us to check your results (don\u2019t stay blocked on a checkpoint if we are busy, we can check \u2154 checkpoints at the same time). Question Point to document/report. Tip Interesting information.","title":"Discover Docker"},{"location":"ch1-discover-docker-tp/#goals","text":"","title":"Goals"},{"location":"ch1-discover-docker-tp/#good-practice","text":"Do not forget to document what you do along the steps, the documentation provided will be evaluated as your report. Create an appropriate file structure, 1 folder per image.","title":"Good practice"},{"location":"ch1-discover-docker-tp/#target-application","text":"3-tiers application: HTTP server Backend API Database For each of those applications, we will follow the same process: choose the appropriate docker base image, create and configure this image, put our application specifics inside and at some point have it running. Our final goal is to have a 3-tier web API running.","title":"Target application"},{"location":"ch1-discover-docker-tp/#base-images","text":"HTTP server Backend API Database","title":"Base images"},{"location":"ch1-discover-docker-tp/#database","text":"","title":"Database"},{"location":"ch1-discover-docker-tp/#basics","text":"We will use the image: postgres:17.2-alpine. Let\u2019s have a simple postgres server running, here is what would be a minimal Dockerfile: FROM postgres:17.2-alpine ENV POSTGRES_DB = db \\ POSTGRES_USER = usr \\ POSTGRES_PASSWORD = pwd Build this image and start a container properly. Your Postgres DB should be up and running. Check that everything is running smoothly with the docker command of your choice. Don\u2019t forget to name your docker image and container. Tip If you have difficulties go back to part 2.3.3 Build the image and 2.3.4 Run your image on TD01 - Docker ( TD 1 Discover Docker ). Re-run your database with adminer . Don't forget --network app-network to enable adminer/database communication. We use -\u2013network instead of -\u2013link because the latter is deprecated. Tip Don't forget to create your network docker network create app-network Also, does it seem right to have passwords written in plain text in a file? You may rather define those environment parameters when running the image using the flag -e . Question 1-1 For which reason is it better to run the container with a flag -e to give the environment variables rather than put them directly in the Dockerfile? It would be nice to have our database structure initialized with the docker image as well as some initial data. Any sql scripts found in /docker-entrypoint-initdb.d will be executed in alphabetical order, therefore let\u2019s add a couple scripts to our image: Tip Don't forget to restart the adminer: docker run \\ -p \"8090:8080\" \\ --net = app-network \\ --name = adminer \\ -d \\ adminer","title":"Basics"},{"location":"ch1-discover-docker-tp/#init-database","text":"01-CreateScheme.sql CREATE TABLE public . departments ( id SERIAL PRIMARY KEY , name VARCHAR ( 20 ) NOT NULL ); CREATE TABLE public . students ( id SERIAL PRIMARY KEY , department_id INT NOT NULL REFERENCES departments ( id ), first_name VARCHAR ( 20 ) NOT NULL , last_name VARCHAR ( 20 ) NOT NULL ); 02-InsertData.sql INSERT INTO departments ( name ) VALUES ( 'IRC' ); INSERT INTO departments ( name ) VALUES ( 'ETI' ); INSERT INTO departments ( name ) VALUES ( 'CGP' ); INSERT INTO students ( department_id , first_name , last_name ) VALUES ( 1 , 'Eli' , 'Copter' ); INSERT INTO students ( department_id , first_name , last_name ) VALUES ( 2 , 'Emma' , 'Carena' ); INSERT INTO students ( department_id , first_name , last_name ) VALUES ( 2 , 'Jack' , 'Uzzi' ); INSERT INTO students ( department_id , first_name , last_name ) VALUES ( 3 , 'Aude' , 'Javel' ); Rebuild your image and check that your scripts have been executed at startup and that the data is present in your container. Tip When we talk about /docker-entrypoint-initdb.d it means inside the container, so you have to copy your directory's content and the container\u2019s directory.","title":"Init database"},{"location":"ch1-discover-docker-tp/#persist-data","text":"You may have noticed that if your database container gets destroyed then all your data is reset, a database must persist data durably. Use volumes to persist data on the host disk. -v /my/own/datadir:/var/lib/postgresql/data Check that data survives when your container gets destroyed. Link Docker volumes Question 1-2 Why do we need a volume to be attached to our postgres container? Question 1-3 Document your database container essentials: commands and Dockerfile.","title":"Persist data"},{"location":"ch1-discover-docker-tp/#backend-api","text":"","title":"Backend API"},{"location":"ch1-discover-docker-tp/#basics_1","text":"For starters, we will simply run a Java hello-world class in our containers, only after will we be running a jar. In both cases, choose the proper image keeping in mind that we only need a Java runtime . Here is a complex Java Hello World implementation: Main.java public class Main { public static void main ( String [] args ) { System . out . println ( \"Hello World!\" ); } } 1- Compile with your target Java: javac Main.java . 2- Write dockerfile. FROM # TODO: Choose a java JRE # TODO: Add the compiled java (aka bytecode, aka .class) # TODO: Run the Java with: \u201cjava Main\u201d command. 3- Now, to launch app you have to do the same thing that Basic step 1. Here you have a first glimpse of your backend application. In the next step we will simply enrich the build (using maven instead of a minimalistic javac) and execute a jar instead of a simple .class. \u2192 If it\u2019s a success you must see \u201cHello Word\u201d in your console.","title":"Basics"},{"location":"ch1-discover-docker-tp/#multistage-build","text":"In the previous section we were building Java code on our machine to have it running on a docker container. Wouldn\u2019t it be great to have Docker handle the build as well? You probably noticed that the default openjdk docker images contain... Well... a JDK! Create a multistage build using the Multistage . Your Dockerfile should look like this: FROM eclipse-temurin:21-jdk-alpine # Build Main.java with JDK # TODO : in next steps (not now) FROM eclipse-temurin:21-jre-alpine # Copy resource from previous stage COPY --from = 0 /usr/src/Main.class . # Run java code with the JRE # TODO : in next steps (not now) Don\u2019t fill the Dockerfile now, we will have to do it in the next steps.","title":"Multistage build"},{"location":"ch1-discover-docker-tp/#backend-simple-api","text":"We will deploy a Springboot application providing a simple API with a single greeting endpoint. Create your Springboot application on: Spring Initializer . Use the following config: Project: Maven Language: Java 21 Spring Boot: 3.4.2 Packaging: Jar Dependencies: Spring Web Generate the project and give it a simple GreetingController class: package fr.takima.training.simpleapi.controller ; import org.springframework.web.bind.annotation.* ; import java.util.concurrent.atomic.AtomicLong ; @RestController public class GreetingController { private static final String template = \"Hello, %s!\" ; private final AtomicLong counter = new AtomicLong (); @GetMapping ( \"/\" ) public Greeting greeting ( @RequestParam ( value = \"name\" , defaultValue = \"World\" ) String name ) { return new Greeting ( counter . incrementAndGet (), String . format ( template , name )); } record Greeting ( long id , String content ) {} } You can now build and start your application, of course you will need maven and a jdk-21. How convenient would it be to have a virtual container to build and run our simplistic API? Oh wait, we have docker, here is how you could build and run your application with Docker: # Build stage FROM eclipse-temurin:21-jdk-alpine AS myapp-build ENV MYAPP_HOME = /opt/myapp WORKDIR $MYAPP_HOME RUN apk add --no-cache maven COPY pom.xml . COPY src ./src RUN mvn package -DskipTests # Run stage FROM eclipse-temurin:21-jre-alpine ENV MYAPP_HOME = /opt/myapp WORKDIR $MYAPP_HOME COPY --from = myapp-build $MYAPP_HOME /target/*.jar $MYAPP_HOME /myapp.jar ENTRYPOINT [ \"java\" , \"-jar\" , \"myapp.jar\" ] Question 1-4 Why do we need a multistage build? And explain each step of this dockerfile. Check A working Springboot application with a simple HelloWorld endpoint. Did you notice that maven downloads all libraries on every image build? You can contribute to saving the planet caching libraries when maven pom file has not been changed by running the goal: mvn dependency:go-offline .","title":"Backend simple api"},{"location":"ch1-discover-docker-tp/#backend-api_1","text":"Let\u2019s now build and run the backend API connected to the database. You can get the zipped source code here: simple-api . You can replace only your src directory and the pom.xml file with the ones available in the repository. Adjust the configuration in simple-api/src/main/resources/application.yml (this is the application configuration). How to access the database container from your backend application? Use the deprecated --link or create a docker network . Once everything is properly bound, you should be able to access your application API, for example on: /departments/IRC/students . [ { \"id\" : 1 , \"firstname\" : \"Eli\" , \"lastname\" : \"Copter\" , \"department\" : { \"id\" : 1 , \"name\" : \"IRC\" } } ] Explore your API other endpoints, have a look at the controllers in the source code. Check A simple web API on top of your database.","title":"Backend API"},{"location":"ch1-discover-docker-tp/#http-server","text":"","title":"Http server"},{"location":"ch1-discover-docker-tp/#basics_2","text":"","title":"Basics"},{"location":"ch1-discover-docker-tp/#choose-an-appropriate-base-image","text":"Create a simple landing page: index.html and put it inside your container. It should be enough for now, start your container and check that everything is working as expected. Here are commands that you may want to try to do so: docker stats docker inspect docker logs Link Httpd Getting Started","title":"Choose an appropriate base image."},{"location":"ch1-discover-docker-tp/#configuration","text":"You are using the default apache configuration, and it will be enough for now, you use yours by copying it in your image. Use docker exec to retrieve this default configuration from your running container /usr/local/apache2/conf/httpd.conf . Note You can also use docker cp .","title":"Configuration"},{"location":"ch1-discover-docker-tp/#reverse-proxy","text":"We will configure the http server as a simple reverse proxy server in front of our application, this server could be used to deliver a front-end application, to configure SSL or to handle load balancing. So this can be quite useful even though in our case we will keep things simple. Here is the documentation: Reverse Proxy . Add the following to the configuration, and you should be all set: <VirtualHost *:80> ProxyPreserveHost On ProxyPass / http://YOUR_BACKEND_LINK:8080/ ProxyPassReverse / http://YOUR_BACKEND_LINK:8080/ </VirtualHost> LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_http_module modules/mod_proxy_http.so Question 1-5 Why do we need a reverse proxy? Check Checkpoint: a working application through a reverse proxy.","title":"Reverse proxy"},{"location":"ch1-discover-docker-tp/#link-application","text":"","title":"Link application"},{"location":"ch1-discover-docker-tp/#docker-compose","text":"1- Install docker-compose if the docker compose command does not work . You may have noticed that this can be quite painful to orchestrate manually the start, stop and rebuild of our containers. Thankfully, a useful tool called docker-compose comes in handy in those situations. 2- Let\u2019s create a docker-compose.yml file with the following structure to define and drive our containers: services : backend : build : #TODO networks : #TODO depends_on : #TODO database : build : #TODO networks : #TODO httpd : build : #TODO ports : #TODO networks : #TODO depends_on : #TODO networks : #TODO volumes : #TODO The docker-compose will handle the three containers for us. The file above is a basic example of structure, you need to add more parameters and think about the cleanest and most optimized approach like you would do in a company (for example: env variables, volumes, restart policies and processes segregation). Once your containers are orchestrated as services by docker-compose you should have a perfectly running application, make sure you can access your API on localhost . Note The ports of both your backend and database should not be opened to your host machine. Question 1-6 Why is docker-compose so important? Question 1-7 Document docker-compose most important commands. Question 1-8 Document your docker-compose file. Check A working 3-tier application running with docker-compose.","title":"Docker-compose"},{"location":"ch1-discover-docker-tp/#publish","text":"Your docker images are stored locally, let\u2019s publish them, so they can be used by other team members or on other machines. You will need a Docker Hub account. 1- Connect to your freshly created account with docker login . 2- Tag your image. For now, we have been only using the latest tag, now that we want to publish it, let\u2019s add some meaningful version information to our images. docker tag my-database USERNAME/my-database:1.0 3- Then push your image to dockerhub: docker push USERNAME/my-database:1.0 Dockerhub is not the only docker image registry, and you can also self-host your images (this is obviously the choice of most companies). Once you publish your images to dockerhub, you will see them in your account: having some documentation for your image would be quite useful if you want to use those later. Question 1-9 Document your publication commands and published images in dockerhub. Question 1-10 Why do we put our images into an online repo? \u00a9 Takima 2025","title":"Publish"},{"location":"cheatsheet/","text":"Cheatsheet Docker & docker-compose","title":"Cheatsheet"},{"location":"cheatsheet/#cheatsheet","text":"","title":"Cheatsheet"},{"location":"cheatsheet/#docker-docker-compose","text":"","title":"Docker & docker-compose"}]} \ No newline at end of file diff --git a/public/sitemap.xml b/public/sitemap.xml index cb17cee..2e5dcb9 100644 --- a/public/sitemap.xml +++ b/public/sitemap.xml @@ -10,4 +10,14 @@ <lastmod>2025-04-30</lastmod> <changefreq>daily</changefreq> </url> + <url> + <loc>None</loc> + <lastmod>2025-04-30</lastmod> + <changefreq>daily</changefreq> + </url> + <url> + <loc>None</loc> + <lastmod>2025-04-30</lastmod> + <changefreq>daily</changefreq> + </url> </urlset> \ No newline at end of file diff --git a/public/sitemap.xml.gz b/public/sitemap.xml.gz index ba8d13c9629d536ced54fbcc2fdddd4a4e75ba10..f31192459a9d0852cdd616fadc1b3694a6c5201c 100644 GIT binary patch literal 200 zcmb2|=HR&VN-%@ze{p6>YHnhIUPW#W!`n%|e1{YSTEEY0+Mu6s)rEJd_4IXL7+R9N zofaBpeEoK5{)~ACrf!~To*(z@HjjS%T33JB0~O&9OcNzCm)E4m#6EcabjsU8&9Fa$ zIxjkcZa6m1me{SlwD!KRLE!dH%#WOX9ZfSCtY%M{$SkHkCCkEK<;ERfz2@w_Jclo9 z=LCW1_|7>`clYFO=JlR<Gx|t~a_riC$NR3Ty|u-=3e=STD*gX3|M2l?c5XEci~nAz JPh?_X003G$U2^~c literal 195 zcmb2|=HQt0OfZA#e{p6>YHnhIUPW#W!_k1#e1{YS_I%go_`WH^G|>6w*~RLA8Jsq% zE(~B?`)}_T!OQjqde+v~Gd-t;zCX8M`X@*0=tsN@B{a2T*X9O4y8U&Ie8ExUZ(r0_ zbWFP0CbZq~n|ec{gvW)z(nb77ezG;?Toy_YY@DS0Owvn&InAt5QvK)A$fF4<ubVkG z7Mtp4Y&`!`@BXEf$SqTSw;eKj_3mo(b*I_<%~Orc53T={|Map){p0vwTluD^Gcqs$ E02ifP2><{9 -- GitLab