{"id":937,"date":"2019-06-16T09:00:57","date_gmt":"2019-06-16T07:00:57","guid":{"rendered":"http:\/\/blogperso.union31.fr\/?p=937"},"modified":"2019-06-22T13:09:49","modified_gmt":"2019-06-22T11:09:49","slug":"docker-dockerfile","status":"publish","type":"post","link":"https:\/\/blogperso.union31.fr\/?p=937","title":{"rendered":"Docker : dockerfile"},"content":{"rendered":"\n<p>Voir comment cr\u00e9er ou modifier des images.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Sommaire<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blogperso.union31.fr\/?p=937\/#I_Preparation_espace_de_travail\" >I Pr\u00e9paration espace de travail<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blogperso.union31.fr\/?p=937\/#II_Les_variables_qui_seront_utilisees\" >II Les variables qui seront utilis\u00e9es<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blogperso.union31.fr\/?p=937\/#III_Mon_premier_Dockerfile\" >III Mon premier Dockerfile<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blogperso.union31.fr\/?p=937\/#IV_Mon_deuxieme_Dockerfile\" >IV Mon deuxi\u00e8me Dockerfile<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"I_Preparation_espace_de_travail\"><\/span>I Pr\u00e9paration espace de travail<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Cr\u00e9ation d&rsquo;un r\u00e9pertoire de travail :<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mkdir ~\/dockerfile\ncd ~\/dockerfile\nmkdir image1\ncd image1<\/pre>\n\n\n\n<p>Le fichier doit avoir le nom \u00ab\u00a0Dockerfile\u00a0\u00bb.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">touch Dockerfile<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"II_Les_variables_qui_seront_utilisees\"><\/span>II Les variables qui seront utilis\u00e9es <span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>FROM<\/strong> permet de d\u00e9finir depuis quelle base votre image ve \u00eatre cr\u00e9\u00e9e <\/li><li><strong>MAINTAINER<\/strong> permet de d\u00e9finir l&rsquo;auteur de l&rsquo;image et s&rsquo;\u00e9crit de la mani\u00e8re suivante <code>Nom &lt;email><\/code><\/li><li><strong>RUN<\/strong> permet de lancer une commande, mais aura aussi pour effet de cr\u00e9er une image interm\u00e9diaire. <\/li><li><strong>ADD<\/strong> permet de copier un fichier depuis la machine h\u00f4te ou depuis une URL <\/li><li><strong>EXPOSE<\/strong> permet d&rsquo;exposer un port du container vers l&rsquo;ext\u00e9rieur <\/li><li><strong>CMD<\/strong> d\u00e9termine la commande qui sera ex\u00e9cut\u00e9e <span style=\"text-decoration: underline;\">lorsque le container d\u00e9marrera<\/span> <\/li><li><strong>ENTRYPOINT<\/strong> permet d&rsquo;ajouter une commande qui sera  ex\u00e9cut\u00e9e par d\u00e9faut, et ce, m\u00eame si on choisit d&rsquo;ex\u00e9cuter une commande  diff\u00e9rente de la commande standard <\/li><li><strong>WORKDIR<\/strong> permet de d\u00e9finir le dossier de travail pour toutes les autres commandes (par exemple RUN, CMD, ENTRYPOINT et ADD) <strong>ENV<\/strong> permet de d\u00e9finir des variables d&rsquo;environnements qui pourront ensuite \u00eatre modifi\u00e9es gr\u00e2ce au param\u00e8tre de la commande <code>run --env &lt;key>=&lt;value><\/code>. <\/li><li><strong>VOLUMES<\/strong> permet de cr\u00e9er un point de montage qui  permettra de persister les donn\u00e9es. On pourra alors choisir de monter ce  volume dans un dossier sp\u00e9cifique en utilisant la commande `run -v : <\/li><\/ul>\n\n\n\n<p>Pour plus d&rsquo;information : <a href=\"https:\/\/docs.docker.com\/engine\/reference\/builder\/#environment-replacement\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"lien vers docker (s\u2019ouvre dans un nouvel onglet)\">lien vers docker<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"III_Mon_premier_Dockerfile\"><\/span>III Mon premier Dockerfile<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Premier contenu :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Image de base \nFROM centos \n# Qui maintien \nMAINTAINER Union31xh union31xh@gmail.com \n\n# Installation \nRUN yum -y update<\/code><\/pre>\n\n\n\n<p>Sauver puis compiler le fichier :<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>docker build -t union31xh\/image1 .<\/li><\/ul>\n\n\n\n<p>Le -t est pour mettre un tag \u00e0 l&rsquo;image sous forme auteur\/nom_projet. Le \u00ab\u00a0.\u00a0\u00bb est pour d\u00e9signer le r\u00e9pertoire courant<\/p>\n\n\n\n<p>Exemple de sortie :<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Sending build context to Docker daemon 2.048 kB\n Step 1\/3 : FROM centos\n  ---&gt; 9f38484d220f\n Step 2\/3 : MAINTAINER Union31xh <a href=\"mailto:union31xh@gmail.com\">union31xh@gmail.com<\/a>\n  ---&gt; Using cache\n  ---&gt; 9ca0e8b832cb\n Step 3\/3 : RUN yum -y  update\n  ---&gt; Running in c4434d7f7cea\n Loaded plugins: fastestmirror, ovl\n Determining fastest mirrors\n base: centos.mirrors.proxad.net\n extras: centos.mirrors.ovh.net <\/pre>\n\n\n\n<p>Si on relance le build :<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Sending build context to Docker daemon 2.048 kB\n Step 1\/3 : FROM centos\n  ---&gt; 9f38484d220f\n Step 2\/3 : MAINTAINER Union31xh <a href=\"mailto:union31xh@gmail.com\">union31xh@gmail.com<\/a>\n  ---&gt; Using cache\n  ---&gt; 9ca0e8b832cb\n Step 3\/3 : RUN yum -y  update\n  ---&gt; Using cache\n  ---&gt; d0fd866d30f6\n Successfully built d0fd866d30f6<\/pre>\n\n\n\n<p>C&rsquo;est beaucoup plus rapide car l&rsquo;ensemble des fichiers sont d\u00e9j\u00e0 pr\u00e9sents&#8230;<\/p>\n\n\n\n<p>L&rsquo;image est maintenant dans docker :<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>docker images<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE\n<strong> union31xh\/image1              latest              d0fd866d30f6        8 minutes ago       401 MB<\/strong>\n centos-test                   latest              1b5c255408dd        2 days ago          202 MB\n docker.io\/centos              latest              9f38484d220f        3 months ago        202 MB\n docker.io\/eaudeweb\/mailtrap   latest              adfbd16b7ba0        6 months ago        269 MB<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>Cr\u00e9ation d&rsquo;un script \u00ab\u00a0un_script.sh\u00a0\u00bb dans le r\u00e9pertoire courant :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#! \/bin\/bash\necho \"--> Bonjour !!!\"<\/code><\/pre>\n\n\n\n<p>Ce script va \u00eatre ajout\u00e9 dans l&rsquo;image et sera execut\u00e9. Dans le fichier Dockerfile :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Image de base\nFROM centos\n\n# Qui maintien\nMAINTAINER Union31xh &lt;union31xh@gmail.com>\n\nRUN yum -y  update\n\n# copie du script vers l'image\nADD un_script.sh \/usr\/bin\/un_script.sh\n# rend le script executable\nRUN chmod 755 \/usr\/bin\/un_script.sh\n\nCMD [\"un_script.sh\"]\n<\/code><\/pre>\n\n\n\n<p>On rebuild l&rsquo;image :<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li> docker build -t union31xh\/image1 . <\/li><\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">Sending build context to Docker daemon 3.072 kB\n Step 1\/6 : FROM centos\n  ---&gt; 9f38484d220f\n Step 2\/6 : MAINTAINER Union31xh <a href=\"mailto:union31xh@gmail.com\">union31xh@gmail.com<\/a>\n  ---&gt; Using cache\n  ---&gt; 9ca0e8b832cb\n Step 3\/6 : RUN yum -y  update\n  ---&gt; Using cache\n  ---&gt; d0fd866d30f6\n Step 4\/6 : ADD un_script.sh \/usr\/bin\/un_script.sh\n  ---&gt; Using cache\n  ---&gt; 6fbe8d3b2df2\n Step 5\/6 : RUN chmod 755 \/usr\/bin\/un_script.sh\n  ---&gt; Running in 2f17b159f2e2\n ---&gt; 7fc29898df1b\n Removing intermediate container 2f17b159f2e2\n Step 6\/6 : CMD un_script.sh\n  ---&gt; Running in 68b39783f4ff\n  ---&gt; bf21b050b930\n Removing intermediate container 68b39783f4ff\n Successfully built bf21b050b930<\/pre>\n\n\n\n<p>Testons l&rsquo;image :<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>docker run &#8211;rm -ti union31xh\/image1<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">[root@localhost image1]# docker run --rm -ti union31xh\/image1\n --&gt; Bonjour !!!<\/pre>\n\n\n\n<p>(L&rsquo;option &#8211;rm est pour supprimer le container une fois ex\u00e9cut\u00e9)<\/p>\n\n\n\n<p>Le container s&rsquo;est lanc\u00e9 mais \u00e0 toute de suite quitter. En effet tous les processus ont \u00e9t\u00e9 execut\u00e9s sans de mise en \u00ab\u00a0attente\u00a0\u00bb. De ce fait docker \u00e0 supprimer le container.<\/p>\n\n\n\n<p>Pour cela nous allons modifier le script est ajout\u00e9 un bash en execution :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#! \/bin\/bash\n\necho \"--> Bonjour !!!\"\nbash\n<\/code><\/pre>\n\n\n\n<p>On rebuild l&rsquo;image :<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li> docker build -t union31xh\/image1 .  <\/li><\/ul>\n\n\n\n<p>On lance le container (en mode d\u00e9mon) :<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>docker run &#8211;rm -d -ti union31xh\/image1<\/li><\/ul>\n\n\n\n<p>Est-il toujours en ex\u00e9cution ?<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>docker ps -all<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES\n ad667d81d739        union31xh\/image1    \"un_script.sh\"      10 seconds ago      Up 9 seconds                            tender_spence<\/pre>\n\n\n\n<p>Oui &#8230;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"IV_Mon_deuxieme_Dockerfile\"><\/span>IV Mon deuxi\u00e8me Dockerfile<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Nous allons maintenant faire une image utile. Elle contiendra apache et php afin de cr\u00e9er un premier environnement de d\u00e9veloppement.<\/p>\n\n\n\n<p>Cr\u00e9ation de l&rsquo;espace de travail :<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">cd ~\/dockerfile\nmkdir image2\ncd image2\ntouch Dockerfile<\/pre>\n\n\n\n<p>Premier contenu du fichier :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># A partir de quelle image\nFROM centos:7\n\n# Qui maintien\nMAINTAINER Union31xh &lt;union31xh@gmail.com>\n\n# permet d'activer le systemd (par defaut non actif)\n#   indications Centos sur le HUB DOCKER\nENV container docker\nRUN (cd \/lib\/systemd\/system\/sysinit.target.wants\/; for i in *; do [ $i == \\\nsystemd-tmpfiles-setup.service ] || rm -f $i; done); \\\nrm -f \/lib\/systemd\/system\/multi-user.target.wants\/*;\\\nrm -f \/etc\/systemd\/system\/*.wants\/*;\\\nrm -f \/lib\/systemd\/system\/local-fs.target.wants\/*; \\\nrm -f \/lib\/systemd\/system\/sockets.target.wants\/*udev*; \\\nrm -f \/lib\/systemd\/system\/sockets.target.wants\/*initctl*; \\\nrm -f \/lib\/systemd\/system\/basic.target.wants\/*;\\\nrm -f \/lib\/systemd\/system\/anaconda.target.wants\/*;\nVOLUME [ \"\/sys\/fs\/cgroup\" ]\n\nRUN yum -y update\nRUN yum -y install httpd\nRUN yum clean all\nRUN systemctl enable httpd.service\nEXPOSE 80\nCMD [\"\/usr\/sbin\/init\"]\n<\/code><\/pre>\n\n\n\n<p>On fabrique l&rsquo;image :<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>docker build -t union31xh\/image2 .<\/li><\/ul>\n\n\n\n<p>On v\u00e9rifie :<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>docker images<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE\n union31xh\/image2              latest              9ad2357b2e76        12 seconds ago      565 MB<\/pre>\n\n\n\n<p>On lance le container :<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>docker run &#8211;rm -d -ti union31xh\/image2<\/li><\/ul>\n\n\n\n<p>On v\u00e9rifie la pr\u00e9sence du container :<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>docker ps<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES\n 22be41836dfb        union31xh\/image2    \"\/bin\/bash\"         42 seconds ago      Up 41 seconds       80\/tcp              boring_euler<\/pre>\n\n\n\n<p>Le container est bien lanc\u00e9 mais le serveur apache n&rsquo;est pas accessible. En effet un oubli de liaison de port a \u00e9t\u00e9 fait. <\/p>\n\n\n\n<p>On tue le container puis on le relance :<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>docker container rm -f 22be41836dfb<\/li><li>docker run &#8211;rm -d -ti -p 80:80  union31xh\/image2<\/li><\/ul>\n\n\n\n<p>La page web \u00ab\u00a0apache\u00a0\u00bb est maintenant accessible<\/p>\n\n\n\n<p>Il ne reste plus qu&rsquo;a rajouter PHP. Editons le fichier Dockerfile<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># A partir de quelle image\nFROM centos:7\n\n# Qui maintien\nMAINTAINER Union31xh &lt;union31xh@gmail.com>\n\n# permet d'activer le systemd (par defaut non actif)\nENV container docker\nRUN (cd \/lib\/systemd\/system\/sysinit.target.wants\/; for i in *; do [ $i == \\\nsystemd-tmpfiles-setup.service ] || rm -f $i; done); \\\nrm -f \/lib\/systemd\/system\/multi-user.target.wants\/*;\\\nrm -f \/etc\/systemd\/system\/*.wants\/*;\\\nrm -f \/lib\/systemd\/system\/local-fs.target.wants\/*; \\\nrm -f \/lib\/systemd\/system\/sockets.target.wants\/*udev*; \\\nrm -f \/lib\/systemd\/system\/sockets.target.wants\/*initctl*; \\\nrm -f \/lib\/systemd\/system\/basic.target.wants\/*;\\\nrm -f \/lib\/systemd\/system\/anaconda.target.wants\/*;\n#VOLUME [ \"\/sys\/fs\/cgroup\" ]\n\n\nRUN yum -y update\nRUN yum -y install httpd\nRUN yum -y install php\nRUN yum -y install php-common\n\nRUN yum clean all\n\nRUN systemctl enable httpd.service\n\nEXPOSE 80\n\nCMD [\"\/usr\/sbin\/init\"]\n<\/code><\/pre>\n\n\n\n<p>Et on recommence : build <\/p>\n\n\n\n<p>Avant de lancer le container, on va mapper le repertoire \/var\/www\/html vers le r\u00e9pertoire h\u00f4te ~\/www.<\/p>\n\n\n\n<p>Cr\u00e9ation du r\u00e9pertoire :<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>mkdir ~\/www<\/li><\/ul>\n\n\n\n<p>Puis lan\u00e7ons le container avec ce nouveau r\u00e9pertoire mapp\u00e9<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>docker run &#8211;rm -d -ti -p 80:80 <strong>-v ~\/www:\/var\/www\/html<\/strong>  union31xh\/image2<\/li><\/ul>\n\n\n\n<p>Ainsi tous ce qui se trouvera dans ~\/www sera lu par le container.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Voir comment cr\u00e9er ou modifier des images. I Pr\u00e9paration espace de travail Cr\u00e9ation d&rsquo;un r\u00e9pertoire de travail : mkdir ~\/dockerfile cd ~\/dockerfile mkdir image1 cd image1 Le fichier doit avoir le nom \u00ab\u00a0Dockerfile\u00a0\u00bb. touch Dockerfile II Les variables qui seront<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-937","post","type-post","status-publish","format-standard","hentry","category-_systeme"],"_links":{"self":[{"href":"https:\/\/blogperso.union31.fr\/index.php?rest_route=\/wp\/v2\/posts\/937","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogperso.union31.fr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogperso.union31.fr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogperso.union31.fr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogperso.union31.fr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=937"}],"version-history":[{"count":20,"href":"https:\/\/blogperso.union31.fr\/index.php?rest_route=\/wp\/v2\/posts\/937\/revisions"}],"predecessor-version":[{"id":965,"href":"https:\/\/blogperso.union31.fr\/index.php?rest_route=\/wp\/v2\/posts\/937\/revisions\/965"}],"wp:attachment":[{"href":"https:\/\/blogperso.union31.fr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=937"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogperso.union31.fr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=937"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogperso.union31.fr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=937"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}