{"id":23589,"date":"2022-10-03T14:09:46","date_gmt":"2022-10-03T14:09:46","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=23589"},"modified":"2025-07-12T05:40:18","modified_gmt":"2025-07-12T05:40:18","slug":"creating-docker-image-step-by-step-guide","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/creating-docker-image-step-by-step-guide\/","title":{"rendered":"Docker Tutorials: Image &#8211; Creating Docker Image Step by Step"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Setting up the prerequisites<\/h3>\n\n\n\n<p>Listing images on the host<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">$ docker images<\/code><\/span><\/pre>\n\n\n<p>If you do not have ubuntu:14.04 images, please download it using following commands.<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">$ docker pull ubuntu:14.0 <\/code><\/span><\/pre>\n\n\n<p>Verify the ubuntu:14.0 image by running the following commands.<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">$ docker run -t -i -d ubuntu:14.04 \/bin\/bash<\/code><\/span><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Method 1: You can update a container created from an image and commit the results to an image.<\/h3>\n\n\n\n<p>We will use training\/sinatra image pretty useful for creating the images for Method1.<\/p>\n\n\n\n<p>To update an image you first need to create a container from the image you\u2019d like to update.<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">$ docker run -t -i training\/sinatra \/bin\/bash<\/code><\/span><\/pre>\n\n\n<p>Inside our running container first let\u2019s update apt, httpd and install git.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$ root@<span class=\"hljs-number\">0<\/span>b2616b0e5a8:\/<span class=\"hljs-comment\"># apt-get update<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$ root@<span class=\"hljs-number\">0<\/span>b2616b0e5a8:\/<span class=\"hljs-comment\"># apt-get install -y git<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$ root@<span class=\"hljs-number\">0<\/span>b2616b0e5a8:\/<span class=\"hljs-comment\"># apt-get install -y httpd<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Once this has completed let\u2019s exit our container using the exit command. Now you have a container with the change you want to make. You can then commit a copy of this container to an image using the docker commit command.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">$ docker commit -m <span class=\"hljs-string\">\"Added json gem\"<\/span> -a <span class=\"hljs-string\">\"Rajesh Kumar\"<\/span> <span class=\"hljs-number\">0<\/span>b2616b0e5a8 scmgalaxy\/sinatra:v2<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here you\u2019ve used the docker commit command. You\u2019ve specified two flags: -m and -a. The&nbsp;<strong>-m flag<\/strong>&nbsp;allows us to specify a commit message, much like you would with a commit on a version control system. The&nbsp;<strong>-a flag<\/strong>&nbsp;allows us to specify an author for our update.<\/p>\n\n\n\n<p>You\u2019ve also specified the container you want to create this new image from,&nbsp;<strong>0b2616b0e5a8<\/strong>&nbsp;(the ID you recorded earlier) and you\u2019ve specified a target for the image:<code>scmgalaxy\/sinatra:v2<\/code><\/p>\n\n\n\n<p>It consists of a new user,&nbsp;<strong>scmgalaxy<\/strong>, that you\u2019re writing this image to. You\u2019ve also specified the name of the image&nbsp;<strong>sinatra<\/strong>. Finally you\u2019re specifying a tag for the image:&nbsp;<strong>v2<\/strong>.<\/p>\n\n\n\n<p>You can then look at our new scmgalaxy\/sinatra image using the docker images command.<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">$ docker images<\/code><\/span><\/pre>\n\n\n<p><strong>To use our new image to create a container you can then:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">$ docker run -t -i ouruser\/sinatra:v2 \/bin\/bash<\/code><\/span><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Method 2: You can use a Dockerfile to specify instructions to create an image.<\/h3>\n\n\n\n<p>Using the docker commit command is a pretty simple way of extending an image but it\u2019s a bit cumbersome and it\u2019s not easy to share a development process for images amongst a team. Instead you can use a new command, docker build, to build new images from scratch.<\/p>\n\n\n\n<p>To do this you create a&nbsp;<strong>Dockerfile<\/strong>&nbsp;that contains a set of instructions that tell Docker how to build our image.<\/p>\n\n\n\n<p>First, create a directory and a Dockerfile.<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">\n$ mkdir sinatra\n$ cd sinatra\n$ touch Dockerfile\n<\/code><\/span><\/pre>\n\n\n<p>Each instruction creates a&nbsp;<strong>new layer of the image<\/strong>. Try a simple example now for building your own Sinatra image for your fictitious development team.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">\nFROM ubuntu:<span class=\"hljs-number\">14.04<\/span>\nMAINTAINER Rajesh Kumar <span class=\"hljs-string\">\"rajesh@scmgalaxy.com\"<\/span>\nRUN apt-<span class=\"hljs-keyword\">get<\/span> update &amp;&amp; apt-<span class=\"hljs-keyword\">get<\/span> install -y git httpd\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Examine what your Dockerfile does. Each instruction prefixes a statement and is capitalized.<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">INSTRUCTION statement<\/code><\/span><\/pre>\n\n\n<p>The first instruction&nbsp;<strong>FROM<\/strong>&nbsp;tells Docker what the source of our image is, in this case you\u2019re basing our new image on an&nbsp;<strong>Ubuntu 14.04<\/strong>&nbsp;image. The instruction uses the&nbsp;<strong>MAINTAINER<\/strong>&nbsp;instruction to specify who maintains the new image.<\/p>\n\n\n\n<p>Lastly, you\u2019ve specified two&nbsp;<strong>RUN<\/strong>&nbsp;git and httpd<\/p>\n\n\n\n<p>Now let\u2019s take our&nbsp;<strong>Dockerfile<\/strong>&nbsp;and use the&nbsp;<strong>docker build<\/strong>&nbsp;command to build an image.<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">$ docker build -t scmgalaxy\/sinatra:v3 .\/ <\/code><\/span><\/pre>\n\n\n<p>You\u2019ve specified our docker build command and used the&nbsp;<strong>-t<\/strong>&nbsp;flag to identify our new image as belonging to the user&nbsp;<strong>scmgalaxy<\/strong>, the repository name&nbsp;<strong>sinatra<\/strong>&nbsp;and given it the tag&nbsp;<strong>v2<\/strong>.<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">$ docker run -t -i scmgalaxy\/sinatra:v2 \/bin\/bash\/ <\/code><\/span><\/pre>\n\n\n<p><strong>Setting tags on an image<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">$ docker tag 5db5f8471261 scmgalaxy\/sinatra:dev<\/code><\/span><\/pre>\n\n\n<p>The docker tag command takes the ID of the image, here&nbsp;<strong>5db5f8471261<\/strong>, and our user name, the repository name and the new tag.<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">$ docker images scmgalaxy\/sinatra<\/code><\/span><\/pre>\n\n\n<p>Remove an image from the host<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">$ docker rmi training\/sinatra<\/code><\/span><\/pre>\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_13092\"><iframe loading=\"lazy\"  id=\"_ytid_94880\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_13092\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Setting up the prerequisites Listing images on the host If you do not have ubuntu:14.04 images, please download it using following commands. Verify the ubuntu:14.0 image by running the following&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[4862],"tags":[],"class_list":["post-23589","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/23589","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=23589"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/23589\/revisions"}],"predecessor-version":[{"id":31423,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/23589\/revisions\/31423"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=23589"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=23589"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=23589"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}