DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Smart Deployment Strategies for Modern Applications
  • Solving the Mystery: Why Java RSS Grows in Docker on M1 Macs
  • How We Diagnosed a Hidden Scheduler Failure in a Docker Swarm Cluster Serving 2 Million Users
  • Java Backend Development in the Era of Kubernetes and Docker

Trending

  • Ujorm3: A New Lightweight ORM for JavaBeans and Records
  • Multi-Scale Feature Learning in CNN and U-Net Architectures
  • Building a Skill-Based Agentic Reviewer with Claude Code: A Practical Guide Using Skills.MD, MCP Servers, Tools, and Tasks
  • A Deep Dive into Tracing Agentic Workflows (Part 1)
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. How to Scale Your Services With Docker During Development

How to Scale Your Services With Docker During Development

In this article, we're going to see how we can leverage Docker to scale and spin up multiple instances of a service on our machine.

By 
David Guida user avatar
David Guida
·
Jan. 14, 21 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
7.6K Views

Join the DZone community and get the full member experience.

Join For Free

Hi all! Today we’re going to see how we can leverage Docker to scale and spin up multiple instances of a service on our machine.

It’s a matter of fact: live and dev boxes are not the same. Code working on your machine is likely to not work when deployed. In some cases, this happens because you’re running a single instance of service when developing. But maybe that service is instantiated multiple times once deployed.

So unless we have very good instrumentation or we start going down the remote debugging path (when possible), the only option is to replicate the services also on our machine.

Luckily, we can use Docker to do the grunt work for us!

The first thing to do, if you’re not doing that already, is to build a Dockerfile for our service. If you’re using Visual Studio (and you’re lazy like me), you can have it generate the Dockerfile automatically.

Visual studio can do that either when creating a new project or later on.

The next step is to create a nice docker-compose config file. Something like this should work:

YAML
 




x


 
1
version: '3.8'
2

          
3
services: 
4
  api:
5
    image: ${DOCKER_REGISTRY}myservice:latest   
6
    build:
7
      context: .
8
      dockerfile: MyService/Dockerfile  



Here we’re defining one single service named “api”, starting from our image “myservice”.

We will save this config in the root folder, along with the .sln file:

MyService screenshot.

If we run the command docker-compose up, we should see the service starting up.

Now, to scale horizontally, we’ll need to set up a Docker Swarm. It will allow us to host and manage a cluster of Docker Engines and spin up multiple instances of our service.

But first, we’ll have to host the image on a registry. We could pick any registry we like or have one on our machine:

YAML
 




x


 
1
docker run -d -p 5000:5000 --name registry registry:latest docker start registry



This will start the registry on localhost:5000 .

Now we have to tag and push our service’s image to this registry:

YAML
 




xxxxxxxxxx
1


 
1
docker image tag my-image localhost:5000/myservice docker push localhost:5000/myservice



The next step is to build the docker-compose config and push it to the repo. If you recall, we used a DOCKER_REGISTRY environment variable when we defined the service image. We can set it to localhost:5000/ and then run:

YAML
 




xxxxxxxxxx
1


 
1
docker-compose build docker-compose push



We’re getting close. Now we have to create the swarm by running docker swarm init and then we can deploy our service as a Docker Stack:

YAML
 




xxxxxxxxxx
1


 
1
docker stack deploy --compose-file docker-compose.yml myAwesomeSystem



Notice that we have to provide a name to the Stack. We might want to add multiple services to our docker-compose file later on (eg. databases, monitoring tools, and so on), so it’s better to use a different name.

By default, we still have a single instance though. We have to update the docker-compose config and add the replicas settings:

YAML
 




xxxxxxxxxx
1
11


 
1
version: '3.8'
2

          
3
services: 
4
  api:
5
    image: ${DOCKER_REGISTRY}myservice:latest      
6
    build:
7
      context: .
8
      dockerfile: MyService/Dockerfile
9
    deploy:
10
      mode: replicated
11
      replicas: 6



Save, and run the docker stack deploy command again.

We’re done! If you want to make sure the replicas are running, just execute docker stack services myAwesomeSystem.

When we’re done coding we can shut down the swarm and clean up all the dangling images and containers:

YAML
 




xxxxxxxxxx
1


 
1
docker swarm leave --force docker system prune --all --force



Happy swarming!

Docker (software)

Opinions expressed by DZone contributors are their own.

Related

  • Smart Deployment Strategies for Modern Applications
  • Solving the Mystery: Why Java RSS Grows in Docker on M1 Macs
  • How We Diagnosed a Hidden Scheduler Failure in a Docker Swarm Cluster Serving 2 Million Users
  • Java Backend Development in the Era of Kubernetes and Docker

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook