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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Chaos Engineering and Kafka In Action: Topic Level Idempotence and Retry Check
  • Deploying Small Language Models on Sovereign Cloud
  • How to Keep AI Agents From Going Rogue Using Docker Sandbox
  • When One Docker Swarm Replica Became Unreachable Over an Overlay Network: A Production Case With Traefik

Trending

  • Modernizing a Legacy Java Application With OpenRewrite: What We Automated and What We Didn't
  • Agent Harness, Memory Indexing, and AI Governance in Test Automation
  • Two AI Agents, One Database: Solving Concurrent Updates With Optimistic Locking
  • You Do Not Need a PhD to Become an AI Engineer: The Essential Skill Stack

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Jan. 14, 21 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
7.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • Chaos Engineering and Kafka In Action: Topic Level Idempotence and Retry Check
  • Deploying Small Language Models on Sovereign Cloud
  • How to Keep AI Agents From Going Rogue Using Docker Sandbox
  • When One Docker Swarm Replica Became Unreachable Over an Overlay Network: A Production Case With Traefik

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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