0

I have two problem Statement for running the docker container.

  1. Run two instances of a docker container.

I am running a container say test-service. When I execute the command Only one container named as test-service get created. I want to change the command and create one more instance of the container.

  1. Dynamic Port number Allocation to the container.

I am binding the port say 8080:8080. I want to configure it in such a way that the port number will be dynamic.

The command which I am using to run the container is as below:

docker run -p ${EXTERNAL_PORT_NUMBER}:${INTERNAL_PORT_NUMBER} --network ${NETWORK} --name ${SERVICE_NAME} --restart always -m 1024M --memory-swap -1 -itd ${ORGANISATION}/${SERVICE_NAME}:${VERSION}

The test-service is a node service.

Please let me know what modifications are needed in the above command.

Anand Deshmukh
  • 1,086
  • 2
  • 17
  • 35

1 Answers1

0

For 2nd question:

Use --publish-all flag instead of providing port mapping.

$ docker run --publish-all --network ${NETWORK} --name ${SERVICE_NAME} --restart always -m 1024M --memory-swap -1 -itd ${ORGANISATION}/${SERVICE_NAME}:${VERSION}

This will publish a container's port(s) to the host port. Host port will be dynamic. And all container ports will be exposed.

But if you want to expose specific port, use -p :<port>

$ docker run -p :${INTERNAL_PORT_NUMBER} --network ${NETWORK} --name ${SERVICE_NAME} --restart always -m 1024M --memory-swap -1 -itd ${ORGANISATION}/${SERVICE_NAME}:${VERSION}

Leave ${EXTERNAL_PORT_NUMBER} part empty, then a port will be selected dynamically.

For 1st question:

You can use docker-compose. Check this answer.

Or, you can run docker run multiple time. In this case, use different --name

Shahriar
  • 13,460
  • 8
  • 78
  • 95