Best practice for redirecting between docker containers

Given that I have multiple web applications running in docker containers, I want the user to be able to redirect from a service to another service in their browser. I wonder how to achieve this - especially if I want my applications to be portable from one docker host to another host.

Let's say we have a ServiceA that redirects a user to ServiceB. So we have a relationship

ServiceA --> ServiceB

One approach would be to statically assign ports and hostnames and set them as environments for my web services - which I wouldn't prefer, because I don't want to care about which service is running on which port.

The second approach would be to have a proxy like nginx and bind the services and use the proxy host and port. But this will require changing the proxy configuration when moving the service to another host.

The third approach that comes to mind is to use etcd and ambassadors to register and resolve services. Therefore ServiceA will use ServiceB-Ambassador, which looks for ServiceB in etcd. This results in many docker containers simply connecting between services.

Which direction would you prefer? Or are there different approaches?

Edit

The real problem is injecting the uri of ServiceB into ServiceA, so I can start my ServiceA with a type argument -DserviceB.uri=<serviceUri>

so that serviceA can build the correct redirect header.

+3


source to share


1 answer


I am using a consul setup to connect tomcat containers to apache http server (using mod_jk). Consul is similar to etcd, i.e. Allows you to register and discover services. This may apply to your question, but you are not limited to the consul.

Every time a new tomcat container starts, I assign a separate public port to that container, register the tomcat container in the consul with information about its IPs and ports, and fire the event (the script is executed on the docker host, and minified for readability)

#!/bin/bash
INTERNAL_PORT=8009
source ~/ports.properties
TOMCAT_PORT=$(( TOMCAT_PORT + 1))
echo "TOMCAT_PORT=$TOMCAT_PORT" > ~/ports.properties

CONTAINER_ID=$(docker run -d -p $TOMCAT_PORT:8009 -v `pwd`$WAR_DIR:/webapps rossbachp/tomcat8)
echo "Container started, CONTAINER_ID:  $CONTAINER_ID"

IP_ADDRESS=$(docker inspect -f '{{.NetworkSettings.IPAddress}}' $CONTAINER_ID )
echo "Container IP_ADDRESS:               $IP_ADDRESS "

echo "Register Container in Consul"
curl -X PUT -d '{"ID": "'$CONTAINER_ID'","Name":"'$CLUSTER_NAME'", "Tags": [ "IP_'$IP_ADDRESS'", "PORT_'$INTERNAL_PORT'"],"Port":'$TOMCAT_PORT'}' localhost:8500/v1/agent/service/register 

echo "Fire Event"
consul event -name "TomcatServiceUp"

      

In the consul (on the docker host) I have defined the clock for the "TomcatServiceUp" event in the /etc/consul.d/bootstrap/watchTomcatServiceUp.json file which executes the script



{
"watches":[    {
"type":"event",
"name":"TomcatServiceUp",
"handler": "/home/dude/docker/docker-http-withmodjk/callbackTomcatServiceUpEvent.sh"
   }  ]
}

      

and script callbackTomcatServiceUpEvent.sh requests services (mainly IPAddress and port), creates a new employee.properties file, copies that file to the http docker instance (its volumes) and gracefully restarts the http server (to the docker container).

#!/bin/bash
SERVICE=$(curl localhost:8500/v1/agent/services)
java -jar /home/dude/docker/JSonParser.jar "$SERVICE" >> /tmp/workers.properties
cp workers.properties /home/dude/docker/docker-http-withmodjk/mod_jk_conf
# http graceful restart

      

Could you please take an approach where your services register with the consul (or etcd) and discover each other via events and service lookup. Or use nginx to handle events and find services?

+1


source







All Articles