{"id":2965,"date":"2018-10-11T07:00:16","date_gmt":"2018-10-11T01:30:16","guid":{"rendered":"http:\/\/www.upnxtblog.com\/?p=2965"},"modified":"2020-04-23T12:06:50","modified_gmt":"2020-04-23T06:36:50","slug":"how-to-scale-services-using-docker-compose","status":"publish","type":"post","link":"https:\/\/www.upnxtblog.com\/index.php\/2018\/10\/11\/how-to-scale-services-using-docker-compose\/","title":{"rendered":"How to scale services using Docker Compose"},"content":{"rendered":"<div class='booster-block booster-read-block'><\/div><p><a href=\"https:\/\/docs.docker.com\/compose\/\" target=\"_blank\" rel=\"noopener\"><strong>Docker Compose<\/strong><\/a>\u00a0tool is used to define and start running multi-container Docker applications.\u00a0Configuration is as easy,there would be YAML file to configure your application\u2019s services\/networks\/volumes etc., Then, with a single command, you can create and start all the services from the compose configuration.<\/p>\n<p>Here are the key steps :<\/p>\n<ol>\n<li>Define\u00a0<code class=\"highlighter-rouge\">Dockerfile<\/code>\u00a0for your app\u2019s environment.<\/li>\n<li>Define\u00a0<code class=\"highlighter-rouge\">docker-compose.yml<\/code>\u00a0for the services that make up your app services.<\/li>\n<li>Run\u00a0<code class=\"highlighter-rouge\">docker-compose up<\/code>\u00a0and Compose starts and runs your entire app.<\/li>\n<\/ol>\n<p>This quickstart assumes basic understanding of Docker\u00a0<a href=\"http:\/\/www.upnxtblog.com\/index.php\/2018\/09\/12\/how-to-install-docker-on-ubuntu\/\">concepts<\/a>, please refer to earlier posts for understanding on Docker &amp; how to <a href=\"http:\/\/www.upnxtblog.com\/index.php\/2018\/09\/12\/how-to-install-docker-on-ubuntu\/\">install<\/a> and containerize <a href=\"http:\/\/www.upnxtblog.com\/index.php\/2018\/09\/19\/docker-tutorial-build-docker-image-for-your-angular-6-application\/\">applications<\/a>. In this post, we can look at how to use existing Docker compose file and scale services.<\/p>\n<p>A\u00a0<code class=\"highlighter-rouge\">docker-compose.yml<\/code>\u00a0looks like this:<\/p>\n<pre>version: \"2\"\r\n services:\r\nredis-master: \r\nimage: redis:latest \r\nports:\r\n- \"6379\"\r\nredis-slave: \r\nimage: gcr.io\/google_samples\/gb-redisslave:v1 \r\nports: \r\n- \"6379\" \r\nenvironment: <code>- GET_HOSTS_FROM=dns\r\nfrontend: \r\nimage: gcr.io\/google-samples\/gb-frontend:v3 \r\nports: \r\n- \"80:80\" \r\nenvironment: \r\n- GET_HOSTS_FROM=dns<\/code><\/pre>\n<h2>Key Features of Docker Compose<\/h2>\n<ul>\n<li>When you define the Compose file, <strong>you can use <em>project name<\/em> to isolate environments from each other<\/strong>. This could be useful in cases like creating multiple copies of a single environment or segregate the builds by a unique build number.<\/li>\n<li><strong>Compose caches the configuration used to create a container.<\/strong> When you restart a service that has not changed, Compose re-uses the existing containers.It\u00a0means that you can make changes to your environment very quickly.<\/li>\n<li>Compose <strong>preserves all volumes used by your services.<\/strong> When\u00a0<code class=\"highlighter-rouge\">docker-compose up<\/code>\u00a0runs, if it finds any containers from previous runs, it copies the volumes from the old container to the new container. This process ensures that any data you\u2019ve created in volumes isn\u2019t lost.<\/li>\n<li>Compose<strong> supports variables in the Compose file.<\/strong> You can use these variables to customize your composition for different environments or different users.<\/li>\n<\/ul>\n<p>For more information on Docker Compose, check out <a href=\"https:\/\/docs.docker.com\/compose\/gettingstarted\/\" target=\"_blank\" rel=\"noopener\">here<\/a>. You can also refer previous posts on Docker <a href=\"http:\/\/www.upnxtblog.com\/index.php\/2017\/11\/29\/docker-tutorial-build-docker-image-for-your-java-application\/\">here<\/a> &amp; <a href=\"http:\/\/www.upnxtblog.com\/index.php\/2018\/02\/02\/build-docker-images-using-jenkins\/\">here<\/a>.<\/p>\n<p>In the next section, we can look at how to use existing Docker compose file and scale services.<\/p>\n<h2>Scale service instances<\/h2>\n<p>Each service defined in Docker compose configuration can be scaled using below command<\/p>\n<p><code>docker-compose scale &lt;service name&gt; = &lt;no of instances&gt;<\/code><\/p>\n<p>For example :<\/p>\n<p><code>docker-compose scale redis-master=3<\/code><\/p>\n<p>Although services can be scaled but you could get an arbitrary range of ports assigned since the ports are randomly assigned by the Docker engine. This can be controlled by <strong>assigning port range on the ports section of the compose YAML file.<\/strong><\/p>\n<pre>services: \r\n\r\nredis-master: \r\n\r\nimage: redis:latest \r\n\r\n<strong>ports: - \"6379-6385:6379\"<\/strong><\/pre>\n<p>Scaling can also be done by using <code>up\u00a0<\/code>command as well with the\u00a0<code class=\"highlighter-rouge\">--scale<\/code>\u00a0flag.<\/p>\n<p><code>docker-compose up --scale redis-master=3 -d<\/code><\/p>\n<p>Alternatively, in\u00a0Compose file version 3.x, you can also specify\u00a0replicas\u00a0under the\u00a0deploy section\u00a0as part of a service configuration for\u00a0Swarm mode.<\/p>\n<p><em>Congrats! today we have learned how to scale services using scale command.<\/em><\/p>\n<p><em><strong>Like this post? Don\u2019t forget to share it!<\/strong><\/em><\/p>\n<h2>Additional Resources:<\/h2>\n<ul>\n<li><a href=\"https:\/\/docs.docker.com\/reference\/builder\/\" target=\"_blank\" rel=\"noopener\">Official documentation<\/a> as a reference to understand any command.<\/li>\n<li><a title=\"Docker build reference\" href=\"https:\/\/docs.docker.com\/engine\/reference\/commandline\/build\/\" target=\"_blank\" rel=\"noopener noreferrer\">Docker build reference<\/a><\/li>\n<li><a title=\"Docker run reference\" href=\"https:\/\/docs.docker.com\/engine\/reference\/run\/\" target=\"_blank\" rel=\"noopener noreferrer\">Docker run reference<\/a><\/li>\n<li><a href=\"https:\/\/www.upnxtblog.com\/index.php\/2018\/01\/17\/top-6-gui-tools-for-managing-docker-environments\/\">TOP 6 GUI tools for managing Docker environments<\/a><\/li>\n<li><a href=\"https:\/\/www.upnxtblog.com\/index.php\/2017\/11\/29\/docker-tutorial-build-docker-image-for-your-java-application\/\">Docker tutorial \u2013 Build Docker image for your Java application<\/a><\/li>\n<li><a href=\"https:\/\/www.upnxtblog.com\/index.php\/2019\/11\/25\/using-docker-application-packages-to-deliver-apps-across-teams\/\">Using Docker Application Packages to Deliver Apps across Teams<\/a><\/li>\n<li><a href=\"https:\/\/docs.docker.com\/articles\/dockerfile_best-practices\/\" target=\"_blank\" rel=\"noopener\">Best Practices<\/a> article on writing Docker files.<\/li>\n<li><a href=\"https:\/\/docs.docker.com\/\" target=\"_blank\" rel=\"noopener\">Test<\/a> your knowledge on Dockerfile.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Docker Compose\u00a0tool is used to define and start running multi-container Docker applications.\u00a0Configuration is as easy,there would be YAML file to configure your application\u2019s services\/networks\/volumes etc., Then, with a single command, you can create and start all the services from the compose configuration. Here are the key steps : Define\u00a0Dockerfile\u00a0for your app\u2019s environment. Define\u00a0docker-compose.yml\u00a0for the services [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1061,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[310,146,37],"tags":[295,53,296],"class_list":["post-2965","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-docker","category-cloud","category-how-to-guides","tag-compose","tag-docker","tag-scale"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/docker_facebook_share.png?fit=336%2C287&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9fbQS-LP","jetpack-related-posts":[{"id":3535,"url":"https:\/\/www.upnxtblog.com\/index.php\/2019\/02\/21\/how-to-build-and-run-your-app-with-compose\/","url_meta":{"origin":2965,"position":0},"title":"How to Build and run your app with Compose","author":"Karthik","date":"February 21, 2019","format":false,"excerpt":"Docker Compose tool is used to define and start running multi-container Docker applications. Configuration is as easy, there would be a YAML file to configure your application\u2019s services\/networks\/volumes, etc., Then, with a single command, you can create and start all the services from the compose configuration. Here are the key\u2026","rel":"","context":"In &quot;Docker Guides&quot;","block_context":{"text":"Docker Guides","link":"https:\/\/www.upnxtblog.com\/index.php\/category\/docker\/"},"img":{"alt_text":"Start containers using docker-compose up command","src":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2019\/01\/compose1.jpg?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2019\/01\/compose1.jpg?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2019\/01\/compose1.jpg?resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2019\/01\/compose1.jpg?resize=700%2C400 2x"},"classes":[]},{"id":3879,"url":"https:\/\/www.upnxtblog.com\/index.php\/2019\/06\/03\/how-do-i-connect-to-postgresql-running-on-host-from-docker-container\/","url_meta":{"origin":2965,"position":1},"title":"How do I connect to Postgresql running on host from Docker container","author":"Karthik","date":"June 3, 2019","format":false,"excerpt":"There could be instances where you want to connect to Postgresql database on the host from your containers. In this post, we take look at configuration steps on how to connect to Postgresql running on host from your Docker containers. We are going to use Docker Compose tool to define\u2026","rel":"","context":"In &quot;Docker Guides&quot;","block_context":{"text":"Docker Guides","link":"https:\/\/www.upnxtblog.com\/index.php\/category\/docker\/"},"img":{"alt_text":"listen_addresses ","src":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2019\/05\/Dock2.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2019\/05\/Dock2.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2019\/05\/Dock2.png?resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2019\/05\/Dock2.png?resize=700%2C400 2x"},"classes":[]},{"id":2597,"url":"https:\/\/www.upnxtblog.com\/index.php\/2018\/08\/16\/kubernetes-tutorial-learn-how-to-use-kompose\/","url_meta":{"origin":2965,"position":2},"title":"Kubernetes Tutorial : Learn how to use Kompose","author":"Karthik","date":"August 16, 2018","format":false,"excerpt":"kompose is basically a deployment accelerator tool to help users who are familiar with\u00a0docker-compose\u00a0format and move to\u00a0Kubernetes.In this post, we are going to take a sample Docker compose file and convert it using kompose utility. This quickstart assumes a basic understanding of Kubernetes concepts, please refer earlier posts for understanding\u2026","rel":"","context":"In &quot;Kubernetes Guides&quot;","block_context":{"text":"Kubernetes Guides","link":"https:\/\/www.upnxtblog.com\/index.php\/category\/kubernetes\/"},"img":{"alt_text":"kubernetes logo","src":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/kubernetes.jpg?fit=722%2C612&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/kubernetes.jpg?fit=722%2C612&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/kubernetes.jpg?fit=722%2C612&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/kubernetes.jpg?fit=722%2C612&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":3523,"url":"https:\/\/www.upnxtblog.com\/index.php\/2019\/02\/07\/docker-tutorial-build-docker-image-for-your-database-application\/","url_meta":{"origin":2965,"position":3},"title":"Docker tutorial &#8211; Build Docker image for your Database application","author":"Karthik","date":"February 7, 2019","format":false,"excerpt":"Docker as we know, is an open platform for developers and sysadmins to build, ship, and run distributed applications, whether on laptops, data center VMs, or the cloud. In this post, we are going to take look at how to build a Docker image for Database application. Step #1. Setup\u2026","rel":"","context":"In &quot;Docker Guides&quot;","block_context":{"text":"Docker Guides","link":"https:\/\/www.upnxtblog.com\/index.php\/category\/docker\/"},"img":{"alt_text":"Validate Docker Installation","src":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2018\/09\/dock14.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2018\/09\/dock14.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2018\/09\/dock14.png?resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2018\/09\/dock14.png?resize=700%2C400 2x"},"classes":[]},{"id":3840,"url":"https:\/\/www.upnxtblog.com\/index.php\/2019\/11\/25\/using-docker-application-packages-to-deliver-apps-across-teams\/","url_meta":{"origin":2965,"position":4},"title":"Using Docker Application Packages to Deliver Apps across Teams","author":"Karthik","date":"November 25, 2019","format":false,"excerpt":"Cloud native applications typically use different technologies, each with their own packaging formats. For example, If you\u2019re working on Microsoft Azure then it would be ARM templates, or if its Kubernetes, then its Helm charts, or if it\u2019s on AWS then it would be CloudFormation and so on. Distributed applications\u2026","rel":"","context":"In &quot;Docker Guides&quot;","block_context":{"text":"Docker Guides","link":"https:\/\/www.upnxtblog.com\/index.php\/category\/docker\/"},"img":{"alt_text":"Install Application Package","src":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2019\/11\/dockerapp22.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2019\/11\/dockerapp22.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2019\/11\/dockerapp22.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2019\/11\/dockerapp22.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2019\/11\/dockerapp22.png?resize=1050%2C600&ssl=1 3x"},"classes":[]},{"id":2929,"url":"https:\/\/www.upnxtblog.com\/index.php\/2018\/09\/19\/docker-tutorial-build-docker-image-for-your-angular-6-application\/","url_meta":{"origin":2965,"position":5},"title":"Docker tutorial &#8211; Build Docker image for your Angular 6 application","author":"Karthik","date":"September 19, 2018","format":false,"excerpt":"Docker as we know, is an open platform for developers and sysadmins to build, ship, and run distributed applications, whether on laptops, data center VMs, or the cloud. In this post, we are going to take look at how to build a Docker image for Angular application (typically the steps\u2026","rel":"","context":"In &quot;Docker Guides&quot;","block_context":{"text":"Docker Guides","link":"https:\/\/www.upnxtblog.com\/index.php\/category\/docker\/"},"img":{"alt_text":"Validate Docker Installation","src":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2018\/09\/dock14.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2018\/09\/dock14.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2018\/09\/dock14.png?resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2018\/09\/dock14.png?resize=700%2C400 2x"},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.upnxtblog.com\/index.php\/wp-json\/wp\/v2\/posts\/2965","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.upnxtblog.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.upnxtblog.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.upnxtblog.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.upnxtblog.com\/index.php\/wp-json\/wp\/v2\/comments?post=2965"}],"version-history":[{"count":13,"href":"https:\/\/www.upnxtblog.com\/index.php\/wp-json\/wp\/v2\/posts\/2965\/revisions"}],"predecessor-version":[{"id":5407,"href":"https:\/\/www.upnxtblog.com\/index.php\/wp-json\/wp\/v2\/posts\/2965\/revisions\/5407"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.upnxtblog.com\/index.php\/wp-json\/wp\/v2\/media\/1061"}],"wp:attachment":[{"href":"https:\/\/www.upnxtblog.com\/index.php\/wp-json\/wp\/v2\/media?parent=2965"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.upnxtblog.com\/index.php\/wp-json\/wp\/v2\/categories?post=2965"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.upnxtblog.com\/index.php\/wp-json\/wp\/v2\/tags?post=2965"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}