I'm creating a service in which django containers are spawned on demand for users to test backend-functionality online.
I would like to make the spawned instances available to users on main domain:
Example: User spawns container userapp
which exposes port 8000
, it should be accessible on my domain as mydomain.net/userapp/
I do not know the amount of instances running, or their names in advance.
I found a nginx-proxy container here which dynamically creates configs for nginx to serve containers to subdomains:
$ docker run -e VIRTUAL_HOST=userapp.mydomain.com ...
I would like to have it accessible on the path. How can I create dynamic proxy paths with nginx or django?
I figured this out with django-http-proxy.
I can inherit from HttpProxy
and create a DynProxyView
:
views.py
from httpproxy.views import HttpProxy
class DynProxyView(HttpProxy):
def get_object(self):
return Fiddle.objects.get(pk=self.kwargs['pk'])
rewrite = True
@property
def base_url(self):
url= self.request.scheme+"://localhost:" + str(self.get_object().port)
print url
return url
def get_full_url(self, url):
result = super(DynProxyView, self).get_full_url(url)
return result[:-1] # To get rid of a pesky redundant slash
urls.py
...
url(r'^(?P<pk>[-\w]+)/result/(?P<url>.*)$', DynProxyView.as_view(),name='result'),
...
models.py
class Fiddle(models.Model):
name = models.CharField(max_length=20, unique=True)
hash = models.CharField(max_length=32, null=True, blank=True)
port = models.IntegerField(null=True, blank=True)
This way I can get the desired effect.