{"id":11,"date":"2019-05-04T23:04:49","date_gmt":"2019-05-04T23:04:49","guid":{"rendered":"http:\/\/localhost:8081\/?p=11"},"modified":"2019-05-06T22:19:22","modified_gmt":"2019-05-06T22:19:22","slug":"how-to-build-your-own-docker-image","status":"publish","type":"post","link":"https:\/\/blog.nikster.de\/wordpress\/index.php\/2019\/05\/04\/how-to-build-your-own-docker-image\/","title":{"rendered":"How to build a docker image from scratch"},"content":{"rendered":"\n<p>This Post covers the steps one will have to go through, to build ones own docker image.<\/p>\n\n\n\n<p>First we&#8217;ll need to create a basic image from which we&#8217;ll go on.<\/p>\n\n\n\n<p>I&#8217;ll use debian here, though I work on gentoo I like debian a lot and debootstrap is available on gentoo as well.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir stretchimage\n\nsudo debootstrap --arch amd64 stretch stretchimage http:\/\/deb.debian.org\/debian\/\n W: Cannot check Release signature; keyring file not available \/usr\/share\/keyrings\/debian-archive-keyring.gpg\n I: Retrieving InRelease \n I: Retrieving Release \n I: Retrieving Packages \n I: Validating Packages \n I: Resolving dependencies of required packages...\n \n [...]\n I: Configuring systemd...\n I: Base system installed successfully. <\/code><\/pre>\n\n\n\n<p>First we create a folder in which to debootstrap our new installation. After some time this is done.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>as root\/sudo, do:\n\nmount -t proc none \/home\/nkalle\/stretchimage\/proc\/\nmount -o bind sys \/home\/nkalle\/stretchimage\/sys\/\nmount -o bind \/dev\/ \/home\/nkalle\/stretchimage\/dev\/\nchroot \/path\/to\/stretchimage\/ \/bin\/bash <\/code><\/pre>\n\n\n\n<p>We would like to change some things in our image, otherwise there would be no need for an individual image. So we create a chroot environment to work in.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apt-get remove vim-common nano\nrm -rf \/var\/lib\/apt\/lists\/* <\/code><\/pre>\n\n\n\n<p>I&#8217;ll reduce the size of our installation here by removing vim and nano and also deleting some installation information. You may play around with this as you wish or even install more basic stuff that you need on every image, you get the idea. I do not recommend on installing additional stuff though, because one of docker&#8217;s strengths is saving resources by reusing the same image with different additional layers where your applications will be installed.  <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>as root\/sudo, do:\n\numount \/home\/nkalle\/stretchimage\/proc\numount \/home\/nkalle\/stretchimage\/sys\numount \/home\/nkalle\/stretchimage\/dev<\/code><\/pre>\n\n\n\n<p>don&#8217;t forget to unmount, before packaging.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cd stretchimage\/\n sudo tar cvf \/home\/nkalle\/stretchbase.tar *\n du -sh ..\/stretchbase.tar \n 259M    ..\/stretchbase.tar <\/code><\/pre>\n\n\n\n<p>tar your installation (you may as well tar and zip it, as you wish)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker import ..\/stretchbase.tar\n sha256:08ec830450cdacf33847bda7ad152f974eddbc69f01f92ba3800470184b64517\n \n nkalle@suse ~\/stretchimage $ docker image ls\n REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE\n &lt;none>                  &lt;none>              08ec830450cd        13 seconds ago      264MB <\/code><\/pre>\n\n\n\n<p>docker import, imports our tarball as a dockerimage, this image has no name though, as &#8220;docker image ls&#8221; shows us.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nkalle@suse ~\/stretchimage $ docker tag 08ec830450cd stretchbase\n nkalle@suse ~\/stretchimage $ docker image ls\n REPOSITORY              TAG                 IMAGE ID            CREATED              SIZE\n stretchbase             latest              08ec830450cd        About a minute ago   264MB<\/code><\/pre>\n\n\n\n<p>docker tag allows us to name the image which is convenient, especially when searching, etc.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker run -i -t stretchbase bash\n root@90c298cbb08e:\/# ps faxww\n   PID TTY      STAT   TIME COMMAND\n     1 pts\/0    Ss     0:00 bash\n     6 pts\/0    R+     0:00 ps faxww<\/code><\/pre>\n\n\n\n<p>to test if the new image is working we run it from the commandline and tell docker to execute a bash and attach us to a terminal (-i = inputstream, -t terminal).<\/p>\n\n\n\n<p>You may now push your new image on dockerhub, use it locally or push it to a private hub (e.g. harbor or a github project, etc.).<\/p>\n\n\n\n<p>If you choose to only use it locally, you&#8217;re done<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>https:\/\/www.docker.com\/products\/docker-hub<\/code><\/pre>\n\n\n\n<p>If you want to publish your image, create an account on docker-hub and then:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker tag stretchbase dockerhubusername\/stretchbase\ndocker login\ndocker push dockerhubusername\/stretchbase<\/code><\/pre>\n\n\n\n<p>You now have build and published your own dockerimage.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This Post covers the steps one will have to go through, to build ones own docker image. First we&#8217;ll need to create a basic image from which we&#8217;ll go on. I&#8217;ll use debian here, though I work on gentoo I like debian a lot and debootstrap is available on gentoo as well. First we create &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.nikster.de\/wordpress\/index.php\/2019\/05\/04\/how-to-build-your-own-docker-image\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to build a docker image from scratch&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[3,22,18,12,5],"class_list":["post-11","post","type-post","status-publish","format-standard","hentry","category-docker","tag-docker","tag-from-scratch","tag-how-to","tag-howto","tag-image","entry"],"_links":{"self":[{"href":"https:\/\/blog.nikster.de\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/11","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.nikster.de\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.nikster.de\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.nikster.de\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.nikster.de\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=11"}],"version-history":[{"count":3,"href":"https:\/\/blog.nikster.de\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/11\/revisions"}],"predecessor-version":[{"id":30,"href":"https:\/\/blog.nikster.de\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/11\/revisions\/30"}],"wp:attachment":[{"href":"https:\/\/blog.nikster.de\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=11"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.nikster.de\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=11"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.nikster.de\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=11"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}