{"id":23593,"date":"2022-10-03T14:08:34","date_gmt":"2022-10-03T14:08:34","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=23593"},"modified":"2025-07-12T05:40:18","modified_gmt":"2025-07-12T05:40:18","slug":"using-a-dockerfile-to-build-a-fedora-container","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/using-a-dockerfile-to-build-a-fedora-container\/","title":{"rendered":"Docker Tutorials: Image &#8211; Dockerfile to Build a Fedora Container"},"content":{"rendered":"\n<p>The administrator may decide that building interactively is tedious and error-prone. Instead the administrator could create a Dockerfile that layers on the Apache Web server and the web site content in one build.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Learning<\/strong><\/h3>\n\n\n\n<p>The&nbsp;docker run&nbsp;command must specify an&nbsp;<em>IMAGE<\/em>&nbsp;to derive the container from. An image developer can define image defaults related to:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>detached or foreground running<\/li><li>container identification<\/li><li>network settings<\/li><li>runtime constraints on CPU and memory<\/li><\/ul>\n\n\n\n<p><strong>Detached vs foreground<\/strong><br>When starting a Docker container, you must first decide if you want to run the container in the background in a \u201cdetached\u201d mode or in the default foreground mode:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Detached (-d)<\/strong><\/h3>\n\n\n\n<p>To start a container in detached mode, you use -d=true or just -d option. By design, containers started in detached mode exit when the root process used to run the container exits.<\/p>\n\n\n\n<p>Do not pass a service x start command to a detached container. For example, this command attempts to start the nginx service.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ docker run -d -p 80:80 my_image service nginx start<\/pre>\n\n\n\n<p>This succeeds in starting the nginx service inside the container. However, it fails the detached container paradigm in that, the root process (service nginx start) returns and the detached container stops as designed. As a result, the nginx service is started but could not be used.<\/p>\n\n\n\n<p>Instead, to start a process such as the nginx web server do the following:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> $ docker run -d -p 80:80 my_image nginx -g 'daemon off;'<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Foreground<\/strong><\/h3>\n\n\n\n<p><br>In foreground mode (the default when -d is not specified), docker run can start the process in the container and attach the console to the process\u2019s standard input, output, and standard error.<\/p>\n\n\n\n<p>It can even pretend to be a TTY (this is what most command line executables expect) and pass along signals.<\/p>\n\n\n\n<p>You can specify to which of the three standard streams (STDIN, STDOUT, STDERR) you\u2019d like to connect instead, as in:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ docker run -a stdin -a stdout -i -t ubuntu \/bin\/bash<\/pre>\n\n\n\n<p>For interactive processes (like a shell), you must use -i -t together in order to allocate a tty for the container process. -i -t is often written -it as you\u2019ll see in later examples.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Objective<\/strong><\/h3>\n\n\n\n<p><strong>Create one mysite.tar with some dummy index.html and images.<\/strong><\/p>\n\n\n\n<p>A good practice is to make a sub-directory with a related name and create a Dockerfile in that directory. E.g. a directory called mongo may contain a Dockerfile for a MongoDB image, or a directory called httpd may container an Dockerfile for an Apache web server. Copy or create all other content that you wish to add to the image into the new directory. Keep in mind that the ADD directive context is relative to this new directory.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># mkdir httpd  \n# cp mysite.tar httpd\/ <\/pre>\n\n\n\n<p><strong>Create the Dockerfile in the httpd directory. This Dockerfile will use the same base image as the interactive command&nbsp;fedora:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">FROM fedora \nMAINTAINER Rajesh Kumar email: devops@rajeshkumar.xyz\n\n# Update the image with the latest packages (recommended) \nRUN yum update -y; yum clean all\n\n# Install Apache Web Server \nRUN yum install -y httpd; yum clean all\n\n# Add the tar file of the web site \nADD mysite.tar \/tmp\/\n\n# Docker automatically extracted. So move files to web directory \nRUN mv \/tmp\/mysite\/* \/var\/www\/html \nEXPOSE 80 \nENTRYPOINT [ \"\/usr\/sbin\/httpd\" ] \nCMD [ \"-D\", \"FOREGROUND\" ]\n<\/pre>\n\n\n\n<p><strong>Build this Dockerfile from the new httpd directory and run it:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># docker build --rm -t newsite httpd\/   # docker run -d -P newsite <\/pre>\n\n\n\n<p>The container build process builds a series of temporary image layers based on the directives in the&nbsp;<strong>Dockerfile<\/strong>. These temporary layers are cached so if you make modifications to the content tarball, it won\u2019t completely rebuild and update the Fedora image.&nbsp;<strong>Since each directive is a new layer, you could reduce the number of layers by combining the&nbsp;RUN yum&nbsp;directives into a single&nbsp;RUN&nbsp;directive:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">RUN yum -y install httpd &amp;&amp; yum -y update; yum clean all <\/pre>\n\n\n\n<p>Planning your layers will determine how many layers need to be recreated on each build of the container.<\/p>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_86566\"><iframe loading=\"lazy\"  id=\"_ytid_71900\"  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_86566\"  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>The administrator may decide that building interactively is tedious and error-prone. Instead the administrator could create a Dockerfile that layers on the Apache Web server and the web site content&#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-23593","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/23593","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=23593"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/23593\/revisions"}],"predecessor-version":[{"id":31421,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/23593\/revisions\/31421"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=23593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=23593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=23593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}