{"id":16793,"date":"2021-02-17T12:19:01","date_gmt":"2021-02-17T12:19:01","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/blog\/?p=16793"},"modified":"2024-11-07T07:17:52","modified_gmt":"2024-11-07T07:17:52","slug":"dockerize-react-app","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/blog\/dockerize-react-app","title":{"rendered":"How to Dockerize React App: A Complete Guide"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>A few days back, I had to <b>dockerize React app<\/b>. That was quite challenging because I had no experience with Docker. I had to start from scratch to understand the dockerize react app, its prerequisites, installation, advantages, and disadvantages. Finding everything in one place about the dockerizing a react app was a real struggle. And that\u2019s when I thought I\u2019d indeed write a blog post to lessen fellow React developers\u2019 struggle to deploy a Docker in a React Application. Here\u2019s the list of subtopics that we will discuss in detail on how to containerize React app.<\/p>\n<h2>Prerequisites: Dockerize React App<\/h2>\n<ul class=\"bullets text-left\">\n<li>Docker installed in your system.<\/li>\n<li>Create an account at the Docker Hub registry for pushing and pulling the Docker images. It\u2019s absolutely free; you can visit <strong><a href=\"https:\/\/hub.docker.com\/\" target=\"_blank\" rel=\"noopener\">Docker Hub<\/a><\/strong> for registration.<\/li>\n<li>As the blog focuses on dockerizing React app, we will need a demo application to implement Docker in a React Application. If you don\u2019t have a ReactJS app, don\u2019t worry; you can just follow the next section\u2019s steps.<\/li>\n<\/ul>\n<h2>Create a React App<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/02\/17-02-2021-Dockerize-React-\u2013-2-300x173.png\" alt=\"Docker with react\" width=\"600\" height=\"600\" class=\"alignnone size-medium wp-image-16817\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/02\/17-02-2021-Dockerize-React-\u2013-2-150x150.png 150w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/02\/17-02-2021-Dockerize-React-\u2013-2-60x60.png 60w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>If your system doesn\u2019t have create-react-app installed, run the below command.<\/p>\n<pre>npm i create-react-app --global<\/pre>\n<p>Here, we will create a new ReactJs application; if you want to use an existing app, that works.<\/p>\n<pre>create-react-app demo-react-docker-app<\/pre>\n<p>Go to <mark>demo-react-docker-app<\/mark> and run the command to make sure it\u2019s working.<\/p>\n<pre>cd demo-react-docker-app\r\nnpm start<\/pre>\n<p class=\"boxed bg--secondary\" style=\"border: 1px solid #c7c7c7; box-shadow: 0 0 40px rgba(0, 0, 0, 0.2);\"><strong><em><span style=\"font-size:22px; color:#000;\">Struggling to implement Docker with React application?<\/span><br \/>\nLooking for dedicated ReactJS developers with extraordinary problem-solving skills? Here we are to lessen your burden! Contact us today and hire <a href=\"https:\/\/www.bacancytechnology.com\/hire-reactjs-developer\" target=\"_blank\" rel=\"noopener\">ReactJS developer<\/a>!<\/em><\/strong><\/p>\n<h2>Create Dockerfile for React App<\/h2>\n<p>Create a file, Dockerfile, and add it to the root directory.<\/p>\n<pre># pull the official base image\r\nFROM node: alpine\r\n# set working direction\r\nWORKDIR \/app\r\n# add `\/app\/node_modules\/.bin` to $PATH\r\nENV PATH \/app\/node_modules\/.bin:$PATH\r\n# install application dependencies\r\nCOPY package.json .\/\r\nCOPY package-lock.json .\/\r\nRUN npm i\r\n# add app\r\nCOPY . .\/\r\n# start app\r\nCMD [\"npm\", \"start\"]\r\n<\/pre>\n<h2>Add a .dockerignore file<\/h2>\n<p>Though it\u2019s not mandatory to have a .dockerignore file, it\u2019s a good practice to add it as it speeds up the build process of the Docker image and excludes the unnecessary files.<\/p>\n<p>Here\u2019s the code which you have to add to your <mark>.dockerignore<\/mark> file.<\/p>\n<pre>node_modules\r\nnpm-debug.log\r\nbuild\r\n.dockerignore\r\n**\/.git\r\n**\/.DS_Store\r\n**\/node_modules\r\n<\/pre>\n<h2>Create Docker Image<\/h2>\n<p>After running the Docker build command, you&#8217;ll be able to create Docker Image with the help of Dockerfile. I have named my Docker Image as a sample:react-app, but you can replace it with your choice. Please keep in mind that a dot must follow the Docker image as it means that the path of the Docker build and Dockerfile is the same. <\/p>\n<pre>docker build -t sample:react-app<\/pre>\n<p>If you wish to list the images which are generated with your React Application Image, then run the following command.<\/p>\n<pre>docker images<\/pre>\n<h2>Run the Docker Container<\/h2>\n<p>Moving on towards the last step of how to <em>Dockerize React app<\/em>.<\/p>\n<p>We have successfully made Docker Image; now follow the below-mentioned command to Run react app on the Docker image. Please verify your image name (here sample:react-app) that you have given.<\/p>\n<pre>docker run \\\r\n    -it \\\r\n    --rm \\\r\n    -v ${PWD}:\/app \\\r\n    -v \/app\/node_modules \\\r\n    -p 3001:3000 \\\r\n    -e CHOKIDAR_USEPOLLING=true \\\r\n    sample:react-app<\/pre>\n<p>So, these were the steps to dockerize react app. I hope this blog post has helped and satisfied your requirements and now you are able to Dockerizing a React App. Just follow and run the commands as mentioned above and dockerize react app.<\/p>\n<h2>Conclusion<\/h2>\n<p>I hope you find this tutorial helpful to create React app with docker. If you are struggling or looking for a helping hand to dockerize React app, then lessen your struggle by combining docker and React application with the help of our expert senior React developer. Work with our <a href=\"https:\/\/www.bacancytechnology.com\/reactjs-development\" target=\"_blank\" rel=\"noopener\">Reactjs development company<\/a> to Dockerize ReactJs App.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction A few days back, I had to dockerize React app. That was quite challenging because I had no experience with Docker. I had to start from scratch to understand the dockerize react app, its prerequisites, installation, advantages, and disadvantages. Finding everything in one place about the dockerizing a react app was a real struggle. [&hellip;]<\/p>\n","protected":false},"author":151,"featured_media":16812,"comment_status":"open","ping_status":"open","sticky":false,"template":"blog-new-template.php","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"_lmt_disableupdate":"no","_lmt_disable":"no","footnotes":""},"categories":[988],"tags":[],"coauthors":[2429],"class_list":["post-16793","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-react"],"acf":[],"modified_by":"Binal Prajapati","_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts\/16793","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/users\/151"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/comments?post=16793"}],"version-history":[{"count":0,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts\/16793\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/media\/16812"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/media?parent=16793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/categories?post=16793"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/tags?post=16793"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/coauthors?post=16793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}