{"id":722,"date":"2018-08-31T04:44:18","date_gmt":"2018-08-31T04:44:18","guid":{"rendered":"http:\/\/shijuvarghese.com\/?p=722"},"modified":"2024-07-17T05:28:34","modified_gmt":"2024-07-17T05:28:34","slug":"docker-installation-and-basics","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=722","title":{"rendered":"Docker: Installation and basics"},"content":{"rendered":"<p>This tutorial is aimed to teach one how to install Docker in CentOS\u00a09 base host, and to perform basic tasks.<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/shijuvarghese.com\/wp-content\/uploads\/2018\/08\/container.jpg\" rel=\"attachment wp-att-749\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-749\" src=\"http:\/\/shijuvarghese.com\/wp-content\/uploads\/2018\/08\/container.jpg\" alt=\"Container explained\" width=\"518\" height=\"327\" srcset=\"http:\/\/shijuvarghese.com\/wp-content\/uploads\/2018\/08\/container.jpg 518w, http:\/\/shijuvarghese.com\/wp-content\/uploads\/2018\/08\/container-300x189.jpg 300w\" sizes=\"auto, (max-width: 518px) 100vw, 518px\" \/><\/a><\/p>\n<p><strong>Quick Notes:<\/strong><\/p>\n<ul>\n<li>You cannot run a Windows based container on a Docker that is installed in a Linux OS which used a Linux Kernel.<\/li>\n<li>You can run any Linux flavor based container on top of a Docker installed on any flavor of Linux.<\/li>\n<li>Docker images are templates that can be used to deploy containers.<\/li>\n<li>Multiple containers can be deployed using a same image<\/li>\n<\/ul>\n<p><strong>Pre-requisites:<\/strong><\/p>\n<ul>\n<li>A new host with CentOS\u00a09 minimal installation, 4GB RAM, 100 GB Hard disk.<\/li>\n<li>Ensure host is accessible via putty<\/li>\n<li>Disable Selinux and firewall<\/li>\n<\/ul>\n<p>Update the host&#8217;s kernel<\/p>\n<p style=\"text-align: center;\"><strong>CentOS 9 based Installation<\/strong><br \/>\n========*****========<\/p>\n<p>Install minimal version of CentOS 9 on a VM.<\/p>\n<p>Update all packages<\/p>\n<p><strong>[root@centos9docker ~]#<\/strong>\u00a0dnf config-manager &#8211;add-repo=https:\/\/download.docker.com\/linux\/centos\/docker-ce.repo<\/p>\n<p><strong>[root@centos9docker ~]#<\/strong>\u00a0docker -v<br \/>\nDocker version 27.0.3, build 7d4bcd8<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/shijuvarghese.com\/wp-content\/uploads\/2018\/08\/run_docker_image.jpg\" rel=\"attachment wp-att-754\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-754\" src=\"http:\/\/shijuvarghese.com\/wp-content\/uploads\/2018\/08\/run_docker_image.jpg\" alt=\"run_docker_image\" width=\"742\" height=\"245\" srcset=\"http:\/\/shijuvarghese.com\/wp-content\/uploads\/2018\/08\/run_docker_image.jpg 742w, http:\/\/shijuvarghese.com\/wp-content\/uploads\/2018\/08\/run_docker_image-300x99.jpg 300w\" sizes=\"auto, (max-width: 742px) 100vw, 742px\" \/><\/a><\/p>\n<p>Let us try to run an image named\u00a0\u00a0h<em>ello-world<\/em>. If the image is not present in localhost, the application will check in Docker repository and download hello-world container image.<\/p>\n<p><strong>[root@centos9docker ~]#<\/strong> docker run hello-world<\/p>\n<p><em>===== ===<\/em><br \/>\n<em>Unable to find image &#8216;hello-world:latest&#8217; locally<\/em><br \/>\n<em>latest: Pulling from library\/hello-world<\/em><br \/>\n<em>c1ec31eb5944: Pull complete<\/em><br \/>\n<em>Digest: sha256:1408fec50309afee38f3535383f5b09419e6dc0925bc69891e79d84cc4cdcec6<\/em><br \/>\n<em>Status: Downloaded newer image for hello-world:latest<\/em><\/p>\n<p><em>Hello from Docker!<\/em><br \/>\n<em>This message shows that your installation appears to be working correctly.<\/em><\/p>\n<p><em>To generate this message, Docker took the following steps:<\/em><br \/>\n<em> 1. The Docker client contacted the Docker daemon.<\/em><br \/>\n<em> 2. The Docker daemon pulled the &#8220;hello-world&#8221; image from the Docker Hub.<\/em><br \/>\n<em> (amd64)<\/em><br \/>\n<em> 3. The Docker daemon created a new container from that image which runs the<\/em><br \/>\n<em> executable that produces the output you are currently reading.<\/em><br \/>\n<em> 4. The Docker daemon streamed that output to the Docker client, which sent it<\/em><br \/>\n<em> to your terminal.<\/em><\/p>\n<p><em>To try something more ambitious, you can run an Ubuntu container with:<\/em><br \/>\n<em> $ docker run -it ubuntu bash<\/em><\/p>\n<p><em>=== ==== ==<\/em><\/p>\n<p>Similarly, let us download and run a small Ubuntu server, opening it in a console view:<\/p>\n<p><em><strong>[root@docker1 ~]# docker run -it\u00a0ubuntu\u00a0<\/strong><\/em><\/p>\n<p>The command <strong>Ctrl+PQ<\/strong> will allow us to exit from the Ubuntu host&#8217;s console. However the server will continue to run, and the Ubuntu image will continue to run. Instead, if we type &#8220;exit&#8221; instead of <strong>Ctrl+PQ<\/strong>\u00a0the container will stop and you will get back to the base node&#8217;s cli.<\/p>\n<p>Try both the situations and check the status of containers running on the node by issuing the command &#8220;<strong>docker ps<\/strong>&#8220;.<\/p>\n<p>Let us run a container from image ubuntu and list out the directories in it when the container comes up<\/p>\n<p><em><strong>[root@docker1 ~]# docker run ubuntu ls<\/strong><\/em><\/p>\n<p><em><strong>[root@docker1 ~]# docker run ubuntu\u00a0ping google.com<\/strong><\/em><\/p>\n<p>Let us\u00a0list the containers running on the docker. Take a note of the &#8220;<strong>NAME&#8221;<\/strong> field.<\/p>\n<p><em><strong>[root@docker1 ~]# docker ps<\/strong><\/em><\/p>\n<p><em><strong>[root@docker1 ~]# docker ps -aq<\/strong><\/em><\/p>\n<p>Below command will list all containers ever created.<\/p>\n<p><em><strong>[root@docker1 ~]# docker ps &#8211;all<\/strong><\/em><\/p>\n<p>Below command will remove an image named hello-world<\/p>\n<p><em><strong>[root@docker1 ~]#\u00a0docker rmi -f\u00a0 hello-world\u00a0<\/strong><\/em><\/p>\n<p>The below command\u00a0will allow us to get back to the container named &#8220;<em>thirsty_ptolemy&#8221;<\/em> if it is shown as running when the above command was executed.<\/p>\n<p><em><strong>[root@docker1 ~]# docker attach thirsty_ptolemy<\/strong><\/em><\/p>\n<p>Once in the container&#8217;s console, the command <em>&#8220;exit&#8221;<\/em> will stop the container, and exit out of its shell. Now the command <em>&#8220;docker ps&#8221;<\/em> will not display the container named &#8220;thirsty_ptolemy&#8221;.<\/p>\n<p><em><strong>root@bb06e23e73e2:\/# exit<\/strong><\/em><\/p>\n<p>Lists the images available in the host<\/p>\n<p><em><strong>[root@docker1 ~]# docker images<\/strong><\/em><\/p>\n<p>Remove the &#8220;hello-world&#8221; image from the host.<\/p>\n<p><em><strong>[root@docker1 ~]# docker rmi -f hello-world<\/strong><\/em><\/p>\n<p style=\"text-align: center;\">************ ***********<\/p>\n<p>The below is an exerciser to create a small image from a set of files, that will be later used to run as container<\/p>\n<p><em><strong>[root@docker1 ~]# mkdir \/sandbox<\/strong><\/em><br \/>\n<em><strong> [root@docker1 ~]# cd \/sandbox<\/strong><\/em><br \/>\n<em><strong> [root@docker1 hello-system]# vi index.js<\/strong><\/em><\/p>\n<p>Add the below contents between the lines<\/p>\n<p style=\"text-align: center;\">===== ====== ====== ===<\/p>\n<p style=\"text-align: left;\"><em>var http = require(&#8216;http&#8217;);<\/em><\/p>\n<p><em>const PORT = 80;<\/em><\/p>\n<p><em>function requestHandler(req, res) {<\/em><br \/>\n<em> res.end(`Hello my ${process.platform}`);<\/em><br \/>\n<em> }<\/em><\/p>\n<p><em>var server = http.createServer(requestHandler);<\/em><\/p>\n<p><em>server.listen(PORT, function(){<\/em><br \/>\n<em> console.log(`${process.env.NODE_ENV} server listening on port: ${PORT}. CTRL-C to exit.`);<\/em><br \/>\n<em> });<\/em><\/p>\n<p style=\"text-align: center;\">===== ====== ====== ===<\/p>\n<p><em><strong>[root@docker1 hello-system]# vi package.json<\/strong><\/em><\/p>\n<p>This is copied from :<\/p>\n<p>https:\/\/github.com\/HermantNET\/hello-system\/blob\/master\/package.json<\/p>\n<p style=\"text-align: center;\">===== === ========= ======= ======== ======<\/p>\n<p><em>{<\/em><br \/>\n<em> &#8220;name&#8221;: &#8220;hello-system&#8221;,<\/em><br \/>\n<em> &#8220;version&#8221;: &#8220;1.0.0&#8221;,<\/em><br \/>\n<em> &#8220;description&#8221;: &#8220;Greets the current operating system&#8221;,<\/em><br \/>\n<em> &#8220;main&#8221;: &#8220;index.js&#8221;,<\/em><br \/>\n<em> &#8220;scripts&#8221;: {<\/em><br \/>\n<em> &#8220;start&#8221;: &#8220;NODE_ENV=production node index.js&#8221;,<\/em><br \/>\n<em> &#8220;test&#8221;: &#8220;echo \\&#8221;Error: no test specified\\&#8221; &amp;&amp; exit 1&#8243;<\/em><br \/>\n<em> },<\/em><br \/>\n<em> &#8220;repository&#8221;: {<\/em><br \/>\n<em> &#8220;type&#8221;: &#8220;git&#8221;,<\/em><br \/>\n<em> &#8220;url&#8221;: &#8220;github.com\/hermantnet\/hello-system&#8221;<\/em><br \/>\n<em> },<\/em><br \/>\n<em> &#8220;author&#8221;: &#8220;Thomas E Herman Jr&#8221;,<\/em><br \/>\n<em> &#8220;license&#8221;: &#8220;ISC&#8221;<\/em><br \/>\n<em> }<\/em><\/p>\n<p style=\"text-align: center;\">===== === ========= ======= ======== ======<\/p>\n<p><em><strong>[root@docker1 hello-system]# vi .Dockerfile<\/strong><\/em><\/p>\n<p style=\"text-align: center;\">=======**=======**===<\/p>\n<p><em>FROM node:4-onbuild<\/em><br \/>\n<em> EXPOSE 80<\/em><\/p>\n<p style=\"text-align: center;\">=======**=======**===<\/p>\n<p>Now let us build the image, and look for the confirmation message &#8220;Successfully tagged hello-system:latest&#8221;<br \/>\n<em><strong>[root@docker1 hello-system]# docker build -t hello-system -f .\/.Dockerfile .<\/strong><\/em><\/p>\n<p>Let us run the image<br \/>\n<em><strong>[root@docker1 hello-system]# docker run -it &#8211;rm &#8211;name shiju-running-app hello-system<\/strong><\/em><br \/>\nThis should run the image in its console view with the message at the end &#8220;production server listening on port: 80. Press CTRL-C to exit.&#8221;<\/p>\n<p>Pressing CTRL+C should bring you back to your host&#8217;s shell prompt.<\/p>\n<p>On basis of message displayed in the docker&#8217;s console view, you may not be able to check the website using <em>&#8220;http:\/\/localhost:80&#8221;<\/em>, etc.<\/p>\n<p>You will have to create the container mapping a free port in the base host with the port 80 listened by the container, as shown below:<\/p>\n<p><em><strong>[root@docker1 \/]# docker run -it -p 1337:80 &#8211;name myContainer hello-system<\/strong><\/em><br \/>\n<em><span style=\"color: #3383ff;\">Now the website can be accessed using &#8220;http:\/\/localhost:1337&#8221;<\/span><\/em><\/p>\n<p>Pressing <em>CTRL+C<\/em> should bring you back to your host&#8217;s shell prompt, but the container may be still running though the command &#8220;docker ps&#8221; wont list this container as running.<\/p>\n<p><em><strong>[root@docker1 hello-system]# docker ps<\/strong><\/em><\/p>\n<p><em><strong>[root@docker1 \/]# docker run -it -p 1337:80 &#8211;name myContainer hello-system<\/strong><\/em><br \/>\nNote: This may give an error stating the container is running.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Below trick will remove the container completely<\/strong><\/p>\n<p><em><strong>[root@docker1 \/]# docker start myContainer<\/strong><\/em><br \/>\n<em><strong> [root@docker1 \/]# docker rm -f myContainer<\/strong><\/em><\/p>\n<p>The\u00a0<strong>docker run<\/strong> command\u00a0<strong>creates<\/strong> a container and\u00a0<strong>starts<\/strong> it.\u00a0The command to create a container is\u00a0<strong>docker create &lt;image&gt;\u00a0<\/strong>and the command to run it is\u00a0<strong>docker start &lt;container ID&gt;<\/strong>.<\/p>\n<p>Imagine you have changed the content in the &#8220;index.js&#8221; file to display a webpage &#8220;Hello my system Linux&#8221;. This will not come into action just by restarting the container. You will have to build a new docker image from the files, or run the following command, binding the folder holding the content to a place where the app will be located.<\/p>\n<p><em><strong>[root@docker1]# docker run -it -p 1338:80 -v \/root\/hello-system\/.:\/usr\/src\/app &#8211;name myContainer hello-system<\/strong><\/em><\/p>\n<p>Check the site accessing the base host via port 1338<\/p>\n<p>&nbsp;<\/p>\n<p>List all containers hosted in the system, weather running or not:<\/p>\n<p><em><strong>[root@docker1 docker]# docker container ls -all<\/strong><\/em><\/p>\n<p>&nbsp;<\/p>\n<p>Remove a container named <em>myContainer2<\/em><\/p>\n<p><em><strong>[root@docker1 docker]#\u00a0docker rm -f myContainer2<\/strong><\/em><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>This tutorial is aimed to teach one how to install Docker in CentOS\u00a09 base host, and to perform basic tasks. &nbsp; Quick Notes: You cannot <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=722\" title=\"Docker: Installation and basics\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":730,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25,30],"tags":[],"class_list":["post-722","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-docker","category-rhel-9"],"_links":{"self":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/722","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=722"}],"version-history":[{"count":30,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/722\/revisions"}],"predecessor-version":[{"id":1618,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/722\/revisions\/1618"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/media\/730"}],"wp:attachment":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=722"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=722"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=722"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}