{"id":55525,"date":"2022-09-26T15:03:08","date_gmt":"2022-09-26T09:33:08","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=55525"},"modified":"2022-09-30T15:32:53","modified_gmt":"2022-09-30T10:02:53","slug":"docker-for-beginners","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/docker-for-beginners\/","title":{"rendered":"Docker for Beginners"},"content":{"rendered":"<p data-pm-slice=\"0 0 []\"><strong>Why Docker?<\/strong><\/p>\n<p>\u00a0Has it ever happened to you that you create an application it runs successfully on your machine?<\/p>\n<p>You share this application with your friends because it doesn\u2019t work on their machines.<\/p>\n<p>But it worked on my machine !!<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-55559\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/3ecf0047-1394-471d-aef0-eec107241c51.png\" alt=\"\" width=\"238\" height=\"212\" \/><\/p>\n<p><strong>What is Docker?<\/strong><\/p>\n<p>Docker is a tool for running applications in an isolated environment, it is very similar to a virtual machine but it is much faster and doesn\u2019t require a lot of memory and an entire operating system to operate.<\/p>\n<p>Also, it gives us the ability to ship our code faster and gives you control over your applications.<\/p>\n<p>Docker-based applications can be seamlessly moved from local development machines to production deployments.<\/p>\n<p><strong>What is Container?<\/strong><\/p>\n<p>A container is an abstraction at the application layer that packages code and dependencies together. Instead of virtualizing the entire physical machine, containers virtualize the host operating system only.<\/p>\n<p>Multiple containers can run on the same machine and share the OS kernel with other containers. Here containers don\u2019t require a full operating system and it simply shares the same underlying operating system by running in isolation from other containers.<\/p>\n<div data-node-type=\"mediaSingle\" data-layout=\"center\" data-width=\"\">\n<div title=\"Attachment\" data-id=\"6265f3e6-812a-475b-810c-fe77d936f6a9\" data-node-type=\"media\" data-type=\"file\" data-collection=\"contentId-56875353584\" data-width=\"1512\" data-height=\"982\" data-alt=\"\" data-file-name=\"Screenshot 2022-08-18 at 6.20.54 PM-20220824-112034.png\" data-file-size=\"271681\" data-file-mime-type=\"image\/png\" data-context-id=\"56875353584\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-55560\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/f4478a23-6b79-4671-9e9d-7a6ad59e234f-300x240.png\" alt=\"\" width=\"300\" height=\"240\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/f4478a23-6b79-4671-9e9d-7a6ad59e234f-300x240.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/f4478a23-6b79-4671-9e9d-7a6ad59e234f-1024x821.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/f4478a23-6b79-4671-9e9d-7a6ad59e234f-768x616.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/f4478a23-6b79-4671-9e9d-7a6ad59e234f-624x500.png 624w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/f4478a23-6b79-4671-9e9d-7a6ad59e234f.png 1244w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/div>\n<\/div>\n<p><strong>Docker image<\/strong><\/p>\n<p>A Docker image is a template for creating an environment of your choice. It could be a database, a web application pretty much it can be anything. Docker images are multi-layered read-only files carrying your application in the desired state inside them.<\/p>\n<p>Image contains everything you need to run your application, it contains the operating system, any software required as well as the application code.<\/p>\n<p>So once you have the image the container comes into play.\u00a0 In simplified terms, a container is a running instance of an image.<\/p>\n<p><strong>Docker Engine<\/strong><\/p>\n<p>The engine consists of three major components:<\/p>\n<ol class=\"ak-ol\">\n<li><strong>Docker Daemon:<\/strong> The daemon (dockerd) is a process that keeps running in the background and waits for commands from the client. The daemon is capable of managing various Docker objects.<\/li>\n<li><strong>Docker Client:<\/strong> The client\u00a0 (docker) is a command-line interface program mostly responsible for transporting commands issued by users.<\/li>\n<li><strong>REST API:<\/strong> The REST API acts as a bridge between the daemon and the client. Any command issued using the client passes through the API to finally reach the daemon.<\/li>\n<li>You as a user will usually execute commands using the client component. The client then uses the REST API to reach out to the long-running daemon and get your work done.<\/li>\n<\/ol>\n<p><strong>Docker commands<\/strong><\/p>\n<p>Go to the terminal considering the docker daemon is running. And type docker ps (docker ps lists all containers that are up and running). whenever you are using docker make sure the actual docker daemon is running.<\/p>\n<p><strong>Let&#8217;s download an image from the docker hub and run a container from it.<\/strong><\/p>\n<p>Pull an existing image from the docker hub and run a container from it.<\/p>\n<p>P.S: Docker hub is simply a registry. A registry is a place from where you can download all the public images that are shared by other people.<\/p>\n<p>Link:- <a href=\"https:\/\/hub.docker.com\/\" data-inline-card=\"\" data-card-data=\"\">https:\/\/hub.docker.com\/<\/a><\/p>\n<p>** <strong>docker run hello-world<\/strong><\/p>\n<p><strong>What happened when we ran docker run hello-world<\/strong><\/p>\n<p>run basically run a container from the image(here hello-world is the image name).<\/p>\n<p>As you type this you will get unable to find the image locally. first, it will try finding the image locally if it doesn\u2019t find then it goes to the docker hub and pull the image from there and run a container from the hello-world image.<\/p>\n<p><strong>Let&#8217;s pull a nginx image from the docker hub and performs some action on it.<\/strong><\/p>\n<p>**\u00a0<strong>docker pull nginx<\/strong><\/p>\n<p>Docker pull command is used to fetch docker image from docker hub to your local machine.<\/p>\n<p>Now let&#8217;s see what all images we have in our local machine for that<\/p>\n<p>** <strong>docker images <\/strong>\u2192 It will list down all the images we have locally<\/p>\n<p>nginx \u00a0 \u00a0 \u00a0 \u00a0 latest\u00a0 \u00a0 f493a2ff2935 \u00a0 2 weeks ago\u00a0 \u00a0 135MB<\/p>\n<p>hello-world \u00a0 latest\u00a0 \u00a0 46331d942d63 \u00a0 5 months ago \u00a0 9.14kB<\/p>\n<p><strong>P.S<\/strong>: while pulling an image sometimes you will see an image layer already exist, it is because images are made up of layers, and while pulling an image they are cached and that layer can be part of other images too, so if next time you try to download another image and if that layer is part of that image then it will not download it again.<\/p>\n<p><strong>Running an ubuntu machine inside the container\u00a0<\/strong><\/p>\n<p><strong>** docker run -it ubuntu:16.04<\/strong><\/p>\n<p><strong>Note: -it (interactive environment i.e don&#8217;t exit from it) ubuntu:16.04 here after colon 16.04 is known as tag here ubuntu version.<\/strong><\/p>\n<p><strong>** docker run -it ubuntu (if you don&#8217;t provide a version it will pull the latest version)<\/strong><\/p>\n<p>Here we are running the ubuntu terminal inside the container<\/p>\n<div data-node-type=\"mediaSingle\" data-layout=\"center\" data-width=\"\">\n<div title=\"Attachment\" data-id=\"53ac4a4e-7471-4405-82c3-4907c27a9e73\" data-node-type=\"media\" data-type=\"file\" data-collection=\"contentId-56875353584\" data-width=\"1512\" data-height=\"982\" data-alt=\"\" data-file-name=\"Screenshot 2022-08-18 at 11.21.30 PM-20220824-112101.png\" data-file-size=\"241897\" data-file-mime-type=\"image\/png\" data-context-id=\"56875353584\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-55561 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/d645b258-4265-44e6-bbd1-3963bd708bd7.png\" alt=\"\" width=\"1564\" height=\"366\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/d645b258-4265-44e6-bbd1-3963bd708bd7.png 1564w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/d645b258-4265-44e6-bbd1-3963bd708bd7-300x70.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/d645b258-4265-44e6-bbd1-3963bd708bd7-1024x240.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/d645b258-4265-44e6-bbd1-3963bd708bd7-768x180.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/d645b258-4265-44e6-bbd1-3963bd708bd7-1536x359.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/d645b258-4265-44e6-bbd1-3963bd708bd7-624x146.png 624w\" sizes=\"(max-width: 1564px) 100vw, 1564px\" \/><\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>We can perform any operation on the ubuntu terminal as usual i.e running inside the container so cool, isn&#8217;t it?<\/p>\n<p><strong>Exposing port\u00a0<\/strong><\/p>\n<p><strong>Let&#8217;s run an nginx container.\u00a0<\/strong><\/p>\n<p><strong>nginx server by default runs on port 80,\u00a0<\/strong><\/p>\n<p><strong>** docker run -d nginx \u00a0\u2192 upon running this command a nginx server will start on the port inside our container.<\/strong><\/p>\n<p><strong>P.S: -d(detached mode i.e in the background)<\/strong><\/p>\n<p>Now let&#8217;s say we want to access port 80 running inside our container from our host machine. For that, we need to expose port 80 to our host machine.<\/p>\n<p><strong>Steps: &#8211;<\/strong><\/p>\n<p>List down all the containers running<\/p>\n<p>** <strong>docker ps\u00a0<\/strong><\/p>\n<p><strong>** docker run -d -p 8080:80 nginx<\/strong><\/p>\n<div data-node-type=\"mediaSingle\" data-layout=\"center\" data-width=\"\">\n<div title=\"Attachment\" data-id=\"dd46f8b2-2150-417c-a7c8-e8575c7bccac\" data-node-type=\"media\" data-type=\"file\" data-collection=\"contentId-56875353584\" data-width=\"1174\" data-height=\"86\" data-alt=\"\" data-file-name=\"Untitled 2-20220824-112130.png\" data-file-size=\"546978\" data-file-mime-type=\"image\/png\" data-context-id=\"56875353584\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-55562 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/753bd8fa-cf58-4a5e-81e1-d93552a80ee9.png\" alt=\"\" width=\"2348\" height=\"172\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/753bd8fa-cf58-4a5e-81e1-d93552a80ee9.png 2348w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/753bd8fa-cf58-4a5e-81e1-d93552a80ee9-300x22.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/753bd8fa-cf58-4a5e-81e1-d93552a80ee9-1024x75.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/753bd8fa-cf58-4a5e-81e1-d93552a80ee9-768x56.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/753bd8fa-cf58-4a5e-81e1-d93552a80ee9-1536x113.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/753bd8fa-cf58-4a5e-81e1-d93552a80ee9-2048x150.png 2048w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/753bd8fa-cf58-4a5e-81e1-d93552a80ee9-624x46.png 624w\" sizes=\"(max-width: 2348px) 100vw, 2348px\" \/><\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Exposed port 80 running inside the container to host 8080<\/p>\n<p>We managed to access the application running inside our container from localhost, and also managed to map the host 8080 port to application port 80 running inside that container.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-55563 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/e9c23083-5f29-48c2-ae86-b0cc4a351fea.png\" alt=\"\" width=\"2342\" height=\"794\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/e9c23083-5f29-48c2-ae86-b0cc4a351fea.png 2342w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/e9c23083-5f29-48c2-ae86-b0cc4a351fea-300x102.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/e9c23083-5f29-48c2-ae86-b0cc4a351fea-1024x347.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/e9c23083-5f29-48c2-ae86-b0cc4a351fea-768x260.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/e9c23083-5f29-48c2-ae86-b0cc4a351fea-1536x521.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/e9c23083-5f29-48c2-ae86-b0cc4a351fea-2048x694.png 2048w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/e9c23083-5f29-48c2-ae86-b0cc4a351fea-624x212.png 624w\" sizes=\"(max-width: 2342px) 100vw, 2342px\" \/><\/p>\n<p>**<strong> Exposing multiple ports\u00a0<\/strong><\/p>\n<p><strong>** docker run -d -p 8080:80 -p 8081:80 nginx<\/strong><\/p>\n<p>Now from localhost:8080 and localhost:8081, we can access nginx server running inside the container.<\/p>\n<p><strong>Some additional commands<\/strong><\/p>\n<p><strong>** docker ps\u00a0<\/strong>(simply list the running container)<\/p>\n<p>**\u00a0<strong>docker ps -a\u00a0<\/strong>(all running as well as an inactive container)<\/p>\n<p>**\u00a0<strong>docker start container_id\u00a0<\/strong>(starts a container)<\/p>\n<p><strong>** docker stop container_id\u00a0<\/strong>(stops a container)<\/p>\n<p><strong>** docker rm container_id\u00a0<\/strong>(delete a container)<\/p>\n<p>**\u00a0<strong>docker rmi image_id\u00a0<\/strong>(delete an image)<\/p>\n<p>Deleting containers one by one can be a tedious task, there&#8217;s also a way to delete all the containers running or not in one go.<\/p>\n<p>** <strong>docker ps -aq<\/strong> (List all the containers running or inactive with its id only)<\/p>\n<div data-node-type=\"mediaSingle\" data-layout=\"center\" data-width=\"\">\n<div title=\"Attachment\" data-id=\"89e6e34d-909e-4d47-9a66-fb0374d0722b\" data-node-type=\"media\" data-type=\"file\" data-collection=\"contentId-56875353584\" data-width=\"1512\" data-height=\"982\" data-alt=\"\" data-file-name=\"Screenshot 2022-08-18 at 11.45.58 PM-20220824-112219.png\" data-file-size=\"140060\" data-file-mime-type=\"image\/png\" data-context-id=\"56875353584\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-55564 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/a988ce24-e551-487a-bfb2-7e51c5ce6472.png\" alt=\"\" width=\"1149\" height=\"306\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/a988ce24-e551-487a-bfb2-7e51c5ce6472.png 1149w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/a988ce24-e551-487a-bfb2-7e51c5ce6472-300x80.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/a988ce24-e551-487a-bfb2-7e51c5ce6472-1024x273.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/a988ce24-e551-487a-bfb2-7e51c5ce6472-768x205.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/a988ce24-e551-487a-bfb2-7e51c5ce6472-624x166.png 624w\" sizes=\"(max-width: 1149px) 100vw, 1149px\" \/><\/div>\n<\/div>\n<p>Now to delete all the containers and images in one go<\/p>\n<p>** <strong>docker rm -f $(docker ps -aq)<\/strong><\/p>\n<p>In the case of images rm changes to rmi<\/p>\n<p>** <strong>docker rmi -f $(docker images -aq)<\/strong><\/p>\n<p><strong>Assigning names to containers<\/strong><\/p>\n<p>So far you must have noticed if you don&#8217;t provide a name to the container, by default docker provides random names to it.<\/p>\n<p>In reality, when we create our own container we should give it a name so that we shouldn\u2019t be relying on the container id or the random name<\/p>\n<div data-node-type=\"mediaSingle\" data-layout=\"center\" data-width=\"\">\n<div title=\"Attachment\" data-id=\"4d1ec709-f571-4407-9d60-28a5ca46c287\" data-node-type=\"media\" data-type=\"file\" data-collection=\"contentId-56875353584\" data-width=\"1512\" data-height=\"982\" data-alt=\"\" data-file-name=\"Screenshot 2022-08-19 at 1.37.01 PM-20220824-112240.png\" data-file-size=\"213408\" data-file-mime-type=\"image\/png\" data-context-id=\"56875353584\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-55565 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/a9a9d3c3-95ec-4aa2-b7eb-e11dfc29bd4e.png\" alt=\"\" width=\"2240\" height=\"239\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/a9a9d3c3-95ec-4aa2-b7eb-e11dfc29bd4e.png 2240w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/a9a9d3c3-95ec-4aa2-b7eb-e11dfc29bd4e-300x32.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/a9a9d3c3-95ec-4aa2-b7eb-e11dfc29bd4e-1024x109.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/a9a9d3c3-95ec-4aa2-b7eb-e11dfc29bd4e-768x82.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/a9a9d3c3-95ec-4aa2-b7eb-e11dfc29bd4e-1536x164.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/a9a9d3c3-95ec-4aa2-b7eb-e11dfc29bd4e-2048x219.png 2048w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/a9a9d3c3-95ec-4aa2-b7eb-e11dfc29bd4e-624x67.png 624w\" sizes=\"(max-width: 2240px) 100vw, 2240px\" \/><\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>You can see the random names of the container like reverent_banach and so on given by the docker.<\/p>\n<p>Let&#8217;s provide custom names to our containers<\/p>\n<p>** <strong>docker run &#8211;name nginx_custom -d nginx<\/strong><\/p>\n<div data-node-type=\"mediaSingle\" data-layout=\"center\" data-width=\"\">\n<div title=\"Attachment\" data-id=\"00b7ee42-4e25-4ad4-bbb9-c9763bcc1891\" data-node-type=\"media\" data-type=\"file\" data-collection=\"contentId-56875353584\" data-width=\"1512\" data-height=\"982\" data-alt=\"\" data-file-name=\"Screenshot 2022-08-19 at 1.45.14 PM-20220824-112308.png\" data-file-size=\"150323\" data-file-mime-type=\"image\/png\" data-context-id=\"56875353584\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-55566 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/448576a0-ead8-48f5-98ce-bf0d80170c09.png\" alt=\"\" width=\"1964\" height=\"167\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/448576a0-ead8-48f5-98ce-bf0d80170c09.png 1964w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/448576a0-ead8-48f5-98ce-bf0d80170c09-300x26.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/448576a0-ead8-48f5-98ce-bf0d80170c09-1024x87.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/448576a0-ead8-48f5-98ce-bf0d80170c09-768x65.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/448576a0-ead8-48f5-98ce-bf0d80170c09-1536x131.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/448576a0-ead8-48f5-98ce-bf0d80170c09-624x53.png 624w\" sizes=\"(max-width: 1964px) 100vw, 1964px\" \/><\/div>\n<\/div>\n<p><strong>Docker commit<\/strong><\/p>\n<p>Use to create a new image from a container&#8217;s change<\/p>\n<p>Let&#8217;s say I have pulled an image name alpine (Lightweight Linux-based os) and installed software in it, and if by mistake we exit or stop the container, the container will not remember the new software we recently installed.<\/p>\n<p>So to create a modified container as an image we commit the changes, so that we can use that image to spin up a container in the future.<\/p>\n<p>**\u00a0<strong>docker pull alpine<\/strong><\/p>\n<p><strong>** docker run -it alpine<\/strong><\/p>\n<p><strong>** apk add tree (downloading the tree software inside our alpine container)<\/strong><\/p>\n<p><strong>** tree . (checks if the software running)<\/strong><\/p>\n<p><strong>** exit<\/strong><\/p>\n<div data-node-type=\"mediaSingle\" data-layout=\"center\" data-width=\"\">\n<div title=\"Attachment\" data-id=\"ff77db61-b7bf-4138-859a-f10ca565ec43\" data-node-type=\"media\" data-type=\"file\" data-collection=\"contentId-56875353584\" data-width=\"1512\" data-height=\"982\" data-alt=\"\" data-file-name=\"Screenshot 2022-08-19 at 10.57.01 PM-20220824-112330.png\" data-file-size=\"353080\" data-file-mime-type=\"image\/png\" data-context-id=\"56875353584\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-55567 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/7b6064b5-691d-4025-ad57-53a9bdd25f3b.png\" alt=\"\" width=\"1530\" height=\"652\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/7b6064b5-691d-4025-ad57-53a9bdd25f3b.png 1530w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/7b6064b5-691d-4025-ad57-53a9bdd25f3b-300x128.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/7b6064b5-691d-4025-ad57-53a9bdd25f3b-1024x436.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/7b6064b5-691d-4025-ad57-53a9bdd25f3b-768x327.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/7b6064b5-691d-4025-ad57-53a9bdd25f3b-624x266.png 624w\" sizes=\"(max-width: 1530px) 100vw, 1530px\" \/><\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>tree software is working properly inside the container.<\/p>\n<p>Let&#8217;s commit these changes so that we can spin this customized container in future<\/p>\n<p><strong>To commit\u00a0<\/strong><\/p>\n<p><strong>docker commit container_id himanshurepository\/alpinetree<\/strong><\/p>\n<p><strong>**\u00a0docker commit 1b0b36766d06 himanshurepository\/alpinetree (new image)<\/strong><\/p>\n<p><strong>Run a container from this new alpine image<\/strong><\/p>\n<p><strong>** docker images<\/strong><\/p>\n<p><strong>** docker run -it himanshurepository\/alpinetree<\/strong><\/p>\n<p><strong>Docker export<\/strong><\/p>\n<p>Useful when we want to share our container with our peers or friends with any of the medium. Let&#8217;s export the new alpinetree image we recently created.<\/p>\n<p>**<strong> docker export &#8211;output alpinetree.tar container_id<\/strong><\/p>\n<p>Now if you go to your home directory you will find alpinetree.tar file there which you can share with our friends.<\/p>\n<div data-node-type=\"mediaSingle\" data-layout=\"center\" data-width=\"\">\n<div title=\"Attachment\" data-id=\"48d61a37-5c29-473c-92c0-87e34c815f16\" data-node-type=\"media\" data-type=\"file\" data-collection=\"contentId-56875353584\" data-width=\"1512\" data-height=\"982\" data-alt=\"\" data-file-name=\"Screenshot 2022-08-19 at 11.08.18 PM-20220824-112401.png\" data-file-size=\"121845\" data-file-mime-type=\"image\/png\" data-context-id=\"56875353584\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-55568\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/1bf1cb4f-0de5-4ce3-8772-c7399e562d88-300x213.png\" alt=\"\" width=\"300\" height=\"213\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/1bf1cb4f-0de5-4ce3-8772-c7399e562d88-300x213.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/1bf1cb4f-0de5-4ce3-8772-c7399e562d88-768x544.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/1bf1cb4f-0de5-4ce3-8772-c7399e562d88-624x442.png 624w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/1bf1cb4f-0de5-4ce3-8772-c7399e562d88.png 965w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p><strong>Docker volumes<\/strong><\/p>\n<p>Docker volumes allow us to share data, it could be files or folders between host and container or between containers.<\/p>\n<p>Folders in the physical host file system are mounted into the virtual file system of docker. So when a container writes to its file system, it gets replicated or automatically written on the host file system directory or vice versa.<\/p>\n<p><strong>Volumes between host and container.<\/strong><\/p>\n<p>I have my nginx server running on port 80 inside the container that is mapped to localhost:8080, if you go to localhost:8080 you will find these default webpage that gets served by nginx server.<\/p>\n<div data-node-type=\"mediaSingle\" data-layout=\"center\" data-width=\"\">\n<div title=\"Attachment\" data-id=\"de8de7fc-f0ba-4984-a5f8-4b2a6d335024\" data-node-type=\"media\" data-type=\"file\" data-collection=\"contentId-56875353584\" data-width=\"1512\" data-height=\"982\" data-alt=\"\" data-file-name=\"Screenshot 2022-08-20 at 5.00.52 PM-20220824-112426.png\" data-file-size=\"307628\" data-file-mime-type=\"image\/png\" data-context-id=\"56875353584\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-55569 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/98237cf0-907a-4c3d-97d0-b92218d384d2.png\" alt=\"\" width=\"1290\" height=\"839\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/98237cf0-907a-4c3d-97d0-b92218d384d2.png 1290w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/98237cf0-907a-4c3d-97d0-b92218d384d2-300x195.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/98237cf0-907a-4c3d-97d0-b92218d384d2-1024x666.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/98237cf0-907a-4c3d-97d0-b92218d384d2-768x499.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/98237cf0-907a-4c3d-97d0-b92218d384d2-624x406.png 624w\" sizes=\"(max-width: 1290px) 100vw, 1290px\" \/><\/div>\n<\/div>\n<p>All the static files served from nginx lie in \/usr\/share\/nginx\/html directory. On my host machine inside the volume directory, I have this index.html file. Let&#8217;s mount this entire directory to nginx static file directory.<\/p>\n<p>** \u00a0<strong>docker run -v $(pwd):\/usr\/share\/nginx\/html -d -p 8080:80 nginx<\/strong><\/p>\n<div data-node-type=\"mediaSingle\" data-layout=\"center\" data-width=\"100\">\n<div title=\"Attachment\" data-id=\"690dce3f-7eb6-451c-b262-84f666a70a90\" data-node-type=\"media\" data-type=\"file\" data-collection=\"contentId-56875353584\" data-width=\"1512\" data-height=\"982\" data-alt=\"\" data-file-name=\"Screenshot 2022-08-20 at 5.08.24 PM-20220824-112448.png\" data-file-size=\"250529\" data-file-mime-type=\"image\/png\" data-context-id=\"56875353584\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-55570 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/a98dd32f-6692-4e4a-b63e-1f4c697b78a7.png\" alt=\"\" width=\"1790\" height=\"340\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/a98dd32f-6692-4e4a-b63e-1f4c697b78a7.png 1790w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/a98dd32f-6692-4e4a-b63e-1f4c697b78a7-300x57.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/a98dd32f-6692-4e4a-b63e-1f4c697b78a7-1024x195.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/a98dd32f-6692-4e4a-b63e-1f4c697b78a7-768x146.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/a98dd32f-6692-4e4a-b63e-1f4c697b78a7-1536x292.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/a98dd32f-6692-4e4a-b63e-1f4c697b78a7-624x119.png 624w\" sizes=\"(max-width: 1790px) 100vw, 1790px\" \/><\/div>\n<\/div>\n<p>We mounted the entire pwd (present working directory i.e volume) to nginx static file directory. Now if we go to localhost:8080 the index.html file i.e present on the host machine will get served.<\/p>\n<div data-node-type=\"mediaSingle\" data-layout=\"center\" data-width=\"\">\n<div title=\"Attachment\" data-id=\"dbdbe1ab-dabe-420e-b7ad-ffa59a68c112\" data-node-type=\"media\" data-type=\"file\" data-collection=\"contentId-56875353584\" data-width=\"1512\" data-height=\"982\" data-alt=\"\" data-file-name=\"Screenshot 2022-08-20 at 5.12.05 PM-20220824-112502.png\" data-file-size=\"1513408\" data-file-mime-type=\"image\/png\" data-context-id=\"56875353584\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-55571\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/185c5284-caed-41b1-8e23-dab422a95597-286x300.png\" alt=\"\" width=\"286\" height=\"300\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/185c5284-caed-41b1-8e23-dab422a95597-286x300.png 286w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/185c5284-caed-41b1-8e23-dab422a95597-975x1024.png 975w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/185c5284-caed-41b1-8e23-dab422a95597-768x807.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/185c5284-caed-41b1-8e23-dab422a95597-624x656.png 624w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/185c5284-caed-41b1-8e23-dab422a95597-24x24.png 24w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/185c5284-caed-41b1-8e23-dab422a95597.png 1344w\" sizes=\"(max-width: 286px) 100vw, 286px\" \/><\/div>\n<\/div>\n<p>In the same way, we can mount container files\/folders to the host machine. Attach a bash terminal to nginx container, go to the static file directory create a new file, this newly created file will be reflected on the host machine.<\/p>\n<p><strong>Volumes (Between containers)<\/strong><\/p>\n<p>Sharing the content of one container whether it&#8217;s a folder or file with another container.<\/p>\n<p>Let&#8217;s create another container name website-copy and share the website container static file data to this new container.<\/p>\n<p>**\u00a0<strong>docker run &#8211;name website-copy &#8211;volumes-from website -d -p 8081:80 nginx<\/strong><\/p>\n<p>Now we have two different websites and website containers running because we have copied the entire same content, now if you hit localhost:8081 you will be served the same content there too.<\/p>\n<p>In this way, we managed to share data across these containers.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>Why Docker? \u00a0Has it ever happened to you that you create an application it runs successfully on your machine? You share this application with your friends because it doesn\u2019t work on their machines. But it worked on my machine !! What is Docker? Docker is a tool for running applications in an isolated environment, it [&hellip;]<\/p>\n","protected":false},"author":1495,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2},"categories":[2348,446,1994,1],"tags":[5023],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55525"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1495"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=55525"}],"version-history":[{"count":4,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55525\/revisions"}],"predecessor-version":[{"id":55639,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55525\/revisions\/55639"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=55525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=55525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=55525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}