How to run multiple instances of an image on different ports?

Asked

Viewed 299 times

2

I have an image docker door 8080. To execute it I use the following command:

docker run -d -p 8080:8080 -e DB_URL="127.0.0.1:BANCO" -e DB_USERNAME="usr" -e DB_PASSWORD="XXXXX" custom-repo.com.br/teste:1-0-0

I’d like to do the same thing but raise, hypothetically, 5 containers, each with a different external door pointing to it. For example:

  • Port 8080 accesses the container 1;
  • Port 8081 accesses the container 2;
  • Port 8082 accesses the container 3;
  • Port 8083 accesses the container 4;
  • Port 8084 accesses the container 5;

Is there any way to accomplish this without defining one by one?

  • Needs to be at different doors? Why?

  • @Andersoncarloswoss because I need to make simultaneous calls. I even thought of using a nginx but the problem is that the authenticated user on one server cannot request on another (using JSESSIONID)

  • 1

    Docker Compose has the option --scale that allows you to generate multiple instances of your image. Does it not solve your problem?

  • @Andersoncarloswoss if generate and I can access without losing this session then I believe it solves yes

1 answer

3

Good morning Sorack,

In this case, simply change your code to porta you want to use, for example

docker run -d -p 8080:8080 -e DB_URL=“127.0.0.1:BANCO” -e DB_USERNAME=“usr” -e DB_PASSWORD=“XXXXX” custom-repo.com.br/teste:1-0-0
docker run -d -p 8081:8080 -e DB_URL=“127.0.0.1:BANCO” -e DB_USERNAME=“usr” -e DB_PASSWORD=“XXXXX” custom-repo.com.br/teste:1-0-0
docker run -d -p 8082:8080 -e DB_URL=“127.0.0.1:BANCO” -e DB_USERNAME=“usr” -e DB_PASSWORD=“XXXXX” custom-repo.com.br/teste:1-0-0

And so on with the other doors.

If you want to understand a little more about exposing portas, read a little about the parameter -p or --port in that link

And if you want the browser to view everything in the conventional HTTP way, you need to put the reverse proxy in front.

  • But so I’m going to be setting up port 8081 for the 8081, for example. And that’s not what I want

  • I think the container door will always be 8080 xD

  • Just one errata on my command, use 8081:8080, 8082:8080

Browser other questions tagged

You are not signed in. Login or sign up in order to post.