{"id":7665,"date":"2021-06-28T02:59:28","date_gmt":"2021-06-28T02:59:28","guid":{"rendered":"https:\/\/linuxways.net\/?p=7665"},"modified":"2021-06-28T02:59:28","modified_gmt":"2021-06-28T02:59:28","slug":"dockerslim-to-minify-optimize-and-secure-docker-container-in-linux","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/centos\/dockerslim-to-minify-optimize-and-secure-docker-container-in-linux\/","title":{"rendered":"DockerSlim to Minify, Optimize, and Secure Docker Container in Linux"},"content":{"rendered":"<p>Most of the IT professionals who work on Docker get stuck at a point when it&#8217;s about the size of the docker containers. Sometimes the size of the docker images goes up to in GB\u2019s which consumes large storage of the system and makes it difficult to upgrade and start the container. Fortunately we have a free Open source tool in Linux\/Mac system which allows us to secure and minimize the size of the docker containers upto 30 times.<\/p>\n<p>Why use DockerSlim ?<\/p>\n<ol>\n<li>To minimize the container\u2019s size which reduces the storage usage.<\/li>\n<li>To create lightweight containers for faster starting<\/li>\n<li>Reduce bugs<\/li>\n<\/ol>\n<p>In this article, we will cover how to install DockerSlim and use it to minimize the docker container.<\/p>\n<h2>Installation<\/h2>\n<p>Dockerslim is currently supported on Linux and UNIX systems including Mac. The dockerslim binary files can be downloaded from <strong>DockerSlim GitHub Repository. <\/strong>Go to the GitHub repository link given below, navigate to installation and download the latest binaries for your system.<\/p>\n<p>DockerSlim GitHub Repository: <a href=\"https:\/\/github.com\/docker-slim\/docker-slim\">https:\/\/github.com\/docker-slim\/docker-slim<\/a><\/p>\n<p>Also, you can use wget command to download the latest DockerSlim binary files as:<\/p>\n<pre>$ wget <a href=\"https:\/\/downloads.dockerslim.com\/releases\/1.36.1\/dist_linux.tar.gz\">https:\/\/downloads.dockerslim.com\/releases\/1.36.1\/dist_linux.tar.gz<\/a><\/pre>\n<p>To extract the downloaded files, run the following command.<\/p>\n<pre>$ tar -zvxf dist_linux.tar.gz<\/pre>\n<p>Once the file is extracted, move the binary files to \/usr\/local\/bin directory as:<\/p>\n<pre>$ sudo mv dist_linux\/* \/usr\/local\/bin<\/pre>\n<p>To verify the installation of DockerSlim, run the following command.<\/p>\n<pre>$ which docker-slim<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"677\" height=\"157\" class=\"wp-image-7666\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-765.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-765.png 677w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-765-300x70.png 300w\" sizes=\"auto, (max-width: 677px) 100vw, 677px\" \/><\/p>\n<p>Run the docker docker-slim with option help for useful commands and guides.<\/p>\n<pre>$ docker-slim help<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1082\" height=\"411\" class=\"wp-image-7667\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-766.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-766.png 1082w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-766-300x114.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-766-1024x389.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-766-768x292.png 768w\" sizes=\"auto, (max-width: 1082px) 100vw, 1082px\" \/><\/p>\n<p>DockerSlim provides a very useful menu-driven command line. To try the available commands, run <strong>docker-slim<\/strong> without any arguments . In the DockerSlim shell, type <strong>help<\/strong> to get the help menu in the interactive prompt.<\/p>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1093\" height=\"138\" class=\"wp-image-7668\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-767.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-767.png 1093w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-767-300x38.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-767-1024x129.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-767-768x97.png 768w\" sizes=\"auto, (max-width: 1093px) 100vw, 1093px\" \/><\/p>\n<p>The basic commands includes the followings:<\/p>\n<p>Exit =&gt; Exit from DockerSlim shell<\/p>\n<p>Xray =&gt; Shows what is inside of the container image and reverse engineers its Dockerfile<\/p>\n<p>Version = &gt; Show DockerSlim and docker version information<\/p>\n<p>Lint =&gt; Analyzes container instructions in the Dockerfiles<\/p>\n<h2>Preparing a container to slim<\/h2>\n<p>To create a slim docker container, we need to have a docker image available in the local system. In this article, I have selected an httpd image. You can select the image as per your requirement. Use the following syntax to pull docker images from the dockerhub. Remember that you should have installed the docker engine and dependencies on your system<\/p>\n<p>Syntax :<\/p>\n<pre>$ docker pull &lt;image name&gt;<\/pre>\n<pre>$ docker pull httpd<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"947\" height=\"239\" class=\"wp-image-7669\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-768.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-768.png 947w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-768-300x76.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-768-768x194.png 768w\" sizes=\"auto, (max-width: 947px) 100vw, 947px\" \/><\/p>\n<h2>Verifying the Output of the Container<\/h2>\n<p>Once the image is pulled, run it as the container and verify the output. In this example, I have exposed port 80 of the httpd docker container and access the default apache page using the browser.<\/p>\n<pre>$ docker run -d --name apache -p 80:80 httpd<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"778\" height=\"245\" class=\"wp-image-7670\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-769.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-769.png 778w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-769-300x94.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-769-768x242.png 768w\" sizes=\"auto, (max-width: 778px) 100vw, 778px\" \/><\/p>\n<p>Also, to check the size of the docker images run the following command<\/p>\n<pre>$ docker images<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"648\" height=\"118\" class=\"wp-image-7671\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-770.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-770.png 648w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-770-300x55.png 300w\" sizes=\"auto, (max-width: 648px) 100vw, 648px\" \/><\/p>\n<h2>Using DockerSlim to build the image<\/h2>\n<p>Docker image has been pulled and we have verified the container is working fine. Now we will use DockerSlim to build optimized images. Use the following syntax to create minimized Docker images.<\/p>\n<pre>$ docker-slim build --target &lt;image name&gt;<\/pre>\n<p>Example:<\/p>\n<pre>$ docker-slim build --target httpd<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1275\" height=\"372\" class=\"wp-image-7672\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-771.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-771.png 1275w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-771-300x88.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-771-1024x299.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-771-768x224.png 768w\" sizes=\"auto, (max-width: 1275px) 100vw, 1275px\" \/><\/p>\n<p>The output shows that the size of the docker image has been reduced from 138MB to 8.1 MB which is minimized by 16.3 times from the original image.<\/p>\n<h2>Verifying the final image<\/h2>\n<p>To check the size of the final image, run the following command.<\/p>\n<pre>$ docker images<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"749\" height=\"127\" class=\"wp-image-7673\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-772.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-772.png 749w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-772-300x51.png 300w\" sizes=\"auto, (max-width: 749px) 100vw, 749px\" \/><\/p>\n<h2>Running the container from the DockerSlim build image<\/h2>\n<p>Finally, we will create a container from the optimized image and monitor the output. As I have used httpd docker images to minimize, I am going to run an httpd container from the latest image. You can select your images accordingly.<\/p>\n<p>Syntax:<\/p>\n<pre>$ docker run -d --name &lt;name of the container &gt; -p &lt;host port : container port&gt; &lt;image name&gt;<\/pre>\n<p>Example<\/p>\n<pre>$ docker run -d --name apache -p 80:80 httpd.slim<\/pre>\n<p>Where,<\/p>\n<p>httpd.slim is the optimized httpd docker image<\/p>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1162\" height=\"159\" class=\"wp-image-7674\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-773.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-773.png 1162w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-773-300x41.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-773-1024x140.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-773-768x105.png 768w\" sizes=\"auto, (max-width: 1162px) 100vw, 1162px\" \/><\/p>\n<p>You can find that the container is up with an optimized image. Lets browse the container in the browser and find the output.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"790\" height=\"241\" class=\"wp-image-7675\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-774.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-774.png 790w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-774-300x92.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-774-768x234.png 768w\" sizes=\"auto, (max-width: 790px) 100vw, 790px\" \/><\/p>\n<p>You can find that the httpd docker container is working fine with the minimized Docker image.<\/p>\n<h2>Conclusion<\/h2>\n<p>In this article, we have learned about how to install DockerSlim in the Linux system and how it can be used to optimize and secure Docker containers.<\/p>","protected":false},"excerpt":{"rendered":"<p>Most of the IT professionals who work on Docker get stuck at a point when it&#8217;s about the size of the docker containers. Sometimes the size of the&hellip;<\/p>","protected":false},"author":1,"featured_media":7829,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,4,5,83,165,2],"tags":[328,160,35],"class_list":["post-7665","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos","category-debian","category-mint","category-opensuse","category-red-hat","category-ubuntu","tag-container","tag-docker","tag-linux"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/7665","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=7665"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/7665\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/7829"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=7665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=7665"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=7665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}