I want to run 3 instances of docker image which runs on some port. I want the ports of all the 3 instances exposed to the host. I want to be able to control which of the host ports is mapped to the container.
First I tried creating 3 different services in docker-compose.yml. This worked but isn't preferable.
Second I create a single service in docker-compose.yml Then I tried to control the exposed port via enviroment variables. But when I create the second container, docker removes the first container I created.
I cannot used the scale
option because it maps the exposed ports to random port on host.
myservice:
image: myimage:latest
container_name: service-${PORT}
volumes:
- ${DIR}:/data
ports:
- "${PORT:-8011}:8011"
- "${ADMINPORT:-8012}:8012"
you can set the ports range to use with scale
option of compose
:
ports: - "8011-8013:8011"
start it:
docker-compose up --scale myservice=3
then the containers will use port 8011
, 8012
and 8013
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With