{"id":4135,"date":"2023-11-04T23:14:05","date_gmt":"2023-11-04T23:14:05","guid":{"rendered":"http:\/\/localhost:10003\/creating-scalable-microservices-with-docker\/"},"modified":"2023-11-05T05:47:58","modified_gmt":"2023-11-05T05:47:58","slug":"creating-scalable-microservices-with-docker","status":"publish","type":"post","link":"http:\/\/localhost:10003\/creating-scalable-microservices-with-docker\/","title":{"rendered":"Creating Scalable Microservices with Docker"},"content":{"rendered":"

Introduction<\/h1>\n

Scalability is a key characteristic in building modern applications. Microservices architecture is a popular approach in building scalable applications. It lets developers break down a monolithic application into small, loosely coupled services. Each service is responsible for a particular functionality and can be independently deployed, scaled and maintained.<\/p>\n

Docker is a containerization technology that simplifies the process of packaging, deploying and managing applications. Combining docker with microservices architecture can help developers build scalable applications that can be easily deployed, managed and scaled irrespective of the underlying infrastructure. In this tutorial, we will explore how to create scalable microservices with Docker.<\/p>\n

Prerequisites<\/h1>\n

To follow along with this tutorial, you will need the following:
\n– Docker installed on your system.
\n– Basic knowledge of docker commands and microservices architecture.<\/p>\n

Steps<\/h1>\n

1. Define the services<\/h2>\n

The first step in building a microservices application is to identify the services that make up the application. Each service should have a specific functionality. For this example, we will build a simple e-commerce application consisting of the following services:
\n– Product service: responsible for managing products.
\n– Order service: responsible for managing orders.
\n– Payment service: responsible for managing payments.<\/p>\n

2. Build the services<\/h2>\n

Once we have identified the services, we can start building each service as a separate microservice. For each service, we need to write the code for the functionality it provides, define an interface (API) that other services can use to interact with it and package it as a docker image.<\/p>\n

Let’s take an example of building the product service. We can define the API for the product service using the OpenAPI standard.<\/p>\n

openapi: \"3.0.0\"\ninfo:\n  title: Product API\n  version: \"1.0.0\"\npaths:\n  \/product:\n    get:\n      summary: Get all products\n      responses:\n        '200':\n          description: A list of products.\n          content:\n            application\/json:\n              schema:\n                type: array\n                items:\n                  $ref: '#\/components\/schemas\/Product'\n    post:\n      summary: Create a new product.\n      responses:\n        '201':\n          description: The created product.\n          content:\n            application\/json:\n              schema:\n                $ref: '#\/components\/schemas\/Product'\ncomponents:\n  schemas:\n    Product:\n      type: object\n      required:\n        - name\n        - price\n      properties:\n        id:\n          type: string\n          format: uuid\n          description: The unique identifier for the product.\n        name:\n          type: string\n          description: The name of the product.\n        description:\n          type: string\n          description: A description of the product.\n        price:\n          type: number\n          format: double\n          minimum: 0\n          description: The price of the product.\n<\/code><\/pre>\n

We can use this API definition to generate the code for the product service using a code generator like Swagger codegen. Once we have the code, we can package it as a docker image. We can define a Dockerfile for the product service as follows:<\/p>\n

FROM openjdk:11-jre-slim\n\nWORKDIR \/app\n\n# Copy the service code into the container\nCOPY target\/product-service-1.0-SNAPSHOT.jar .\n\n# Set the service command\nCMD [\"java\", \"-jar\", \"product-service-1.0-SNAPSHOT.jar\"]\n<\/code><\/pre>\n

We can use the maven build tool to build the product service and package it as a docker image.<\/p>\n

mvn clean package docker:build -DskipTests\n<\/code><\/pre>\n

We can repeat these steps to build the order and payment services and package them as docker images.<\/p>\n

3. Define the infrastructure<\/h2>\n

To deploy and manage the microservices, we need to define the infrastructure for the application. There are several ways to do this. One way is to use docker-compose. Docker-compose is a tool for defining and running multi-container Docker applications.<\/p>\n

We can define the infrastructure for our e-commerce application in a docker-compose file. Here’s an example docker-compose file:<\/p>\n

version: '3.7'\nservices:\n  product-service:\n    image: <product-service-image>\n    ports:\n      - \"8080:8080\"\n    environment:\n      SPRING_DATASOURCE_URL: jdbc:mysql:\/\/mysql\/product?useSSL=false\n      SPRING_DATASOURCE_USERNAME: root\n      SPRING_DATASOURCE_PASSWORD: password\n    depends_on:\n      - mysql\n  order-service:\n    image: <order-service-image>\n    ports:\n      - \"8081:8081\"\n    environment:\n      PRODUCT_SERVICE_URL: http:\/\/product-service:8080\n      SPRING_DATASOURCE_URL: jdbc:mysql:\/\/mysql\/order?useSSL=false\n      SPRING_DATASOURCE_USERNAME: root\n      SPRING_DATASOURCE_PASSWORD: password\n    depends_on:\n      - product-service\n      - mysql\n  payment-service:\n    image: <payment-service-image>\n    ports:\n      - \"8082:8082\"\n    environment:\n      ORDER_SERVICE_URL: http:\/\/order-service:8081\n      SPRING_DATASOURCE_URL: jdbc:mysql:\/\/mysql\/payment?useSSL=false\n      SPRING_DATASOURCE_USERNAME: root\n      SPRING_DATASOURCE_PASSWORD: password\n    depends_on:\n      - order-service\n      - mysql\n  mysql:\n    image: mysql:5.7\n    environment:\n      MYSQL_DATABASE: product\n      MYSQL_ROOT_PASSWORD: password\n    volumes:\n      - db-data:\/var\/lib\/mysql\nvolumes:\n  db-data:\n<\/code><\/pre>\n

In the above docker-compose file, we have defined the services for the product, order and payment microservices. We have also defined the mysql service as a dependency for the other services. The environment variables for the services are also defined. The mysql service uses a named volume to persist the data.<\/p>\n

4. Deploy the services<\/h2>\n

Once we have defined the services and infrastructure, we can deploy the services using docker-compose.<\/p>\n

docker-compose up -d\n<\/code><\/pre>\n

This command will start all the services defined in the docker-compose file in detached mode. We can check the status of the services using the docker-compose ps command.<\/p>\n

docker-compose ps\n<\/code><\/pre>\n

5. Scale the services<\/h2>\n

One of the benefits of using docker and microservices architecture is the ability to scale the services horizontally. We can scale the services by increasing the number of instances of a service.<\/p>\n

For example, if we want to scale the product service to three instances, we can use the docker-compose scale command like this:<\/p>\n

docker-compose scale product-service=3\n<\/code><\/pre>\n

6. Monitor the services<\/h2>\n

Monitoring the microservices is essential to ensure the application is running smoothly. We can use several tools to monitor the services. One such tool is Prometheus.<\/p>\n

Prometheus is a monitoring and alerting system. It collects metrics from the services and stores them in a time-series database. It provides a query language to extract and aggregate the metrics. We can use Prometheus to monitor the services in our application.<\/p>\n

To use Prometheus, we need to add the Prometheus client library to each service. We can add the following dependency to each service:<\/p>\n

<dependency>\n    <groupId>io.prometheus<\/groupId>\n    <artifactId>simpleclient_spring_boot<\/artifactId>\n    <version>0.10.0<\/version>\n<\/dependency>\n<\/code><\/pre>\n

We also need to add the following configuration to each service’s application.properties file:<\/p>\n

management.endpoint.metrics.enabled=true\nmanagement.metrics.export.prometheus.enabled=true\n<\/code><\/pre>\n

Once we have added the Prometheus client library and configuration, we can start the services and navigate to the \/actuator\/prometheus endpoint of each service to verify that Prometheus is collecting metrics.<\/p>\n

We can also set up a Prometheus server to collect and display the metrics. We can define a docker-compose file for the Prometheus server as follows:<\/p>\n

version: '3.7'\nservices:\n  prometheus:\n    image: prom\/prometheus:v2.28.1\n    volumes:\n      - .\/prometheus.yml:\/etc\/prometheus\/prometheus.yml\n    ports:\n      - \"9090:9090\"\n<\/code><\/pre>\n

In the above docker-compose file, we have defined the prometheus service and mounted a configuration file that specifies the targets to scrape. The Prometheus server will scrape the metrics from the services defined as targets in the configuration file.<\/p>\n

7. Conclusion<\/h2>\n

In this tutorial, we have explored how to build scalable microservices with Docker. We have defined the services, built the services as docker images, defined the infrastructure using docker-compose and deployed the services. We have also explored how to scale the services and monitor them using Prometheus.<\/p>\n

Using Docker and microservices architecture can help developers build scalable, easily deployable and manageable applications. With tools like docker-compose and Prometheus, managing the infrastructure and monitoring the services can be simplified.<\/p>\n","protected":false},"excerpt":{"rendered":"

Introduction Scalability is a key characteristic in building modern applications. Microservices architecture is a popular approach in building scalable applications. It lets developers break down a monolithic application into small, loosely coupled services. Each service is responsible for a particular functionality and can be independently deployed, scaled and maintained. Docker Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[621,199,1425,96,1433,569,1394,1431,1432,1430],"yoast_head":"\nCreating Scalable Microservices with Docker - Pantherax Blogs<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/localhost:10003\/creating-scalable-microservices-with-docker\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Creating Scalable Microservices with Docker\" \/>\n<meta property=\"og:description\" content=\"Introduction Scalability is a key characteristic in building modern applications. Microservices architecture is a popular approach in building scalable applications. It lets developers break down a monolithic application into small, loosely coupled services. Each service is responsible for a particular functionality and can be independently deployed, scaled and maintained. Docker Continue Reading\" \/>\n<meta property=\"og:url\" content=\"http:\/\/localhost:10003\/creating-scalable-microservices-with-docker\/\" \/>\n<meta property=\"og:site_name\" content=\"Pantherax Blogs\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-04T23:14:05+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-05T05:47:58+00:00\" \/>\n<meta name=\"author\" content=\"Panther\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Panther\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\n\t \"@context\": \"https:\/\/schema.org\",\n\t \"@graph\": [\n\t {\n\t \"@type\": \"Article\",\n\t \"@id\": \"http:\/\/localhost:10003\/creating-scalable-microservices-with-docker\/#article\",\n\t \"isPartOf\": {\n\t \"@id\": \"http:\/\/localhost:10003\/creating-scalable-microservices-with-docker\/\"\n\t },\n\t \"author\": {\n\t \"name\": \"Panther\",\n\t \"@id\": \"http:\/\/localhost:10003\/#\/schema\/person\/b63d816f4964b163e53cbbcffaa0f3d7\"\n\t },\n\t \"headline\": \"Creating Scalable Microservices with Docker\",\n\t \"datePublished\": \"2023-11-04T23:14:05+00:00\",\n\t \"dateModified\": \"2023-11-05T05:47:58+00:00\",\n\t \"mainEntityOfPage\": {\n\t \"@id\": \"http:\/\/localhost:10003\/creating-scalable-microservices-with-docker\/\"\n\t },\n\t \"wordCount\": 856,\n\t \"publisher\": {\n\t \"@id\": \"http:\/\/localhost:10003\/#organization\"\n\t },\n\t \"keywords\": [\n\t \"\\\"cloud-native applications\\\"]\",\n\t \"\\\"containerization\\\"\",\n\t \"\\\"DevOps practices\\\"\",\n\t \"\\\"distributed computing\\\"\",\n\t \"\\\"Docker images\\\"]\",\n\t \"\\\"Docker\\\"\",\n\t \"\\\"microservices architecture\\\"\",\n\t \"\\\"Scalable application development\\\"\",\n\t \"\\\"Service-oriented architecture\\\"\",\n\t \"[\\\"Creating Scalable Microservices\\\"\"\n\t ],\n\t \"inLanguage\": \"en-US\"\n\t },\n\t {\n\t \"@type\": \"WebPage\",\n\t \"@id\": \"http:\/\/localhost:10003\/creating-scalable-microservices-with-docker\/\",\n\t \"url\": \"http:\/\/localhost:10003\/creating-scalable-microservices-with-docker\/\",\n\t \"name\": \"Creating Scalable Microservices with Docker - Pantherax Blogs\",\n\t \"isPartOf\": {\n\t \"@id\": \"http:\/\/localhost:10003\/#website\"\n\t },\n\t \"datePublished\": \"2023-11-04T23:14:05+00:00\",\n\t \"dateModified\": \"2023-11-05T05:47:58+00:00\",\n\t \"breadcrumb\": {\n\t \"@id\": \"http:\/\/localhost:10003\/creating-scalable-microservices-with-docker\/#breadcrumb\"\n\t },\n\t \"inLanguage\": \"en-US\",\n\t \"potentialAction\": [\n\t {\n\t \"@type\": \"ReadAction\",\n\t \"target\": [\n\t \"http:\/\/localhost:10003\/creating-scalable-microservices-with-docker\/\"\n\t ]\n\t }\n\t ]\n\t },\n\t {\n\t \"@type\": \"BreadcrumbList\",\n\t \"@id\": \"http:\/\/localhost:10003\/creating-scalable-microservices-with-docker\/#breadcrumb\",\n\t \"itemListElement\": [\n\t {\n\t \"@type\": \"ListItem\",\n\t \"position\": 1,\n\t \"name\": \"Home\",\n\t \"item\": \"http:\/\/localhost:10003\/\"\n\t },\n\t {\n\t \"@type\": \"ListItem\",\n\t \"position\": 2,\n\t \"name\": \"Creating Scalable Microservices with Docker\"\n\t }\n\t ]\n\t },\n\t {\n\t \"@type\": \"WebSite\",\n\t \"@id\": \"http:\/\/localhost:10003\/#website\",\n\t \"url\": \"http:\/\/localhost:10003\/\",\n\t \"name\": \"Pantherax Blogs\",\n\t \"description\": \"\",\n\t \"publisher\": {\n\t \"@id\": \"http:\/\/localhost:10003\/#organization\"\n\t },\n\t \"potentialAction\": [\n\t {\n\t \"@type\": \"SearchAction\",\n\t \"target\": {\n\t \"@type\": \"EntryPoint\",\n\t \"urlTemplate\": \"http:\/\/localhost:10003\/?s={search_term_string}\"\n\t },\n\t \"query-input\": \"required name=search_term_string\"\n\t }\n\t ],\n\t \"inLanguage\": \"en-US\"\n\t },\n\t {\n\t \"@type\": \"Organization\",\n\t \"@id\": \"http:\/\/localhost:10003\/#organization\",\n\t \"name\": \"Pantherax Blogs\",\n\t \"url\": \"http:\/\/localhost:10003\/\",\n\t \"logo\": {\n\t \"@type\": \"ImageObject\",\n\t \"inLanguage\": \"en-US\",\n\t \"@id\": \"http:\/\/localhost:10003\/#\/schema\/logo\/image\/\",\n\t \"url\": \"http:\/\/localhost:10003\/wp-content\/uploads\/2023\/11\/cropped-9e7721cb-2d62-4f72-ab7f-7d1d8db89226.jpeg\",\n\t \"contentUrl\": \"http:\/\/localhost:10003\/wp-content\/uploads\/2023\/11\/cropped-9e7721cb-2d62-4f72-ab7f-7d1d8db89226.jpeg\",\n\t \"width\": 1024,\n\t \"height\": 1024,\n\t \"caption\": \"Pantherax Blogs\"\n\t },\n\t \"image\": {\n\t \"@id\": \"http:\/\/localhost:10003\/#\/schema\/logo\/image\/\"\n\t }\n\t },\n\t {\n\t \"@type\": \"Person\",\n\t \"@id\": \"http:\/\/localhost:10003\/#\/schema\/person\/b63d816f4964b163e53cbbcffaa0f3d7\",\n\t \"name\": \"Panther\",\n\t \"image\": {\n\t \"@type\": \"ImageObject\",\n\t \"inLanguage\": \"en-US\",\n\t \"@id\": \"http:\/\/localhost:10003\/#\/schema\/person\/image\/\",\n\t \"url\": \"http:\/\/2.gravatar.com\/avatar\/b8c0eda5a49f8f31ec32d0a0f9d6f838?s=96&d=mm&r=g\",\n\t \"contentUrl\": \"http:\/\/2.gravatar.com\/avatar\/b8c0eda5a49f8f31ec32d0a0f9d6f838?s=96&d=mm&r=g\",\n\t \"caption\": \"Panther\"\n\t },\n\t \"sameAs\": [\n\t \"http:\/\/localhost:10003\"\n\t ],\n\t \"url\": \"http:\/\/localhost:10003\/author\/pepethefrog\/\"\n\t }\n\t ]\n\t}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Creating Scalable Microservices with Docker - Pantherax Blogs","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":"http:\/\/localhost:10003\/creating-scalable-microservices-with-docker\/","og_locale":"en_US","og_type":"article","og_title":"Creating Scalable Microservices with Docker","og_description":"Introduction Scalability is a key characteristic in building modern applications. Microservices architecture is a popular approach in building scalable applications. It lets developers break down a monolithic application into small, loosely coupled services. Each service is responsible for a particular functionality and can be independently deployed, scaled and maintained. Docker Continue Reading","og_url":"http:\/\/localhost:10003\/creating-scalable-microservices-with-docker\/","og_site_name":"Pantherax Blogs","article_published_time":"2023-11-04T23:14:05+00:00","article_modified_time":"2023-11-05T05:47:58+00:00","author":"Panther","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Panther","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/localhost:10003\/creating-scalable-microservices-with-docker\/#article","isPartOf":{"@id":"http:\/\/localhost:10003\/creating-scalable-microservices-with-docker\/"},"author":{"name":"Panther","@id":"http:\/\/localhost:10003\/#\/schema\/person\/b63d816f4964b163e53cbbcffaa0f3d7"},"headline":"Creating Scalable Microservices with Docker","datePublished":"2023-11-04T23:14:05+00:00","dateModified":"2023-11-05T05:47:58+00:00","mainEntityOfPage":{"@id":"http:\/\/localhost:10003\/creating-scalable-microservices-with-docker\/"},"wordCount":856,"publisher":{"@id":"http:\/\/localhost:10003\/#organization"},"keywords":["\"cloud-native applications\"]","\"containerization\"","\"DevOps practices\"","\"distributed computing\"","\"Docker images\"]","\"Docker\"","\"microservices architecture\"","\"Scalable application development\"","\"Service-oriented architecture\"","[\"Creating Scalable Microservices\""],"inLanguage":"en-US"},{"@type":"WebPage","@id":"http:\/\/localhost:10003\/creating-scalable-microservices-with-docker\/","url":"http:\/\/localhost:10003\/creating-scalable-microservices-with-docker\/","name":"Creating Scalable Microservices with Docker - Pantherax Blogs","isPartOf":{"@id":"http:\/\/localhost:10003\/#website"},"datePublished":"2023-11-04T23:14:05+00:00","dateModified":"2023-11-05T05:47:58+00:00","breadcrumb":{"@id":"http:\/\/localhost:10003\/creating-scalable-microservices-with-docker\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/localhost:10003\/creating-scalable-microservices-with-docker\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/localhost:10003\/creating-scalable-microservices-with-docker\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/localhost:10003\/"},{"@type":"ListItem","position":2,"name":"Creating Scalable Microservices with Docker"}]},{"@type":"WebSite","@id":"http:\/\/localhost:10003\/#website","url":"http:\/\/localhost:10003\/","name":"Pantherax Blogs","description":"","publisher":{"@id":"http:\/\/localhost:10003\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/localhost:10003\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/localhost:10003\/#organization","name":"Pantherax Blogs","url":"http:\/\/localhost:10003\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/localhost:10003\/#\/schema\/logo\/image\/","url":"http:\/\/localhost:10003\/wp-content\/uploads\/2023\/11\/cropped-9e7721cb-2d62-4f72-ab7f-7d1d8db89226.jpeg","contentUrl":"http:\/\/localhost:10003\/wp-content\/uploads\/2023\/11\/cropped-9e7721cb-2d62-4f72-ab7f-7d1d8db89226.jpeg","width":1024,"height":1024,"caption":"Pantherax Blogs"},"image":{"@id":"http:\/\/localhost:10003\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"http:\/\/localhost:10003\/#\/schema\/person\/b63d816f4964b163e53cbbcffaa0f3d7","name":"Panther","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/localhost:10003\/#\/schema\/person\/image\/","url":"http:\/\/2.gravatar.com\/avatar\/b8c0eda5a49f8f31ec32d0a0f9d6f838?s=96&d=mm&r=g","contentUrl":"http:\/\/2.gravatar.com\/avatar\/b8c0eda5a49f8f31ec32d0a0f9d6f838?s=96&d=mm&r=g","caption":"Panther"},"sameAs":["http:\/\/localhost:10003"],"url":"http:\/\/localhost:10003\/author\/pepethefrog\/"}]}},"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/posts\/4135"}],"collection":[{"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/comments?post=4135"}],"version-history":[{"count":1,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/posts\/4135\/revisions"}],"predecessor-version":[{"id":4391,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/posts\/4135\/revisions\/4391"}],"wp:attachment":[{"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/media?parent=4135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/categories?post=4135"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/tags?post=4135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}