{"id":227,"date":"2023-11-01T13:03:00","date_gmt":"2023-11-01T12:03:00","guid":{"rendered":"https:\/\/extendsclass.com\/blog\/?p=227"},"modified":"2023-06-19T20:19:07","modified_gmt":"2023-06-19T18:19:07","slug":"creating-your-first-docker-container","status":"publish","type":"post","link":"https:\/\/extendsclass.com\/blog\/creating-your-first-docker-container","title":{"rendered":"Creating your first Docker container"},"content":{"rendered":"\n<p><br>This article is an introduction to creating Docker containers. We will walk through setting up an nginx web server together.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"has-text-align-center has-larger-font-size\"><strong>If you haven&#8217;t installed Docker yet, please refer to the article on <a href=\"https:\/\/extendsclass.com\/blog\/installing-docker-on-linux\" title=\"\">Docker installation<\/a>.<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_47_1 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\">Table of Contents<\/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=\"ez-toc-toggle-icon-1\"><label for=\"item-69f51f00e3eda\" aria-label=\"Table of Content\"><span style=\"display: flex;align-items: center;width: 35px;height: 30px;justify-content: center;direction:ltr;\"><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><\/label><input  type=\"checkbox\" id=\"item-69f51f00e3eda\"><\/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:\/\/extendsclass.com\/blog\/creating-your-first-docker-container\/#Image_vs_Container\" title=\"Image vs Container\">Image vs Container<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/extendsclass.com\/blog\/creating-your-first-docker-container\/#What_is_an_image\" title=\"What is an image?\">What is an image?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/extendsclass.com\/blog\/creating-your-first-docker-container\/#What_is_a_container\" title=\"What is a container?\">What is a container?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/extendsclass.com\/blog\/creating-your-first-docker-container\/#Creating_a_container\" title=\"Creating a container\">Creating a container<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading has-text-align-left\"><span class=\"ez-toc-section\" id=\"Image_vs_Container\"><\/span>Image vs Container<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>First, let&#8217;s start with some definitions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_an_image\"><\/span>What is an image?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>A <strong>Docker image<\/strong>  is a file system hierarchy that includes everything necessary to run an application: binaries, system tools, libraries, etc.<\/p>\n\n\n\n<p>Note: An image does not contain a kernel; it uses the host machine&#8217;s kernel.<\/p>\n\n\n\n<p>An image is &#8220;immutable&#8221; or inert, serving as a snapshot.<\/p>\n\n\n\n<p><strong>Docker Hub<\/strong> is a public repository where numerous images are published and regularly updated. It offers a wide range of images, including Debian, CentOS, MySQL, Java, and more.<\/p>\n\n\n\n<p>Please note that there are official images (created by Docker employees), images directly created by software vendors (such as MySQL and Debian), and images created by individual users. Many images may have security vulnerabilities or even worse issues (as reported in an article by <a href=\"https:\/\/www.lemondeinformatique.fr\/actualites\/lire-la-moitie-des-images-de-docker-hub-vulnerables-a-des-failles-critiques-81223.html\" title=\"\">Le Monde Informatique<\/a> (in French)).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_a_container\"><\/span>What is a container?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To put it simply, a container can be seen as the instantiation of an image. With a single image, you can create multiple containers if desired.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Creating_a_container\"><\/span>Creating a container<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>We will start with an official nginx image. You can find it on Docker Hub at this location: <a href=\"https:\/\/hub.docker.com\/_\/nginx\">https:\/\/hub.docker.com\/_\/nginx<\/a><\/p>\n\n\n\n<p>We will execute the following command line:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker run -d -p 8080:80 --name web nginx<\/code><\/pre>\n\n\n\n<p><br>Brief explanation of the different parameters:<\/p>\n\n\n\n<ul>\n<li>docker run: Command to create a container from an image.<\/li>\n\n\n\n<li>-d: Detached mode to run the container in the background. <\/li>\n\n\n\n<li>-p 8080:80: Mapping port 80 (from the container) to port 8080 on the host machine.<\/li>\n\n\n\n<li> &#8211;name web: Naming our container as &#8220;web&#8221;.<\/li>\n\n\n\n<li> nginx: Name of the image used to create the container.<\/li>\n<\/ul>\n\n\n\n<p>And there you have it, you have just created your first container, that was quick! ^^<\/p>\n\n\n\n<p>Your web server is up and running, you can access it from your browser.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"659\" height=\"411\" src=\"https:\/\/extendsclass.com\/blog\/wp-content\/uploads\/2023\/07\/nginx.png\" alt=\"\" class=\"wp-image-232\" srcset=\"https:\/\/extendsclass.com\/blog\/wp-content\/uploads\/2023\/07\/nginx.png 659w, https:\/\/extendsclass.com\/blog\/wp-content\/uploads\/2023\/07\/nginx-300x187.png 300w\" sizes=\"(max-width: 659px) 100vw, 659px\" \/><\/figure>\n\n\n\n<p>Now, let&#8217;s take it a step further by hosting our HTML pages. We will simply customize the index.html page in this article.<\/p>\n\n\n\n<p>First, we start by creating an index.html file in our favorite text editor (nano for me\u2026 Not getting into the debate\u2026).<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"737\" height=\"490\" src=\"https:\/\/extendsclass.com\/blog\/wp-content\/uploads\/2023\/08\/docker-nginx-first-container.png\" alt=\"\" class=\"wp-image-239\" srcset=\"https:\/\/extendsclass.com\/blog\/wp-content\/uploads\/2023\/08\/docker-nginx-first-container.png 737w, https:\/\/extendsclass.com\/blog\/wp-content\/uploads\/2023\/08\/docker-nginx-first-container-300x199.png 300w\" sizes=\"(max-width: 737px) 100vw, 737px\" \/><\/figure>\n\n\n\n<p>I have created this index.html file in the \/tmp directory of my machine.<\/p>\n\n\n\n<p>Next, we will delete the container that we created earlier.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker rm -f web<\/code><\/pre>\n\n\n\n<p>The &#8220;-f&#8221; parameter allows us to force the deletion of our container because by default, we cannot delete an active container.<\/p>\n\n\n\n<p>Finally, we execute the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker run -d -p 8080:80 --name web -v \/tmp:\/usr\/share\/nginx\/html nginx<\/code><\/pre>\n\n\n\n<p>Brief explanation of the new parameter: &#8220;-v \/tmp:\/usr\/share\/nginx\/html&#8221;:<br>We mount the local directory &#8220;\/tmp&#8221; to the location &#8220;\/usr\/share\/nginx\/html&#8221; inside the container.<\/p>\n\n\n\n<p>Note: You need to refer to the nginx image page on Docker Hub to find out where nginx stores the HTML pages.<\/p>\n\n\n\n<p>Now, if we access our web server again, it will return our customized page.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"659\" height=\"412\" src=\"https:\/\/extendsclass.com\/blog\/wp-content\/uploads\/2023\/08\/container-nginx-1.png\" alt=\"\" class=\"wp-image-238\" srcset=\"https:\/\/extendsclass.com\/blog\/wp-content\/uploads\/2023\/08\/container-nginx-1.png 659w, https:\/\/extendsclass.com\/blog\/wp-content\/uploads\/2023\/08\/container-nginx-1-300x188.png 300w\" sizes=\"(max-width: 659px) 100vw, 659px\" \/><\/figure>\n\n\n\n<p>And there you have it, this article is just a taste of what can be done. There are several aspects to delve into (port mapping, volumes, etc.).<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tutorial to learn how to create a Docker container. We will see how to run a web server in a container.<\/p>\n","protected":false},"author":1,"featured_media":236,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_sitemap_exclude":false,"_sitemap_priority":"","_sitemap_frequency":""},"categories":[2],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/posts\/227"}],"collection":[{"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/comments?post=227"}],"version-history":[{"count":7,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/posts\/227\/revisions"}],"predecessor-version":[{"id":229,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/posts\/227\/revisions\/229"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/media\/236"}],"wp:attachment":[{"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/media?parent=227"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/categories?post=227"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/tags?post=227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}