动态服务django码头集装箱 [英] Dynamically serving django docker containers

查看:119
本文介绍了动态服务django码头集装箱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个服务,其中 django 容器是根据需要产生的,用户需要测试后端功能在线。

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:

示例:用户生成容器 userapp ,它暴露了端口 8000 ,它应该可以在我的域上以 mydomain的方式访问。 net / userapp /

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.

在这里找到一个nginx-proxy 容器动态创建 nginx 的配置,以将容器运送到子域:

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 ...

$ docker run -e VIRTUAL_HOST=userapp.mydomain.com ...

我想要它可以在路径上访问。如何使用nginx或django创建动态代理路径?

I would like to have it accessible on the path. How can I create dynamic proxy paths with nginx or django?

推荐答案

我认为这是与 django-http-proxy

我可以继承自 HttpProxy 并创建一个 DynProxyView

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.

这篇关于动态服务django码头集装箱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆