

{"id":37803,"date":"2018-11-30T14:36:01","date_gmt":"2018-11-30T09:06:01","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=37803"},"modified":"2018-11-30T14:36:01","modified_gmt":"2018-11-30T09:06:01","slug":"docker-container","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/docker-container\/","title":{"rendered":"Docker Container Tutorial &#8211; Command and Lifecycle"},"content":{"rendered":"<p>Today, we will see Docker Container Tutorial. Moreover, in this <strong>Docker tutorial<\/strong>, we will discuss why containers are used in Docker. Also, we will look at example and lifecycle of Container in Docker. Along with this, we will see Docker Container Command with syntax.<\/p>\n<p>So, let&#8217;s start Docker Containers tutorial.<\/p>\n<h2><span style=\"font-weight: 400\">What is Docker Container?<\/span><\/h2>\n<p><span style=\"font-weight: 400\">Basically, instances of <strong>Docker images<\/strong> that can be run by using the Docker run command is what we call <strong>Containers<\/strong>. However, Docker\u2019s basic purpose is to run containers.<\/span><\/p>\n<p><span style=\"font-weight: 400\">By using the Docker API or CLI, we can create, start, stop, move, or delete a container. Moreover, on the basis of the current state of a container, we can connect a container to one or more networks, attach storage to it, or even can create a new image.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Additionally, a container is relatively well isolated from other containers as well as its host machine. Though, it is possible to control how isolated a container\u2019s network, storage, or other underlying subsystems are from other containers or from the host machine. <\/span><\/p>\n<p><span style=\"font-weight: 400\">Also, make sure that any changes to its state that are not stored in persistent storage, disappears, when we remove a container.<\/span><\/p>\n<h3><span style=\"font-weight: 400\">i. Example of Docker Run Command<\/span><\/h3>\n<p><span style=\"font-weight: 400\">Below command runs an Ubuntu container, attaches interactively to your local command-line session, and runs \/bin\/bash.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">$ docker run -i -t ubuntu \/bin\/bash<\/pre>\n<p><span style=\"font-weight: 400\">Following happens, while we run this command (assuming you are using the default registry configuration):<\/span><\/p>\n<p><span style=\"font-weight: 400\">Docker pulls ubuntu image from our configured registry if we do not have the ubuntu image locally.<\/span><\/p>\n<p><span style=\"font-weight: 400\">We run a docker container create command manually, Docker creates a new container.<\/span><\/p>\n<p><span style=\"font-weight: 400\">As its final layer, Docker allocates a read-write filesystem to the container. It permits a running container to create or modify files and directories in its local filesystem.<\/span><\/p>\n<p><span style=\"font-weight: 400\">The container stops when we type exit to terminate the \/bin\/bash command, but not removed. Though, we can again start it or remove it.<\/span><\/p>\n<h2><span style=\"font-weight: 400\">Why Docker Container?<\/span><\/h2>\n<p><span style=\"font-weight: 400\">It virtualizes at the operating system level, with multiple containers running atop the OS kernel directly, rather than virtualizing the hardware stack as with the virtual machines approach. <\/span><\/p>\n<p><span style=\"font-weight: 400\">It simply means that containers are far more lightweight, start much faster, and use a fraction of the memory compared to booting an entire OS. Some more advantages of using a Container in Docker are:<\/span><\/p>\n<h3><span style=\"font-weight: 400\">i. Consistent Environment<\/span><\/h3>\n<p><span style=\"font-weight: 400\">Here Consistent Environment means, Containers give developers the ability to create predictable environments which are isolated from other applications. <\/span><\/p>\n<p><span style=\"font-weight: 400\">Also, Containers include software dependencies needed by the application, like specific versions of programming language runtimes as well as other software libraries.<\/span><\/p>\n<h3><span style=\"font-weight: 400\">ii. Run Anywhere<\/span><\/h3>\n<p><span style=\"font-weight: 400\">Basically, on many platforms like Linux, Windows, and Mac operating systems, virtual machines or bare metal, developer\u2019s machine or in data centres on-premises or also in the public cloud, containers can run.<\/span><\/p>\n<h2><span style=\"font-weight: 400\">Docker Container and Layers<\/span><\/h2>\n<p><span style=\"font-weight: 400\">Well, the top writable layer is the major difference between a container and an image. The writable layer is also deleted when the container is deleted. The underlying image remains unchanged.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Also, ensure that all changes are stored in this container layer, multiple containers can share access to the same underlying image because each container has its own writable container layer. <\/span><\/p>\n<p><span style=\"font-weight: 400\">In addition, to manage the contents of the image layers and the writable container layer, Docker uses storage drivers. All drivers use stackable image layers and the copy-on-write (CoW) strategy, even if each storage driver handles the implementation differently.<\/span><\/p>\n<h2><span style=\"font-weight: 400\">Docker Container Size on Disk<\/span><\/h2>\n<p><span style=\"font-weight: 400\">We can use the docker ps -s command, to view the approximate size of a running container. However, 2 columns relate to size.<\/span><\/p>\n<p><span style=\"font-weight: 400\"><strong>Size:<\/strong> Here size refers to the amount of data (on disk) which we use for the writable layer of each container<\/span><\/p>\n<p><span style=\"font-weight: 400\"><strong>Virtual size:<\/strong> And, virtual size refers to the amount of data which containers used for the read-only image data. <\/span><\/p>\n<p><span style=\"font-weight: 400\">All of the running containers on disk use the total disk space, that space is the combination of each container\u2019s size and the virtual size values. <\/span><\/p>\n<p><span style=\"font-weight: 400\">Though, the total size on disk for these containers would be SUM (size of containers) plus one container\u2019s (virtual size- size), if multiple containers started from the same exact image.<\/span><\/p>\n<h2><span style=\"font-weight: 400\">Docker Container Lifecycle<\/span><\/h2>\n<p><span style=\"font-weight: 400\">Here is the entire lifecycle of a container in Docker.<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400\">At first, the container will be in the created state.<\/span><\/li>\n<li><span style=\"font-weight: 400\">Further, the container moves into the running state while we use the Docker run command<\/span><\/li>\n<li><span style=\"font-weight: 400\">Though, to kill an existing container, we use the Docker kill command.<\/span><\/li>\n<li><span style=\"font-weight: 400\">In order to pause an existing container, we use the Docker pause command.<\/span><\/li>\n<li><span style=\"font-weight: 400\">Moreover, to pause an existing container, we use the Docker stop command.<\/span><\/li>\n<li><span style=\"font-weight: 400\">And, to put a container back from a stopped state to a running state, we use the Docker run command.<\/span><\/li>\n<\/ul>\n<h2><span style=\"font-weight: 400\">Docker Container Command<\/span><\/h2>\n<p><strong>i. docker top<\/strong><\/p>\n<p><span style=\"font-weight: 400\">We can see the top processes within a container by using the below command.<\/span><\/p>\n<p><strong>Syntax<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">docker top ContainerID<\/pre>\n<p><strong>ii. docker stop<\/strong><\/p>\n<p><span style=\"font-weight: 400\">In order to stop a running container.<\/span><\/p>\n<p><strong>Syntax<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">docker stop ContainerID<\/pre>\n<p><strong>iii. docker rm<\/strong><\/p>\n<p><span style=\"font-weight: 400\">To delete a container.<\/span><\/p>\n<p><strong>Syntax<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">docker rm ContainerID<\/pre>\n<p><strong>iv. docker stats<\/strong><\/p>\n<p><span style=\"font-weight: 400\">To offer the statistics of a running container.<\/span><\/p>\n<p><strong>Syntax<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">docker stats ContainerID<\/pre>\n<p><strong>v. docker attach<\/strong><\/p>\n<p><span style=\"font-weight: 400\">In order to attach to a running container.<\/span><\/p>\n<p><strong>Syntax<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">docker attach ContainerID<\/pre>\n<p><strong>vi. docker pause<\/strong><\/p>\n<p><span style=\"font-weight: 400\">To pause the processes in a running container.<\/span><\/p>\n<p><strong>Syntax<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">docker pause ContainerID<\/pre>\n<p><strong>vii. docker unpause<\/strong><\/p>\n<p><span style=\"font-weight: 400\">To unpause the processes in a running container.<\/span><\/p>\n<p><strong>Syntax<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">docker unpause ContainerID<\/pre>\n<p><strong>viii. docker kill<\/strong><\/p>\n<p><span style=\"font-weight: 400\">To kill the processes in a running container.<\/span><\/p>\n<p><strong>Syntax<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">docker kill ContainerID<\/pre>\n<h2>Conclusion<\/h2>\n<p><span style=\"font-weight: 400\">Hence, in this Docker Container Tutorial, we have learned all about Container in Docker in detail. Moreover, we discussed Why Containers used in Docker with its lifecycle. <\/span><\/p>\n<p><span style=\"font-weight: 400\">Also, we looked at Docker Container Command with syntax. Still, if any doubt occurs, feel free to ask in the comment tab. We are happy to help!<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today, we will see Docker Container Tutorial. Moreover, in this Docker tutorial, we will discuss why containers are used in Docker. Also, we will look at example and lifecycle of Container in Docker. Along&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":37815,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16988],"tags":[17068,17070,17067,17069,17065,17066],"class_list":["post-37803","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-docker","tag-docker-container-command","tag-docker-container-layer","tag-docker-container-lifecycle","tag-docker-container-size-on-disk","tag-docker-container-tutorial","tag-why-docker-containers"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Docker Container Tutorial - Command and Lifecycle - DataFlair<\/title>\n<meta name=\"description\" content=\"Docker Container Tutorial, why Containers used in Docker, Container Lifecycle, commands with syntax &amp; examples, Size on Disk, Docker layers, docker top,stop\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/docker-container\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Docker Container Tutorial - Command and Lifecycle - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Docker Container Tutorial, why Containers used in Docker, Container Lifecycle, commands with syntax &amp; examples, Size on Disk, Docker layers, docker top,stop\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/docker-container\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-11-30T09:06:01+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/10\/Docker-Container-Tutorial-01.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1202\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Docker Container Tutorial - Command and Lifecycle - DataFlair","description":"Docker Container Tutorial, why Containers used in Docker, Container Lifecycle, commands with syntax & examples, Size on Disk, Docker layers, docker top,stop","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/docker-container\/","og_locale":"en_US","og_type":"article","og_title":"Docker Container Tutorial - Command and Lifecycle - DataFlair","og_description":"Docker Container Tutorial, why Containers used in Docker, Container Lifecycle, commands with syntax & examples, Size on Disk, Docker layers, docker top,stop","og_url":"https:\/\/data-flair.training\/blogs\/docker-container\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-11-30T09:06:01+00:00","og_image":[{"width":1202,"height":630,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/10\/Docker-Container-Tutorial-01.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/docker-container\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/docker-container\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89"},"headline":"Docker Container Tutorial &#8211; Command and Lifecycle","datePublished":"2018-11-30T09:06:01+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/docker-container\/"},"wordCount":950,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/docker-container\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/10\/Docker-Container-Tutorial-01.jpg","keywords":["docker container command","Docker Container layer","docker container lifecycle","Docker Container size on disk","docker container tutorial","why docker containers"],"articleSection":["Docker Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/docker-container\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/docker-container\/","url":"https:\/\/data-flair.training\/blogs\/docker-container\/","name":"Docker Container Tutorial - Command and Lifecycle - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/docker-container\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/docker-container\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/10\/Docker-Container-Tutorial-01.jpg","datePublished":"2018-11-30T09:06:01+00:00","description":"Docker Container Tutorial, why Containers used in Docker, Container Lifecycle, commands with syntax & examples, Size on Disk, Docker layers, docker top,stop","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/docker-container\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/docker-container\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/docker-container\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/10\/Docker-Container-Tutorial-01.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/10\/Docker-Container-Tutorial-01.jpg","width":1202,"height":630,"caption":"Docker Container Tutorial - Command and Lifecycle"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/docker-container\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Docker Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/docker\/"},{"@type":"ListItem","position":3,"name":"Docker Container Tutorial &#8211; Command and Lifecycle"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam2\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/37803","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=37803"}],"version-history":[{"count":0,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/37803\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/37815"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=37803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=37803"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=37803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}