Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker-Compose running multiple instances of one image in different ports

I have a python flask application and I want to have multiple instances of it at the same server that each container have own output port (or DNS) and dependencies. I used docker-compose and it works well. I saw scale arg but I think it is good for something like load balancing not having a different version of an application. Another solution that came up to my mind is having multiple services for one app and run each instance by its name in the terminal.

like image 290
Artin Falahi Avatar asked Oct 27 '18 14:10

Artin Falahi


1 Answers

The docker-compose scale command is deprecated and the docs suggest you use docker-compose up --scale SERVICE=NUM. But you must be careful when specifying ports for scaling. If you try to put your containers on port 80 five times, four of the five will fail - the port would be already occupied. But there is a solution.

I just tested this approach and it worked. Here's the docker-compose.yml I tested:

version: '3.7'
services:
  test_app:
    image: <my_custom_image>
    ports:
      - 10000-10003:80

Then I ran it with docker-compose up --scale test_app=4 and it deployed 4 instances of test_app on four different (but specified beforehand) ports: 10000, 10001, 10002 and 10003.

I hope I answered your question.

like image 80
Alex Karshin Avatar answered Oct 05 '22 02:10

Alex Karshin