{"id":492,"date":"2024-10-04T05:43:19","date_gmt":"2024-10-04T05:43:19","guid":{"rendered":"https:\/\/www.xopsschool.com\/tutorials\/?p=492"},"modified":"2024-10-07T05:49:59","modified_gmt":"2024-10-07T05:49:59","slug":"write-a-short-dockerfile-and-docker-image","status":"publish","type":"post","link":"https:\/\/www.xopsschool.com\/tutorials\/write-a-short-dockerfile-and-docker-image\/","title":{"rendered":"Write A Short Dockerfile And Docker Image"},"content":{"rendered":"\n<p>Step 1: Set Up Project Directory\/Folder Create a directory for your project files.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>                   mkdir myapp\n                   cd myapp<\/code><\/pre>\n\n\n\n<p>               <\/p>\n\n\n\n<p> Step 2: Create Application (Example: Simple Node.js App) Create a server.js file for a basic web server. you can use vs code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>                   \/\/ server.js\n                   const http = require('http');\n                   const port = 3000;\n                   http.createServer((req, res) => res.end('Hello from Docker!')).listen(port);<\/code><\/pre>\n\n\n\n<p>                  <\/p>\n\n\n\n<p> Step 3: Create package.json This file manages dependencies for Node.js. Create it in the same directory as server.js.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>                   {\n                      \"name\": \"myapp\",\n                      \"version\": \"1.0.0\",\n                      \"main\": \"server.js\",\n                      \"scripts\": {\n                      \"start\": \"node server.js\"\n                      }\n                    }<\/code><\/pre>\n\n\n\n<p>                   <\/p>\n\n\n\n<p>Step 4: Create Dockerfile In the same directory (all file create same folder), create a file called Dockerfile with the following contents:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>               # Use the official Node.js base image\n\n               FROM node:14 \n\n               # Set the working directory inside the container \n\n               WORKDIR \/usr\/src\/app\n\n               # Copy package.json and install dependencies\n\n               COPY package*.json .\/\n               RUN npm install\n\n              # Copy the rest of the application files\n\n               COPY . . \n\n               # Expose the app's port\n\n               EXPOSE 3000\n\n              # Run the app\n\n              CMD &#91;\"npm\", \"start\"]<\/code><\/pre>\n\n\n\n<p>               <\/p>\n\n\n\n<p>Step 5: Build Docker Image In your terminal (Command Prompt or Terminal or VS Code Terminal), run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> docker build -t myapp .<\/code><\/pre>\n\n\n\n<p>              <\/p>\n\n\n\n<p>Step 6: Run the Docker Container Run the container using the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> docker run -p 3000:3000 myapp<\/code><\/pre>\n\n\n\n<p>            <\/p>\n\n\n\n<p>Step 7: View the Result Open your browser and go to http:\/\/localhost:3000. You should see:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> Hello from Docker!<\/code><\/pre>\n\n\n\n<p>             <\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Step 1: Set Up Project Directory\/Folder Create a directory for your project files. Step 2: Create Application (Example: Simple Node.js App) Create a server.js file for a basic web server. you can use vs code. Step 3: Create package.json This file manages dependencies for Node.js. Create it in the same directory as server.js. Step 4: &#8230; <a title=\"Write A Short Dockerfile And Docker Image\" class=\"read-more\" href=\"https:\/\/www.xopsschool.com\/tutorials\/write-a-short-dockerfile-and-docker-image\/\" aria-label=\"Read more about Write A Short Dockerfile And Docker Image\">Read more<\/a><\/p>\n","protected":false},"author":200020,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-492","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Write A Short Dockerfile And Docker Image - XOps Tutorials!!!<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.xopsschool.com\/tutorials\/write-a-short-dockerfile-and-docker-image\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Write A Short Dockerfile And Docker Image - XOps Tutorials!!!\" \/>\n<meta property=\"og:description\" content=\"Step 1: Set Up Project Directory\/Folder Create a directory for your project files. Step 2: Create Application (Example: Simple Node.js App) Create a server.js file for a basic web server. you can use vs code. Step 3: Create package.json This file manages dependencies for Node.js. Create it in the same directory as server.js. Step 4: ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.xopsschool.com\/tutorials\/write-a-short-dockerfile-and-docker-image\/\" \/>\n<meta property=\"og:site_name\" content=\"XOps Tutorials!!!\" \/>\n<meta property=\"article:published_time\" content=\"2024-10-04T05:43:19+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-10-07T05:49:59+00:00\" \/>\n<meta name=\"author\" content=\"Pritesh Kumar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Pritesh Kumar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.xopsschool.com\/tutorials\/write-a-short-dockerfile-and-docker-image\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.xopsschool.com\/tutorials\/write-a-short-dockerfile-and-docker-image\/\"},\"author\":{\"name\":\"Pritesh Kumar\",\"@id\":\"https:\/\/www.xopsschool.com\/tutorials\/#\/schema\/person\/121833f332dc4a3674d2904cface44f1\"},\"headline\":\"Write A Short Dockerfile And Docker Image\",\"datePublished\":\"2024-10-04T05:43:19+00:00\",\"dateModified\":\"2024-10-07T05:49:59+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.xopsschool.com\/tutorials\/write-a-short-dockerfile-and-docker-image\/\"},\"wordCount\":127,\"commentCount\":0,\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.xopsschool.com\/tutorials\/write-a-short-dockerfile-and-docker-image\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.xopsschool.com\/tutorials\/write-a-short-dockerfile-and-docker-image\/\",\"url\":\"https:\/\/www.xopsschool.com\/tutorials\/write-a-short-dockerfile-and-docker-image\/\",\"name\":\"Write A Short Dockerfile And Docker Image - XOps Tutorials!!!\",\"isPartOf\":{\"@id\":\"https:\/\/www.xopsschool.com\/tutorials\/#website\"},\"datePublished\":\"2024-10-04T05:43:19+00:00\",\"dateModified\":\"2024-10-07T05:49:59+00:00\",\"author\":{\"@id\":\"https:\/\/www.xopsschool.com\/tutorials\/#\/schema\/person\/121833f332dc4a3674d2904cface44f1\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.xopsschool.com\/tutorials\/write-a-short-dockerfile-and-docker-image\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.xopsschool.com\/tutorials\/write-a-short-dockerfile-and-docker-image\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.xopsschool.com\/tutorials\/write-a-short-dockerfile-and-docker-image\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.xopsschool.com\/tutorials\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Write A Short Dockerfile And Docker Image\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.xopsschool.com\/tutorials\/#website\",\"url\":\"https:\/\/www.xopsschool.com\/tutorials\/\",\"name\":\"XOps Tutorials!!!\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.xopsschool.com\/tutorials\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.xopsschool.com\/tutorials\/#\/schema\/person\/121833f332dc4a3674d2904cface44f1\",\"name\":\"Pritesh Kumar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.xopsschool.com\/tutorials\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/231a0e8b7a02636f2fbacf8dcf4494cb1cc0d49ecc9a8165fbaeaeeaf102641a?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/231a0e8b7a02636f2fbacf8dcf4494cb1cc0d49ecc9a8165fbaeaeeaf102641a?s=96&d=mm&r=g\",\"caption\":\"Pritesh Kumar\"},\"description\":\"Devops Engineer at Cotocus pvt.ltd\",\"url\":\"https:\/\/www.xopsschool.com\/tutorials\/author\/priteshcotocus\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Write A Short Dockerfile And Docker Image - XOps Tutorials!!!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.xopsschool.com\/tutorials\/write-a-short-dockerfile-and-docker-image\/","og_locale":"en_US","og_type":"article","og_title":"Write A Short Dockerfile And Docker Image - XOps Tutorials!!!","og_description":"Step 1: Set Up Project Directory\/Folder Create a directory for your project files. Step 2: Create Application (Example: Simple Node.js App) Create a server.js file for a basic web server. you can use vs code. Step 3: Create package.json This file manages dependencies for Node.js. Create it in the same directory as server.js. Step 4: ... Read more","og_url":"https:\/\/www.xopsschool.com\/tutorials\/write-a-short-dockerfile-and-docker-image\/","og_site_name":"XOps Tutorials!!!","article_published_time":"2024-10-04T05:43:19+00:00","article_modified_time":"2024-10-07T05:49:59+00:00","author":"Pritesh Kumar","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Pritesh Kumar","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.xopsschool.com\/tutorials\/write-a-short-dockerfile-and-docker-image\/#article","isPartOf":{"@id":"https:\/\/www.xopsschool.com\/tutorials\/write-a-short-dockerfile-and-docker-image\/"},"author":{"name":"Pritesh Kumar","@id":"https:\/\/www.xopsschool.com\/tutorials\/#\/schema\/person\/121833f332dc4a3674d2904cface44f1"},"headline":"Write A Short Dockerfile And Docker Image","datePublished":"2024-10-04T05:43:19+00:00","dateModified":"2024-10-07T05:49:59+00:00","mainEntityOfPage":{"@id":"https:\/\/www.xopsschool.com\/tutorials\/write-a-short-dockerfile-and-docker-image\/"},"wordCount":127,"commentCount":0,"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.xopsschool.com\/tutorials\/write-a-short-dockerfile-and-docker-image\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.xopsschool.com\/tutorials\/write-a-short-dockerfile-and-docker-image\/","url":"https:\/\/www.xopsschool.com\/tutorials\/write-a-short-dockerfile-and-docker-image\/","name":"Write A Short Dockerfile And Docker Image - XOps Tutorials!!!","isPartOf":{"@id":"https:\/\/www.xopsschool.com\/tutorials\/#website"},"datePublished":"2024-10-04T05:43:19+00:00","dateModified":"2024-10-07T05:49:59+00:00","author":{"@id":"https:\/\/www.xopsschool.com\/tutorials\/#\/schema\/person\/121833f332dc4a3674d2904cface44f1"},"breadcrumb":{"@id":"https:\/\/www.xopsschool.com\/tutorials\/write-a-short-dockerfile-and-docker-image\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.xopsschool.com\/tutorials\/write-a-short-dockerfile-and-docker-image\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.xopsschool.com\/tutorials\/write-a-short-dockerfile-and-docker-image\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.xopsschool.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Write A Short Dockerfile And Docker Image"}]},{"@type":"WebSite","@id":"https:\/\/www.xopsschool.com\/tutorials\/#website","url":"https:\/\/www.xopsschool.com\/tutorials\/","name":"XOps Tutorials!!!","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.xopsschool.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.xopsschool.com\/tutorials\/#\/schema\/person\/121833f332dc4a3674d2904cface44f1","name":"Pritesh Kumar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.xopsschool.com\/tutorials\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/231a0e8b7a02636f2fbacf8dcf4494cb1cc0d49ecc9a8165fbaeaeeaf102641a?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/231a0e8b7a02636f2fbacf8dcf4494cb1cc0d49ecc9a8165fbaeaeeaf102641a?s=96&d=mm&r=g","caption":"Pritesh Kumar"},"description":"Devops Engineer at Cotocus pvt.ltd","url":"https:\/\/www.xopsschool.com\/tutorials\/author\/priteshcotocus\/"}]}},"_links":{"self":[{"href":"https:\/\/www.xopsschool.com\/tutorials\/wp-json\/wp\/v2\/posts\/492","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xopsschool.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xopsschool.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xopsschool.com\/tutorials\/wp-json\/wp\/v2\/users\/200020"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xopsschool.com\/tutorials\/wp-json\/wp\/v2\/comments?post=492"}],"version-history":[{"count":2,"href":"https:\/\/www.xopsschool.com\/tutorials\/wp-json\/wp\/v2\/posts\/492\/revisions"}],"predecessor-version":[{"id":515,"href":"https:\/\/www.xopsschool.com\/tutorials\/wp-json\/wp\/v2\/posts\/492\/revisions\/515"}],"wp:attachment":[{"href":"https:\/\/www.xopsschool.com\/tutorials\/wp-json\/wp\/v2\/media?parent=492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xopsschool.com\/tutorials\/wp-json\/wp\/v2\/categories?post=492"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xopsschool.com\/tutorials\/wp-json\/wp\/v2\/tags?post=492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}