1

I have a docker container that contains a server.

I want to spin up multiple instances of this container that are all listening on a different port. I know I can issue multiple docker run commands which works fine for small numbers, but I'm wondering if there's an easier way to do this?

AndyReifman
  • 1,459
  • 4
  • 18
  • 34

2 Answers2

0

You can use a while loop to achieve this.

i=0; while [ $i -lt no_of_times ]; do docker run -d -p 80 <image_id>; sleep 1; ((i++)); done

Looking forward to an even better solution.

Got this one-liner from here

Thilak
  • 935
  • 8
  • 12
0

I have done this using docker-compose scale scale functionality.

Basically defining a single service and scaling it.

For instance, defining a docker-compose.yml:

version: '3.7'

services:
  your-service:
    image: "your-image"
  ports:
    - "<port to expose>"

Then when you want to start several instances execute:

docker-compose up --scale your-service=X

You could also take a look to docker swarm mode, which allows you to define services using command line

Gonzalo Matheu
  • 8,984
  • 5
  • 35
  • 58