{"id":520,"date":"2022-06-24T18:43:00","date_gmt":"2022-06-24T18:43:00","guid":{"rendered":"http:\/\/frontendguruji.com\/?p=520"},"modified":"2022-06-30T12:59:11","modified_gmt":"2022-06-30T12:59:11","slug":"how-to-create-react-app-in-docker-container","status":"publish","type":"post","link":"https:\/\/frontendguruji.com\/blog\/how-to-create-react-app-in-docker-container\/","title":{"rendered":"How to Create React App in Docker Container &#8211; React 18, Docker Engine 20.10, Windows 11"},"content":{"rendered":"\n<p>Hello Friends &#x1f64f;,&nbsp;in this tutorial we will create React App in Docker Container and will also configure it for Hot Reloading &#x1f680;.<\/p>\n\n\n\n<h2>What is a Docker<\/h2>\n\n\n\n<p>Docker is used to package applications into containers, it contains all application source code with the operating system (OS) libraries and dependencies required to run that code in any environment.<br>&#x1f53d;Setup Docker on Windows\/Linux: <a href=\"https:\/\/www.docker.com\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">https:\/\/www.docker.com\/<\/a><\/p>\n\n\n\n<h2>Why to use Docker<\/h2>\n\n\n\n<p>You must have been in a situation in your career where code must be running in your system but not on others system quoting <strong>&#8220;mere system me to chal raha hai&#8221; <\/strong>&#x1f601;. Well, Docker solves that problem by packing application with all required dependencies in a container which can be easily shared and deployed to any OS running Docker &#x1f525;.<\/p>\n\n\n\n<p>Overall it improves &#x2705;Development, &#x2705;Deployment and &#x2705;Business Agility<\/p>\n\n\n\n<h2>What&#8217;s in this Tutorial<\/h2>\n\n\n\n<p>In this docker react tutorial, we<br>&#8211; will configure docker in a react project<br>&#8211; will use Node image from Docker cloud<br>&#8211; will modify it by copying our application code<br>&#8211; will create a modified image<br>&#8211; will run the modified image inside the docker<br>&#8211; will access the Docker port from our localhost<br>&#8211; will also implement Hot Reloading so that browser reflects the code changes automatically.<\/p>\n\n\n\n<p>I am using Windows 11 while writing tutorial, React version is 18 and Docker Engine is 20.10. <br>You must setup docker on Windows or Mac before proceeding.<\/p>\n\n\n\n<p><strong>Chalo suru kiya jaye!<\/strong>&#x1f37b;<strong>&nbsp;Lets start!&nbsp;<\/strong>&#x1f57a;.<\/p>\n\n\n\n<h2>Setup React App in Docker Container<\/h2>\n\n\n\n<h3>Create React App<\/h3>\n\n\n\n<p>First thing! create a React app using create-react-app toolkit<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">npx create-react-app react-docker<\/pre>\n\n\n\n<h3>Create Dockerfile<\/h3>\n\n\n\n<p>Now create a dockerfile to define the tasks to be execuited by docker.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img width=\"296\" height=\"275\" src=\"http:\/\/frontendguruji.com\/wp-content\/uploads\/2022\/06\/image-1.png\" alt=\"create react app in docker container, docker hot reload react, docker hot reload not working, docker react tutorial, setup docker on windows\" class=\"wp-image-522\" srcset=\"https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-1.png 296w, https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-1-150x139.png 150w\" sizes=\"(max-width: 296px) 100vw, 296px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img width=\"322\" height=\"203\" src=\"http:\/\/frontendguruji.com\/wp-content\/uploads\/2022\/06\/image-2.png\" alt=\"create react app in docker container, docker hot reload react, docker hot reload not working, docker react tutorial, setup docker on windows\" class=\"wp-image-523\" srcset=\"https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-2.png 322w, https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-2-300x189.png 300w, https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-2-150x95.png 150w\" sizes=\"(max-width: 322px) 100vw, 322px\" \/><\/figure>\n\n\n\n<p><strong>FROM <\/strong>node &#8211; fetch node image from docker cloud<br><strong>WORKDIR <\/strong>\/app &#8211; set the docker container root directory<br><strong>COPY <\/strong>package.json . &#8211; copy the package.json to container directory (&#8216;.&#8217; or &#8216;\/app&#8217;)<br><strong>RUN<\/strong> npm install &#8211; install the dependencies of the project in container directory<br><strong>COPY <\/strong>. . &#8211; copy all files from current directory to container directory<br><strong>EXPOSE <\/strong>3000 &#8211; open 3000 port of container<br><strong>CMD <\/strong>[&#8220;npm&#8221;, &#8220;start&#8221;] &#8211; start the app in the container <\/p>\n\n\n\n<p>We need Node image because this React app will be build and served from node server.<\/p>\n\n\n\n<h3>Create .dockerignore<\/h3>\n\n\n\n<p>Thsi file works exactly same as gitignore file, stopping non required files from getting copied to container directory.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img width=\"288\" height=\"272\" src=\"http:\/\/frontendguruji.com\/wp-content\/uploads\/2022\/06\/image-3.png\" alt=\"create react app in docker container, docker hot reload react, docker hot reload not working, docker react tutorial, setup docker on windows\" class=\"wp-image-524\" srcset=\"https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-3.png 288w, https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-3-150x142.png 150w\" sizes=\"(max-width: 288px) 100vw, 288px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img width=\"265\" height=\"191\" src=\"http:\/\/frontendguruji.com\/wp-content\/uploads\/2022\/06\/image-4.png\" alt=\"create react app in docker container, docker hot reload react, docker hot reload not working, docker react tutorial, setup docker on windows\" class=\"wp-image-525\" srcset=\"https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-4.png 265w, https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-4-150x108.png 150w\" sizes=\"(max-width: 265px) 100vw, 265px\" \/><\/figure>\n\n\n\n<h3>Build the project image<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">docker build -t react-image<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img width=\"917\" height=\"801\" src=\"http:\/\/frontendguruji.com\/wp-content\/uploads\/2022\/06\/image-6.png\" alt=\"create react app in docker container, docker hot reload react, docker hot reload not working, docker react tutorial, setup docker on windows\" class=\"wp-image-527\" srcset=\"https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-6.png 917w, https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-6-300x262.png 300w, https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-6-768x671.png 768w, https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-6-150x131.png 150w\" sizes=\"(max-width: 917px) 100vw, 917px\" \/><\/figure>\n\n\n\n<h3>Verify the files copied in Docker container<\/h3>\n\n\n\n<p>Check if correct files are getting copied to the docker container<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker exec -it react-app bash<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img src=\"http:\/\/frontendguruji.com\/wp-content\/uploads\/2022\/06\/image-12.png\" alt=\"create react app in docker container, docker hot reload react, docker hot reload not working, docker react tutorial, setup docker on windows\" class=\"wp-image-533\" width=\"658\" height=\"63\" srcset=\"https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-12.png 658w, https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-12-300x29.png 300w, https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-12-150x14.png 150w\" sizes=\"(max-width: 658px) 100vw, 658px\" \/><\/figure>\n\n\n\n<h3>Run the project image<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">docker run -d -p 3000:3000 --name react-app react-image<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img width=\"755\" height=\"47\" src=\"http:\/\/frontendguruji.com\/wp-content\/uploads\/2022\/06\/image-9.png\" alt=\"\" class=\"wp-image-530\" srcset=\"https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-9.png 755w, https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-9-300x19.png 300w, https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-9-150x9.png 150w\" sizes=\"(max-width: 755px) 100vw, 755px\" \/><\/figure>\n\n\n\n<p><strong>-d<\/strong> &#8211; running the container in background<br><strong>-p<\/strong> 3000:3000 &#8211; this is to connect port of the local machine to docker container port, so that localmachine can access project running inside docker.<br><strong>&#8211;name<\/strong> react-app &#8211; define name of the app<br><strong>react-image<\/strong> &#8211; name of the project image created earlier<\/p>\n\n\n\n<h3>Verify the image<\/h3>\n\n\n\n<p>Now verify if image is created successfully<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker image ls<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img width=\"608\" height=\"65\" src=\"http:\/\/frontendguruji.com\/wp-content\/uploads\/2022\/06\/image-8.png\" alt=\"create react app in docker container, docker hot reload react, docker hot reload not working, docker react tutorial, setup docker on windows\" class=\"wp-image-529\" srcset=\"https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-8.png 608w, https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-8-300x32.png 300w, https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-8-150x16.png 150w\" sizes=\"(max-width: 608px) 100vw, 608px\" \/><\/figure>\n\n\n\n<h3>Verify project in browser<\/h3>\n\n\n\n<figure class=\"wp-block-image size-full\"><img width=\"499\" height=\"447\" src=\"http:\/\/frontendguruji.com\/wp-content\/uploads\/2022\/06\/image-10.png\" alt=\"create react app in docker container, docker hot reload react, docker hot reload not working, docker react tutorial, setup docker on windows\" class=\"wp-image-531\" srcset=\"https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-10.png 499w, https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-10-300x269.png 300w, https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-10-150x134.png 150w\" sizes=\"(max-width: 499px) 100vw, 499px\" \/><\/figure>\n\n\n\n<h3>Activate Hot Reloading<\/h3>\n\n\n\n<p>Kill the already running container<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker rm reactapp -f<\/pre>\n\n\n\n<p>Now add <strong>-v<\/strong> flag for <strong>volume bind mount<\/strong> in docker run command<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img width=\"925\" height=\"37\" src=\"http:\/\/frontendguruji.com\/wp-content\/uploads\/2022\/06\/image-13.png\" alt=\"create react app in docker container, docker hot reload react, docker hot reload not working, docker react tutorial, setup docker on windows\" class=\"wp-image-534\" srcset=\"https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-13.png 925w, https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-13-300x12.png 300w, https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-13-768x31.png 768w, https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-13-150x6.png 150w\" sizes=\"(max-width: 925px) 100vw, 925px\" \/><\/figure>\n\n\n\n<p><strong>dirlocaldirectory<\/strong> is the local directory path &#8211; this  is full path of the directory<br><strong>containerdirectory<\/strong> is the directory path of container<\/p>\n\n\n\n<p><strong>Get full path of directory<\/strong> &#8211; right click on the left panel in Visualstudio and select <strong>Copy Path<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img width=\"481\" height=\"641\" src=\"http:\/\/frontendguruji.com\/wp-content\/uploads\/2022\/06\/image-14-edited-1.png\" alt=\"create react app in docker container, docker hot reload react, docker hot reload not working, docker react tutorial, setup docker on windows\" class=\"wp-image-543\" srcset=\"https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-14-edited-1.png 481w, https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-14-edited-1-225x300.png 225w, https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-14-edited-1-150x200.png 150w\" sizes=\"(max-width: 481px) 100vw, 481px\" \/><\/figure>\n\n\n\n<p>run the project image<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker run -v &lt;path to src folder>:\/app\/src -d -p 3000:3000 --name react-app react-image<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img width=\"1024\" height=\"41\" src=\"http:\/\/frontendguruji.com\/wp-content\/uploads\/2022\/06\/image-18-1024x41.png\" alt=\"create react app in docker container, docker hot reload react, docker hot reload not working, docker react tutorial, setup docker on windows\" class=\"wp-image-539\" srcset=\"https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-18-1024x41.png 1024w, https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-18-300x12.png 300w, https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-18-768x31.png 768w, https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-18-150x6.png 150w, https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-18.png 1070w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Now Hot Reloading will start working.&#x1f60e;<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h3 id=\"Hot-Reloading-not-working\">Docker Hot Reload not Working?<\/h3>\n\n\n\n<p>&#x1f525;To fix docker hot reload react not working in windows, pass below environment variable using -e flag <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">WATCHPACK_POLLING=true<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">docker run -e WATCHPACK_POLLING=true -v &lt;path to src folder>:\/app\/src -d -p 3000:3000 --name react-app react-image<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img width=\"1024\" height=\"36\" src=\"http:\/\/frontendguruji.com\/wp-content\/uploads\/2022\/06\/image-20-1024x36.png\" alt=\"create react app in docker container, docker hot reload react, docker hot reload not working, docker react tutorial, setup docker on windows\" class=\"wp-image-542\" srcset=\"https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-20-1024x36.png 1024w, https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-20-300x11.png 300w, https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-20-768x27.png 768w, https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-20-150x5.png 150w, https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2022\/06\/image-20.png 1306w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Tada!&#8230; we have completed create react app in docker container tutorial. Any query, please do ask in comments!<\/p>\n\n\n\n<p><strong><a href=\"https:\/\/github.com\/mandeeppasbola\/react-docker\" target=\"_blank\" rel=\"noreferrer noopener\">Checkout the code here.<\/a><\/strong><\/p>\n\n\n\n<p>Thanks &#x1f64f;<\/p>\n\n\n\n<p>Do you know <a href=\"http:\/\/frontendguruji.com\/how-to-setup-a-react-js-project-from-scratch-without-create-react-app\/\">how to setup a React project from Scratch without using create-react-app? Check this tutorial<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Create React App in Docker Container with Hot reloading in Windows machine.<\/p>\n","protected":false},"author":1,"featured_media":546,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-global-header-display":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":""},"categories":[4],"tags":[29,84,13],"_links":{"self":[{"href":"https:\/\/frontendguruji.com\/blog\/wp-json\/wp\/v2\/posts\/520"}],"collection":[{"href":"https:\/\/frontendguruji.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/frontendguruji.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/frontendguruji.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/frontendguruji.com\/blog\/wp-json\/wp\/v2\/comments?post=520"}],"version-history":[{"count":0,"href":"https:\/\/frontendguruji.com\/blog\/wp-json\/wp\/v2\/posts\/520\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/frontendguruji.com\/blog\/wp-json\/wp\/v2\/media\/546"}],"wp:attachment":[{"href":"https:\/\/frontendguruji.com\/blog\/wp-json\/wp\/v2\/media?parent=520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/frontendguruji.com\/blog\/wp-json\/wp\/v2\/categories?post=520"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/frontendguruji.com\/blog\/wp-json\/wp\/v2\/tags?post=520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}