{"id":314129,"date":"2021-08-03T12:00:00","date_gmt":"2021-08-03T09:00:00","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-run-nginx-inside-docker-for-easy-auto-scaling-cloudsavvy-it\/"},"modified":"2021-08-03T12:00:00","modified_gmt":"2021-08-03T09:00:00","slug":"how-to-run-nginx-inside-docker-for-easy-auto-scaling-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-run-nginx-inside-docker-for-easy-auto-scaling-cloudsavvy-it\/","title":{"rendered":"#How to Run NGINX Inside Docker (for Easy Auto-Scaling) \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a40d4274d12a\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a40d4274d12a\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-run-nginx-inside-docker-for-easy-auto-scaling-cloudsavvy-it\/#Setting_Up_NGINX_Inside_Docker\" >Setting Up NGINX Inside Docker<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Run NGINX Inside Docker (for Easy Auto-Scaling) \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<p><img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage imgchk9 alignnone wp-image-503 size-full\" srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/06\/c454d054.png?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/06\/c454d054.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/06\/c454d054.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Docker logo\" width=\"700\" height=\"300\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>One of the most common workloads of Docker is using it to containerize web servers like NGINX and Apache to run a high-performance content delivery fleet that can be easily auto-scaled and managed. We\u2019ll show you how to set it up with NGINX.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Setting_Up_NGINX_Inside_Docker\"><\/span>Setting Up NGINX Inside Docker<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Docker is a containerization platform, used to package up your <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>lication and all of its code into one easily manageable container image. The process of doing this is pretty similar to how you\u2019d go about setting up a new server\u2014the container is a blank slate, so you\u2019ll need to install dependencies, build your code, copy over the build artifacts, and copy over any configuration. Luckily, you don\u2019t have to automate that much. NGINX already has a <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/hub.docker.com\/_\/nginx\">publicly available Docker container<\/a>, which you can use as the starting point for your application.<\/p>\n<p>Of course, depending on the application you\u2019re containerizing, this can be a bit more involved. If you\u2019re deploying a CMS like WordPress, you\u2019ll likely need to have an external database, as containers aren\u2019t designed to be persistent. A good place to start for WordPress, in particular, would be <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/hub.docker.com\/_\/wordpress\/\">WordPress\u2019s Docker container<\/a>.<\/p>\n<p>For the purposes of having something a little more involved than a simple Hello World webpage, we\u2019ll create a new project directory and initialize a basic Vue.js application. Your configuration will be different depending on the content you\u2019re serving, but the <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/general\/\" data-internallinksmanager029f6b8e52c=\"3\" title=\"General\" target=\"_blank\" rel=\"noopener\">general<\/a> idea is the same.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5168\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2020\/06\/cbeade24.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"project template\" width=\"662\" height=\"443\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>At the root of your project, create a new file simply named <code>Dockerfile<\/code>\u00a0with no extension. This will act as the build configuration. By default, the container is empty, and only includes the applications and dependencies that come installed with the base image. You will need to copy over your application\u2019s code; if you\u2019re just serving static content, this is easy, but if you\u2019re working with server-side applications like WordPress, you might need to install additional dependencies.<\/p>\n<p>The following config is pretty basic. Because this is a node application, we need to run <code>npm run build<\/code>\u00a0to get a distribution-ready build. We can handle this all in the Dockerfile, by setting up a two-part container build:<\/p>\n<pre>FROM node:latest as build-stage&#13;\nWORKDIR \/src&#13;\nCOPY package*.json .\/&#13;\nRUN npm install&#13;\nCOPY .\/ .&#13;\nRUN npm run build&#13;\n&#13;\nFROM nginx as production-stage&#13;\nRUN mkdir \/src&#13;\nCOPY --from=build-stage \/src\/dist \/src&#13;\nCOPY nginx.conf \/etc\/nginx\/nginx.conf<\/pre>\n<p>The first line\u2019s\u00a0<code>FROM<\/code>\u00a0command pulls the <code>node<\/code>\u00a0container from Docker Hub and makes a new container called <code>build-stage<\/code>. The next <code>cd<\/code>\u2018s to that directory, and copies over the <code>package.json<\/code>. Then, it runs <code>npm install<\/code>, then copies over the app\u2019s code and starts the build process. If your application needs to be built from source, you\u2019ll want to do something similar to this.<\/p>\n<p>The next state pulls the <code>nginx<\/code>\u00a0container to serve as the production deployment. It makes the <code>src<\/code>\u00a0directory and then copies, from the <code>build-stage<\/code>\u00a0container, the <code>\/src\/dist\/<\/code>\u00a0folder containing the build artifacts, over to the <code>\/src<\/code>\u00a0folder of the production container. It then copies over an NGINX config file.<\/p>\n<p>You\u2019ll also want to make a new file called <code>.dockerignore<\/code>, to tell it to ignore <code>node_modules<\/code>\u00a0as well as any build artifacts from local builds.<\/p>\n<pre>**\/node_modules&#13;\n**\/dist<\/pre>\n<p>The Dockerfile references an <code>nginx.conf<\/code>, which you\u2019ll also need to create. If you\u2019re running a more complex configuration with multiple configs in <code>\/sites-available<\/code>, you might want to create a new folder for your NGINX configuration, and copy that over.<\/p>\n<pre>user  nginx;&#13;\nworker_processes  1;&#13;\nerror_log  \/var\/log\/nginx\/error.log warn;&#13;\npid        \/var\/run\/nginx.pid;&#13;\nevents {&#13;\n  worker_connections  1024;&#13;\n}&#13;\nhttp {&#13;\n  include       \/etc\/nginx\/mime.types;&#13;\n  default_type  application\/octet-stream;&#13;\n  log_format  main  '$remote_addr - $remote_user [$time_local] \"$request\" '&#13;\n                    '$status $body_bytes_sent \"$http_referer\" '&#13;\n                    '\"$http_user_agent\" \"$http_x_forwarded_for\"';&#13;\n  access_log  \/var\/log\/nginx\/access.log  main;&#13;\n  sendfile        on;&#13;\n  keepalive_timeout  65;&#13;\n  server {&#13;\n    listen       80;&#13;\n    server_name  localhost;&#13;\n    location \/ {&#13;\n      root   \/src;&#13;\n      index  index.html;&#13;\n      try_files $uri $uri\/ \/index.html;&#13;\n    }&#13;\n    error_page   500 502 503 504  \/50x.html;&#13;\n    location = \/50x.html {&#13;\n      root   \/usr\/share\/nginx\/html;&#13;\n    }&#13;\n  }&#13;\n}<\/pre>\n<p>This is just an HTTP web server. The simplest way to set up HTTPS would be to run LetsEncrypt\u2019s certbot locally, and copy over the certificate from <code>\/etc\/letsencrypt\/live\/example.com\/fullchain.pem<\/code>\u00a0into the production container. These certs are valid for 90 days, so you\u2019ll need to renew them regularly. You can <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/github.com\/staticfloat\/docker-nginx-certbot\">automate this as part of the container build process<\/a>.<\/p>\n<p>Once everything is in order, you can run the Docker build:<\/p>\n<pre>docker build . -t my-app<\/pre>\n<p>This will build the container as <code>my-app<\/code>, after which you\u2019re free to tag it and send it off to ECS or a container registry for eventual deployment. You should, of course, test it locally first with <code>docker run<\/code>\u00a0binding <code>localhost:8080<\/code>\u00a0to port 80 of the NGINX instance:<\/p>\n<pre>docker run -d -p 8080:80 my-app<\/pre>\n<p>Once you have a built image, deploying it in production is fairly simple. You can read our guide to setting up an <span class=\"htgeditornotesel\">auto-scaling container deployment on AWS ECS<\/span> to learn more, or read our guide on <span class=\"htgeditornotesel\">setting up a CI\/CD pipeline<\/span> with containers to handle automated builds and deployments.<\/p>\n<p><strong>RELATED:<\/strong> <strong><em>What Is a PEM File and How Do You Use It?<\/em><\/strong><\/p>\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/5167\/how-to-run-nginx-inside-docker-for-easy-auto-scaling\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Run NGINX Inside Docker (for Easy Auto-Scaling) \u2013 CloudSavvy IT&#8221; One of the most common workloads of Docker is using it to containerize web servers like NGINX and Apache to run a high-performance content delivery fleet that can be easily auto-scaled and managed. We\u2019ll show you how to set it up with NGINX&#8230;.<\/p>\n","protected":false},"author":1,"featured_media":314130,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/06\/c454d054.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-314129","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/314129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=314129"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/314129\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/314130"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=314129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=314129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=314129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}