1
  • I have Node services which are running in Docker container
    • I am using shell script to run these services
    • I want to run three different instances of the same service on 3 different port. say 9011 9022 9033
    • I also want it to configure it in such a way that after every new deployment it should stop the previous service and remove it
    • I am using docker rm test-service to remove it but it will remove other instances too.
    • by this approach only once instance can be running.

Is there any way to remove Docker service running on the specific port.

here is my shell script

#!/bin/bash
ORGANISATION="$1"
SERVICE_NAME="$2"
VERSION="$3"
ENVIRONMENT="$4"
INTERNAL_PORT_NUMBER="$5"
EXTERNAL_PORT_NUMBER="$6"
NETWORK="$7"
docker build -t ${ORGANISATION}/${SERVICE_NAME}:${VERSION} --build-arg PORT=${INTERNAL_PORT_NUMBER} --build-arg ENVIRONMENT=${ENVIRONMENT} --no-cache .
docker stop ${SERVICE_NAME}
docker rm ${SERVICE_NAME}

sudo npm install
sudo npm install -g express
docker run -p ${EXTERNAL_PORT_NUMBER}:${INTERNAL_PORT_NUMBER} --network ${NETWORK} --name ${SERVICE_NAME} --restart always -itd ${ORGANISATION}/${SERVICE_NAME}:${VERSION}
  • Can you explain more about your use case? This sounds pretty confusing / unnecessary atm. Also the script is running containers, not services? Can you paste your exact commands? – johnharris85 Jun 18 '17 at 03:55
  • `docker stop ${SERVICE_NAME} docker rm ${SERVICE_NAME}` these are the commands. but it will also remove the other instance of a service as a reason I want to know is there any another way of removing the container on the basis of port –  Jun 18 '17 at 04:12
  • But you're not running services. Services are something very specific in Docker. What you're running is containers, and you can't run more than 1 container with the same name, so you can't actually be doing what you say you're doing anyway. Please paste the entire set of commands you're running, with `docker ps` output after each one. – johnharris85 Jun 18 '17 at 04:33
  • I am running the shell only.From the shell file it will run the container.Please see the commands in shell file which I posted above. –  Jun 18 '17 at 04:52
  • As you mention I can not run more than one container with the same name. Can I run the docker service with the same name on 3 different port. if yes what modifications do i need to make in above shell fle –  Jun 18 '17 at 04:57
  • You simply can't have two containers using the same name. It does not matter which ports they use. Give them different names. – Henry Jun 18 '17 at 05:40
  • @Henry : Correct, So what can I do. I want to perform LoadBalance for service. So is there replica or something that I can do. I dont want to change the name of the service as I am calling this service using service name –  Jun 18 '17 at 06:13
  • @AnandDeshmukh You should read about docker services in swarm mode: https://docs.docker.com/engine/swarm/how-swarm-mode-works/services/ – Henry Jun 18 '17 at 06:18

1 Answers1

0

I can not run more than one container with the same name. Can I run the docker service with the same name on 3 different port. if yes what modifications do i need to make in above shell file?

That would be three docker run, each using the same internal port, but mapped to a different host port, with three different names

docker run -p ${EXTERNAL_PORT_NUMBER1}:${INTERNAL_PORT_NUMBER} --name ${SERVICE_NAME1}
docker run -p ${EXTERNAL_PORT_NUMBER2}:${INTERNAL_PORT_NUMBER} --name ${SERVICE_NAME2} 
docker run -p ${EXTERNAL_PORT_NUMBER3}:${INTERNAL_PORT_NUMBER}  --name ${SERVICE_NAME3}

I want to perform LoadBalance for service

See docker swarm mode

The swarm manager uses ingress load balancing to expose the services you want to make available externally to the swarm.
The swarm manager can automatically assign the service a PublishedPort or you can configure a PublishedPort for the service. You can specify any unused port. If you do not specify a port, the swarm manager assigns the service a port in the 30000-32767 range.

Example:

the following command publishes port 80 in the nginx container to port 8080 for any node in the swarm

$ docker service create \
  --name my-web \
  --publish 8080:80 \
  --replicas 2 \
  nginx
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks for the reply but its not possible to run the container by same name –  Jun 18 '17 at 06:14
  • @AnandDeshmukh no: https://stackoverflow.com/a/31698016/6309. Each container has its own id and own name. – VonC Jun 18 '17 at 06:18
  • Yes, Every docker container has its own ID but the name should be different When I try to implement I get an error saying container already exists with the same name. I don't want to change the name As I am calling this service using name –  Jun 18 '17 at 06:32