欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

Docker+DockerCompose封装web应用的方法步骤

程序员文章站 2022-03-25 10:58:05
目录nginx 配置dockerfilelua 实现基于企业微信的网关认证使用 dockercompose 进行容器编排这篇文章会介绍如何将后端、前端和网关通通使用 docker 容器进行运行,并最终...

这篇文章会介绍如何将后端、前端和网关通通使用 docker 容器进行运行,并最终使用 dockercompose 进行容器编排。

技术栈

前端

  • react
  • ant design

后端

  • go
  • iris

网关

  • nginx
  • openresty
  • lua
  • 企业微信

后端构建 api

这里虽然我们写了 expose 4182,这个只用在测试的时候,生产环境实际上我们不会将后端接口端口进行暴露,
而是通过容器间的网络进行互相访问,以及最终会使用 nginx 进行转发。

from golang:1.15.5

label maintainer="k8scat <k8scat@gmail.com>"

expose 4182

env goproxy=https://goproxy.cn,direct \
    go111module=on

workdir /go/src/github.com/k8scat/containerized-app/api

copy . .

run go mod download && \
go build -o api main.go && \
chmod +x api

entrypoint [ "./api" ]

前端构建 web

这里值得一提的是,因为前端肯定会去调用后端接口,而且这个接口地址是根据部署而改变,
所以这里我们使用了 arg 指令进行设置后端的接口地址,这样我们只需要在构建镜像的时候传入 --build-arg react_app_base_url=https://example.com/api 就可以调整后端接口地址了,而不是去改动代码。

还有一点,有朋友肯定会发现这里同时使用到了 entrypoint 和 cmd,这是为了可以在运行的时候调整前端的端口,但实际上我们这里没必要去调整,因为这里最终也是用 nginx 进行转发。

from node:lts

label maintainer="k8scat <k8scat@gmail.com>"

workdir /web

copy . .

arg react_app_base_url

run npm config set registry https://registry.npm.taobao.org && \
npm install && \
npm run build && \
npm install -g serve

entrypoint [ "serve", "-s", "build" ]
cmd [ "-l", "3214" ]

网关构建 gateway

nginx 配置

这里我们就分别设置了后端和前端的上游,然后设置 location 规则进行转发。
这里有几个点可以说一下:

  • 通过 set_by_lua 获取容器的环境变量,最终在运行的时候通过设置 environment 设置这些环境变量,更加灵活
  • server_name 使用到了 $hostname,运行时需要设置容器的 hostname
  • ssl_certificate 和 ssl_certificate_key 不能使用变量设置
  • 加载 gateway.lua 脚本实现企业微信的网关认证
upstream web {
    server ca-web:3214;
}

upstream api {
 server ca-api:4182;
}

server {
 set_by_lua $corp_id 'return os.getenv("corp_id")';
 set_by_lua $agent_id 'return os.getenv("agent_id")';
 set_by_lua $secret 'return os.getenv("secret")';
 set_by_lua $callback_host 'return os.getenv("callback_host")';
 set_by_lua $callback_schema 'return os.getenv("callback_schema")';
 set_by_lua $callback_uri 'return os.getenv("callback_uri")';
 set_by_lua $logout_uri 'return os.getenv("logout_uri")';
 set_by_lua $token_expires 'return os.getenv("token_expires")';
 set_by_lua $use_secure_cookie 'return os.getenv("use_secure_cookie")';

 listen 443 ssl http2;
 server_name $hostname;
 resolver 8.8.8.8;
 ssl_certificate /certs/cert.crt;
 ssl_certificate_key /certs/cert.key;
 ssl_session_cache shared:ssl:1m;
 ssl_session_timeout 5m;
 ssl_protocols tlsv1 tlsv1.1 tlsv1.2;
 ssl_ciphers aesgcm:high:!anull:!md5;
 ssl_prefer_server_ciphers on;
 lua_ssl_verify_depth 2;
    lua_ssl_trusted_certificate /etc/pki/tls/certs/ca-bundle.crt;

 if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})t(\d{2})") {
  set $year $1;
  set $month $2;
  set $day $3;
 }
 access_log logs/access_$year$month$day.log main;
 error_log logs/error.log;

 access_by_lua_file "/usr/local/openresty/nginx/conf/gateway.lua";

 location ^~ /gateway {
        root   html;
        index  index.html index.htm;
    }

 location ^~ /api {
        proxy_pass http://api;
        proxy_read_timeout 3600;
        proxy_http_version 1.1;
        proxy_set_header x_forwarded_proto https;
        proxy_set_header x-real-ip $remote_addr;
        proxy_set_header x-forwarded-for $remote_addr;
        proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
        proxy_set_header host $host;
        proxy_set_header connection "";
    }

 location ^~ / {
        proxy_pass http://web;
        proxy_read_timeout 3600;
        proxy_http_version 1.1;
        proxy_set_header x_forwarded_proto https;
        proxy_set_header x-real-ip $remote_addr;
        proxy_set_header x-forwarded-for $remote_addr;
        proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
        proxy_set_header host $host;
        proxy_set_header connection "";
    }

 error_page 500 502 503 504 /50x.html;
 location = /50x.html {
  root html;
 }
}

server {
 listen 80;
 server_name $hostname;

 location / {
  rewrite ^/(.*) https://$server_name/$1 redirect;
 }
}

dockerfile

from openresty/openresty:1.19.3.1-centos

label maintainer="k8scat <k8scat@gmail.com>"

copy gateway.conf /etc/nginx/conf.d/gateway.conf
copy gateway.lua /usr/local/openresty/nginx/conf/gateway.lua
copy nginx.conf /usr/local/openresty/nginx/conf/nginx.conf

# install lua-resty-http
run /usr/local/openresty/luajit/bin/luarocks install lua-resty-http

lua 实现基于企业微信的网关认证

这里面的一些配置参数都是通过获取 nginx 设置的变量。

local json = require("cjson")
local http = require("resty.http")

local uri = ngx.var.uri
local uri_args = ngx.req.get_uri_args()
local scheme = ngx.var.scheme

local corp_id = ngx.var.corp_id
local agent_id = ngx.var.agent_id
local secret = ngx.var.secret
local callback_scheme = ngx.var.callback_scheme or scheme
local callback_host = ngx.var.callback_host
local callback_uri = ngx.var.callback_uri
local use_secure_cookie = ngx.var.use_secure_cookie == "true" or false
local callback_url = callback_scheme .. "://" .. callback_host .. callback_uri
local redirect_url = callback_scheme .. "://" .. callback_host .. ngx.var.request_uri
local logout_uri = ngx.var.logout_uri or "/logout"
local token_expires = ngx.var.token_expires or "7200"
token_expires = tonumber(token_expires)

local function request_access_token(code)
    local request = http.new()
    request:set_timeout(7000)
    local res, err = request:request_uri("https://qyapi.weixin.qq.com/cgi-bin/gettoken", {
        method = "get",
        query = {
            corpid = corp_id,
            corpsecret = secret,
        },
        ssl_verify = true,
    })
    if not res then
        return nil, (err or "access token request failed: " .. (err or "unknown reason"))
    end
    if res.status ~= 200 then
        return nil, "received " .. res.status .. " from https://qyapi.weixin.qq.com/cgi-bin/gettoken: " .. res.body
    end
    local data = json.decode(res.body)
    if data["errcode"] ~= 0 then
        return nil, data["errmsg"]
    else
        return data["access_token"]
    end
end

local function request_user(access_token, code)
    local request = http.new()
    request:set_timeout(7000)
    local res, err = request:request_uri("https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo", {
        method = "get",
        query = {
            access_token = access_token,
            code = code,
        },
        ssl_verify = true,
    })
    if not res then
        return nil, "get profile request failed: " .. (err or "unknown reason")
    end
    if res.status ~= 200 then
        return nil, "received " .. res.status .. " from https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo"
    end
    local userinfo = json.decode(res.body)
    if userinfo["errcode"] == 0 then
        if userinfo["userid"] then
            res, err = request:request_uri("https://qyapi.weixin.qq.com/cgi-bin/user/get", {
                method = "get",
                query = {
                    access_token = access_token,
                    userid = userinfo["userid"],
                },
                ssl_verify = true,
            })
            if not res then
                return nil, "get user request failed: " .. (err or "unknown reason")
            end
            if res.status ~= 200 then
                return nil, "received " .. res.status .. " from https://qyapi.weixin.qq.com/cgi-bin/user/get"
            end
            local user = json.decode(res.body)
            if user["errcode"] == 0 then
                return user
            else
                return nil, user["errmsg"]
            end
        else
            return nil, "userid not exists"
        end
    else
        return nil, userinfo["errmsg"]
    end
end

local function is_authorized()
    local headers = ngx.req.get_headers()
    local expires = tonumber(ngx.var.cookie_oauthexpires) or 0
    local user_id = ngx.unescape_uri(ngx.var.cookie_oauthuserid or "")
    local token = ngx.var.cookie_oauthaccesstoken or ""
    if expires == 0 and headers["oauthexpires"] then
        expires = tonumber(headers["oauthexpires"])
    end
    if user_id:len() == 0 and headers["oauthuserid"] then
        user_id = headers["oauthuserid"]
    end
    if token:len() == 0 and headers["oauthaccesstoken"] then
        token = headers["oauthaccesstoken"]
    end
    local expect_token = callback_host .. user_id .. expires
    if token == expect_token and expires then
        if expires > ngx.time() then
            return true
        else
            return false
        end
    else
        return false
    end
end

local function redirect_to_auth()
    return ngx.redirect("https://open.work.weixin.qq.com/wwopen/sso/qrconnect?" .. ngx.encode_args({
        appid = corp_id,
        agentid = agent_id,
        redirect_uri = callback_url,
        state = redirect_url
    }))
end

local function authorize()
    if uri ~= callback_uri then
        return redirect_to_auth()
    end
    local code = uri_args["code"]
    if not code then
        ngx.log(ngx.err, "not received code from https://open.work.weixin.qq.com/wwopen/sso/qrconnect")
        return ngx.exit(ngx.http_forbidden)
    end

    local access_token, request_access_token_err = request_access_token(code)
    if not access_token then
        ngx.log(ngx.err, "got error during access token request: " .. request_access_token_err)
        return ngx.exit(ngx.http_forbidden)
    end

    local user, request_user_err = request_user(access_token, code)
    if not user then
        ngx.log(ngx.err, "got error during profile request: " .. request_user_err)
        return ngx.exit(ngx.http_forbidden)
    end
    ngx.log(ngx.err, "user id: " .. user["userid"])

    local expires = ngx.time() + token_expires
    local cookie_tail = "; version=1; path=/; max-age=" .. expires
    if use_secure_cookie then
        cookie_tail = cookie_tail .. "; secure"
    end

    local user_id = user["userid"]
    local user_token = callback_host .. user_id .. expires

    ngx.header["set-cookie"] = {
        "oauthuserid=" .. ngx.escape_uri(user_id) .. cookie_tail,
        "oauthaccesstoken=" .. ngx.escape_uri(user_token) .. cookie_tail,
        "oauthexpires=" .. expires .. cookie_tail,
    }
    return ngx.redirect(uri_args["state"])
end

local function handle_logout()
    if uri == logout_uri then
        ngx.header["set-cookie"] = "oauthaccesstoken==deleted; path=/; expires=thu, 01 jan 1970 00:00:00 gmt"
        --return ngx.redirect("/")
    end
end

handle_logout()
if (not is_authorized()) then
    authorize()
end

使用 dockercompose 进行容器编排

这里需要讲几个点:

  • 设置前端的 args 可以在前端构建时传入后端接口地址
  • 设置网关的 hostname 可以设置网关容器的 hostname
  • 设置网关的 environment 可以传入相关配置
  • 最终运行时只有网关层进行暴露端口
version: "3.8"

services:
  api:
    build: ./api
    image: ca-api:latest
    container_name: ca-api

  web:
    build:
      context: ./web
      args:
        react_app_base_url: https://example.com/api
    image: ca-web:latest
    container_name: ca-web
    
  gateway:
    build: ./gateway
    image: ca-gateway:latest
    hostname: example.com
    volumes:
      - ./gateway/certs/fullchain.pem:/certs/cert.crt
      - ./gateway/certs/privkey.pem:/certs/cert.key
    ports:
      - 80:80
      - 443:443
    environment:
      - corp_id=
      - agent_id=
      - secret=
      - callback_host=example.com
      - callback_schema=https
      - callback_uri=/gateway/oauth_wechat
      - logout_uri=/gateway/oauth_logout
      - token_expires=7200
      - use_secure_cookie=true
    container_name: ca-gateway

开源代码

github https://github.com/k8scat/containerized-app
gitee https://gitee.com/k8scat/containerized-app

到此这篇关于docker+dockercompose封装web应用的文章就介绍到这了,更多相关docker+dockercompose封装web应用内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!