{"id":22499,"date":"2023-10-20T14:34:24","date_gmt":"2023-10-20T14:34:24","guid":{"rendered":"https:\/\/linuxways.net\/?p=22499"},"modified":"2023-10-23T12:47:00","modified_gmt":"2023-10-23T12:47:00","slug":"build-configure-custom-docker-images-dockerfile","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/linux-commands\/build-configure-custom-docker-images-dockerfile\/","title":{"rendered":"How to Build and Configure the Custom Docker Images with Dockerfile"},"content":{"rendered":"<p>Docker has altered the way the software is created, distributed, and executed by providing a lightweight and efficient containerization platform. Docker&#8217;s ability to automate the construction and configuration of the bespoke Docker images using Dockerfiles is a crucial component of its success. Dockerfiles are simple text files that include a set of instructions to automatically build a Docker image.<\/p>\n<p>In this article, we will talk about the power and variety of Dockerfiles, as well as how to utilize them to automate the creation of the custom Docker images. We will go over some best practices to create the efficient and manageable Dockerfile settings, as well as how to define and organize the Dockerfile instructions. At the end, we can understand how to use the Dockerfile to automatically produce and configure the custom Docker images, streamlining our containerization workflow.<\/p>\n<h2><strong>Installing Docker in Centos<\/strong><\/h2>\n<p>The system packages should be updated:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\"># <\/span><span class=\"kw2\">sudo<\/span> <span class=\"kw2\">yum update<\/span><\/div><\/div>\n<p>Install the following packages in order to set up the Docker repository:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\"># <\/span><span class=\"kw2\">sudo<\/span> <span class=\"kw2\">yum install<\/span> <span class=\"re5\">-y<\/span> yum-utils device-mapper-persistent-data lvm2<\/div><\/div>\n<p>Set up the Docker repository as follows:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\"># <\/span><span class=\"kw2\">sudo<\/span> yum-config-manager <span class=\"re5\">--add-repo<\/span> https:<span class=\"sy0\">\/\/<\/span>download.docker.com<span class=\"sy0\">\/<\/span>linux<span class=\"sy0\">\/<\/span>centos<span class=\"sy0\">\/<\/span>docker-ce.repo<\/div><\/div>\n<p>The Docker Engine must be installed:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\"># <\/span><span class=\"kw2\">sudo<\/span> <span class=\"kw2\">yum install<\/span> <span class=\"re5\">-y<\/span> docker-ce docker-ce-cli containerd.io<\/div><\/div>\n<p>Docker may be installed by &#8221;\/\u201d. The following commands can be used to start the daemon, verify its state, and allow it system-wide:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co0\"># systemctl start docker<\/span><br \/>\n<br \/>\n<span class=\"co0\"># systemctl status docker<\/span><br \/>\n<br \/>\n<span class=\"co0\"># systemctl enable docker<\/span><\/div><\/div>\n<p>Run a hello-world container to test the Docker installation:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\"># <\/span><span class=\"kw2\">sudo<\/span> docker run hello-world<\/div><\/div>\n<p>We may now execute a few basic Docker commands to learn more about Docker:<\/p>\n<p>Docker system-wide information:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\"># <\/span>docker info<\/div><\/div>\n<p>Moreover, we may examine the Docker version:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\"># <\/span>docker version<\/div><\/div>\n<p>Type \u201cdocker\u201d on our console to see a list of all possible Docker commands.<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\"># <\/span>docker<\/div><\/div>\n<h2><strong>Making or Creating a Dockerfile Repository<\/strong><\/h2>\n<p>First, let&#8217;s create several Dockerfile repositories so that we can reuse the files to generate more images in the future. Make an empty directory in the \u201c\/var\u201d partition where we construct the file that contains the instructions to build the freshly generated Docker image.<\/p>\n<p>Then, begin editing the file as follows:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\"># <\/span><span class=\"kw2\">vi<\/span> <span class=\"sy0\">\/<\/span>var<span class=\"sy0\">\/<\/span>docker<span class=\"sy0\">\/<\/span>ubuntu<span class=\"sy0\">\/<\/span>apache<span class=\"sy0\">\/<\/span>Dockerfile<\/div><\/div>\n<p>After that, insert the following:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">FROM ubuntu<br \/>\n<br \/>\nMAINTAINER your_name <span class=\"sy0\">&lt;<\/span>user<span class=\"sy0\">@<\/span>domain.tld<span class=\"sy0\">&gt;<\/span><br \/>\n<br \/>\nRUN <span class=\"kw2\">apt-get<\/span> <span class=\"re5\">-y<\/span> <span class=\"kw2\">install<\/span> apache2<\/div><\/div>\n<h2><strong>Creating a New Docker Image Named \u201cUbuntu-Apache\u201d<\/strong><\/h2>\n<p>The next step is to begin constructing the image by running the following command which creates a new Docker image named \u201cubuntu-apache\u201d that is locally based on the Dockerfile that is prepared earlier as demonstrated in this example:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\"># <\/span>docker build <span class=\"re5\">-t<\/span> ubuntu-apache <span class=\"sy0\">\/<\/span>var<span class=\"sy0\">\/<\/span>docker<span class=\"sy0\">\/<\/span>ubuntu<span class=\"sy0\">\/<\/span>apache<span class=\"sy0\">\/<\/span><\/div><\/div>\n<p>After Docker has built the image, we may use the following command to display all available images and identify our image:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\"># <\/span>docker images<\/div><\/div>\n<h2><strong>Starting the Container and Connecting to Apache through LAN<\/strong><\/h2>\n<p>To keep the container running endlessly and to access the container&#8217;s exposed services and ports from the host or another remote workstation in our LAN, type the following command into our host terminal prompt:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\"># <\/span>docker run <span class=\"re5\">-d<\/span> <span class=\"re5\">-p<\/span> <span class=\"nu0\">81<\/span>:<span class=\"nu0\">80<\/span> ubuntu-apache<\/div><\/div>\n<p>To display the network interface IP addresses, use the following IP command line:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co0\"># ip addr [List nework interfaces]<\/span><br \/>\n<br \/>\n<span class=\"co0\"># curl ip-address:81 [System Docker IP Address]<\/span><br \/>\n<br \/>\n<span class=\"co0\"># curl localhost:81 [Localhost]<\/span><\/div><\/div>\n<h2><strong>Make a Docker Container System-Wide Configuration File<\/strong><\/h2>\n<p>Make a new systemd file called \u201capache-docker.service\u201d. Then, execute the following command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\"># <\/span><span class=\"kw2\">vi<\/span> <span class=\"sy0\">\/<\/span>etc<span class=\"sy0\">\/<\/span>systemd<span class=\"sy0\">\/<\/span>system<span class=\"sy0\">\/<\/span>apache-docker.service<\/div><\/div>\n<p>After that, insert the following:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"br0\">&#91;<\/span>Unit<span class=\"br0\">&#93;<\/span><br \/>\n<br \/>\n<span class=\"re2\">Description<\/span>=apache container<br \/>\n<br \/>\n<span class=\"re2\">Requires<\/span>=docker.service<br \/>\n<br \/>\n<span class=\"re2\">After<\/span>=docker.service<br \/>\n<br \/>\n<span class=\"br0\">&#91;<\/span>Service<span class=\"br0\">&#93;<\/span><br \/>\n<br \/>\n<span class=\"re2\">Restart<\/span>=always<br \/>\n<br \/>\n<span class=\"re2\">ExecStart<\/span>=<span class=\"sy0\">\/<\/span>usr<span class=\"sy0\">\/<\/span>bin<span class=\"sy0\">\/<\/span>docker start <span class=\"re5\">-a<\/span> my-www<br \/>\n<br \/>\n<span class=\"re2\">ExecStop<\/span>=<span class=\"sy0\">\/<\/span>usr<span class=\"sy0\">\/<\/span>bin<span class=\"sy0\">\/<\/span>docker stop <span class=\"re5\">-t<\/span> <span class=\"nu0\">2<\/span> my-www<br \/>\n<br \/>\n<span class=\"br0\">&#91;<\/span>Install<span class=\"br0\">&#93;<\/span><br \/>\n<br \/>\n<span class=\"re2\">WantedBy<\/span>=local.target<\/div><\/div>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>Automating the construction and setup of Docker images with Dockerfile is a powerful solution that simplifies the containerization workflow. By knowing the fundamentals of Dockerfile instructions, optimizing the settings, and utilizing the build tools, we can expedite the process and save time. We can also assure a consistent and dependable deployments of their apps within the Docker containers.<\/p>","protected":false},"excerpt":{"rendered":"<p>Tutorial on the power of Dockerfiles and how to utilize them to automate the creation of the custom Docker images to create efficient Dockerfile settings.<\/p>","protected":false},"author":111,"featured_media":22501,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1049],"tags":[],"class_list":["post-22499","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux-commands"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/22499","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\/111"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=22499"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/22499\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/22501"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=22499"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=22499"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=22499"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}