使用nginx管理docker容器

阅读更多
### 一直YY着想有一个基于web的docker管理器,市面上的都比较重,我只想要轻量一点的,于是想着自己做一个,直接部署在nginx里面,不占内存

先说说原理,其实很简单,docker提供了一套socket接口,基于unix套接字,只要实现nginx能通过socket访问docker基本就上就OK了。

于是想到了nginx + lua,由于服务器上有openresty,直接就拿来用了,通过lua-resty-http 模块,使用unix套接字访问docker的接口,没想到还真可以

代码很简单

docker.lua
```
local http = require "resty.http"

local _M = {}

-- 运行docker remote api,
function do_docker_cmd(uri, is_post)
    local httpc = http.new()
    local method = "GET"
    if is_post == true then
        method = "POST"
    end
    httpc:connect('unix:/var/run/docker.sock')
    local res, err = httpc:request({
        path = 'http://localhost' .. uri,
        method = method,
        headers = {
            ["Host"] = "localhost",
            ["Accept"] = "application/json",
            ["Accept-Encoding"] = "utf-8"
        }
    })
    if err then
       return nil, err
    end
    httpc:set_keepalive(60)
    if res.status == 500 then
        ngx.log(ngx.ALERT, res.body)
    end
    return res, nil
end

-- 获取所有容器
function _M.index()
    local resp, err = do_docker_cmd('/containers/json');
    if err then
        ngx.say(err);
        return
    end
    ngx.say(resp:read_body());
end

-- 停止容器
function _M.stop()
    local id = ngx.var.arg_id
    local resp, err = do_docker_cmd('/containers/' .. id .. '/stop', true);
    if err then
        ngx.say(err);
        return
    end
    ngx.say(resp:read_body());
end

-- 重启容器
function _M.restart()
    local id = ngx.var.arg_id
    local resp, err = do_docker_cmd('/containers/' .. id .. '/restart', true);
    if err then
        ngx.say(err);
        return
    end
    ngx.say(resp:read_body());
end
```

如果连不上,那可能是docker.sock文件没有权限,chmod 给一下权限就行了,或者把nginx 的用户改一下也行

剩下的就是写一个简单的页面 html + angular1,把容器都读出来,显示容器信息了,效果图如下:

![这里写图片描述](http://img.blog.csdn.net/20161227000547675?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXEzNjIyMjg0MTY=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)

我这里,只有两个操作,一个是停止 一个是重启,看日志功能没时间做

angularjs
```
function loadContainer() {
    $http.get('/app/docker').then(function (res) {
        $scope.containers = res.data
    })
}
$scope.stopContainer = function(item) {
    $http.get('/app/docker/stop', {params: {id: item.Id}}).then(function(res) {
        loadContainer();
    })
}
$scope.restartContainer = function(item) {
    $http.get('/app/docker/restart', {params: {id: item.Id}}).then(function(res) {
        loadContainer();
    })
}
$scope.showContainers = function() {
    var views = document.getElementsByClassName('ui-view');
    for (var i = 0; i < views.length; i++) {
        views[i].setAttribute('class', 'ng-hide ui-view');
    }
    document.getElementById('containers').setAttribute('class', 'ui-view')

    loadContainer();
}
```

html
```

           

               

                   

                       
容器列表

                       
                   

               


               
               

                   

                       

                       

                   

                   

                       
                           
                           
                               
                               
                               
                               
                               
                               
                               
                               
                           
                           
                           
                           
                                                            data-ng-repeat="item in containers" class="ng-scope">
                               
                               
                               
                               
                               
                               
                               
                               
                               
                           
                           
                           
                       
镜像名 容器名 创建时间 运行时间 状态 IP 暴露端口 操作
{{(item.Created * 1000) | date: 'yyyy-MM-dd HH:mm:ss'}}
                                   
                                    class="ng-isolate-scope">
                                    停止
                                        |
                                    重启
                                       
                                   

                               

                   


               

           

       

```

基本就是这个样子啦,不用登录服务器,就能管理docker容器了,然后简单的在nginx成配置一个 basic auth ,使用用户名密码登陆,成本很低,几乎没有什么内存消耗。是不是很轻量。

你可能感兴趣的:(nginx,lua,docker)