我的docker随笔22:多域名同主机部署

需求:
只有一台云主机,但有多个不同域名网站,甚至还有二级域名。不能通过端口访问,即只有域名(二级域名)。同时需要启用 https (到期自动更新证书)。
网站内容为静态文件(当前暂定),docker 部署。使用原始httpd镜像,但是网站文件挂载。
使用 gitlab 管理网站源文件,通过 CI 构建静态文件,并自动更新到云主机。

目录

创建目录/home/latelee/docker/composefile/httpd,下设vhost.dacmecerts目录,再创建网站文件挂载目录,如html1html2,等等。

云主机

需要在域名后台添加子域名和IP的映射,否则无法访问。
云主机需要开启 80 和 443 端口。

部署

为方便管理,使用docker-compose部署。
反向代理:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
proxy:
image: jwilder/nginx-proxy
container_name: nginx-proxy
restart: always
ports:
- 80:80
- 443:443
labels:
com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy: "true"
volumes:
- ./certs:/etc/nginx/certs:ro
- ./acme:/acmecerts
- ./vhost.d:/etc/nginx/vhost.d
- ./html:/usr/share/nginx/html
- /var/run/docker.sock:/tmp/docker.sock:ro
networks:

使用镜像名为jwilder/nginx-proxy,可到 了解文档。
其中需要映射 80 和 443 端口,标签com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy是必须的,否则docker-letsencrypt-nginx-proxy-companion连不上代理容器。为方便管理,大部分挂载目录都在当前目录,注意,certs 是只读的。

https 认证:

1
2
3
4
5
6
7
8
9
10
11
12
13
letsencrypt-companion:
image: jrcs/letsencrypt-nginx-proxy-companion
container_name: letsencrypt
restart: always
volumes:
- ./certs:/etc/nginx/certs
- ./vhost.d:/etc/nginx/vhost.d
- ./html:/usr/share/nginx/html
- /var/run/docker.sock:/var/run/docker.sock:ro
networks:
- nginxp-net
depends_on:
- proxy

使用镜像jrcs/letsencrypt-nginx-proxy-companion,可到 了解详情。挂载目录也是必须的,其中自动创建的证书位于 certs 目录中。(存疑:证书有效期为三个月,到期是否自动更新?)

网站服务容器:

1
2
3
4
5
6
7
8
9
10
11
12
http1:
image: httpd
container_name: httpd1
volumes:
- ./html1:/usr/local/apache2/htdocs/
environment:
- VIRTUAL_HOST=latelee.org,www.latelee.org
- LETSENCRYPT_HOST=latelee.org,www.latelee.org
- LETSENCRYPT_EMAIL=test@latelee.org
- ENABLE_ACME=true
networks:
- nginxp-net

挂载当前目录 html1 为 apache 的服务根目录,里面为静态网站文件。注意,由于 httpd 只暴露了一个 80 端口,所以此处不需要指定 VIRTUAL_PORT 的值,反向代理容器会自动指定这个端口。VIRTUAL_HOST 和 LETSENCRYPT_HOST 指定了主机域名,可同时指定多个,使用逗号隔开即可。为方便理解,一般指定无前缀和带www的域名。LETSENCRYPT_EMAIL 指定邮箱,方便接收 letsencrypt 发的邮件。

这是一个模板,可以根据需求任意添加。如:

1
2
3
4
5
6
7
8
9
10
11
12
http2:
image: httpd
container_name: httpd2
volumes:
- ./html1:/usr/local/apache2/htdocs/
environment:
- VIRTUAL_HOST=i.latelee.org
- LETSENCRYPT_HOST=i.latelee.org
- LETSENCRYPT_EMAIL=test@latelee.org
- ENABLE_ACME=true
networks:
- nginxp-net

启动

使用如下命令启动:

1
docker-compose up -d

可针对个别服务进行启动、停止操作,如:

1
2
3
docker-compose up -d http2
docker-compose stop http2
docker-compose start http2

参考资料

jekyll生成页面
先安装ruby,使用管理员权限打开cmd,执行gem install jekyll
构建:
bundle exec jekyll b

运行:
bundle exec jekyll serve –incremental -H 0.0.0.0 -P 80

备份网站。全部在github上

后台:去掉网站名称的cname,登陆github.com,去掉cname OK

CI自动化:
统一在个人账号管理所有源文件。通过ci,生成静态文件,分别提交到云主机以及github.com对应仓库。
提交云:登陆云,删除对应目录内容,再拷贝。

更新文件后,不会自动刷新,重启容器可行。更好方法?

网站:
工作室:

个人:www.latelee.org blog.latelee.org i.latelee.org(jekyll)
大锤:jekyll
真实:jekyll

备份:
www CNAME 默认

@ A 默认 120.79.237.54

出错:
ACME server returned an error: urn:ietf:params:acme:error:rateLimited :: There were too many requests of a given type :: Error creating new account :: too many registrations for this IP: see

networks:
mongocluster_default:
external: true

docker-compose文件:
version: “2”
services:
proxy:
image: jwilder/nginx-proxy
container_name: nginx-proxy
restart: always
ports:

  - 80:80
  - 443:443
labels:
  com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy: "true"
volumes:
  - ./certs:/etc/nginx/certs:ro
  - ./acme:/acmecerts
  - ./vhost.d:/etc/nginx/vhost.d
  - ./html:/usr/share/nginx/html
  - /var/run/docker.sock:/tmp/docker.sock:ro
networks:
  - nginxp-net

letsencrypt:
image: jrcs/letsencrypt-nginx-proxy-companion
container_name: letsencrypt
restart: always
volumes:

  - ./certs:/etc/nginx/certs
  - ./vhost.d:/etc/nginx/vhost.d
  - ./html:/usr/share/nginx/html
  - /var/run/docker.sock:/var/run/docker.sock:ro
networks:
  - nginxp-net
depends_on:
  - proxy

latelee:
image: registry.cn-hangzhou.aliyuncs.com/latelee/lidch
container_name: latelee
volumes:

  - ./html_latelee:/usr/local/apache2/htdocs/ 
environment:
  - VIRTUAL_HOST=www.latelee.org
  - LETSENCRYPT_HOST=www.latelee.org
  - LETSENCRYPT_EMAIL=li@latelee.org
  - ENABLE_ACME=true
networks:
  - nginxp-net

ilatelee:
image: registry.cn-hangzhou.aliyuncs.com/latelee/lidch
container_name: ilatelee
volumes:

  - ./html_i.latelee:/usr/local/apache2/htdocs/ 
environment:
  - VIRTUAL_HOST=i.latelee.org
  - LETSENCRYPT_HOST=i.latelee.org
  - LETSENCRYPT_EMAIL=li@latelee.org
  - ENABLE_ACME=true
networks:
  - nginxp-net

bloglatelee:
image: registry.cn-hangzhou.aliyuncs.com/latelee/lidch
container_name: bloglatelee
volumes:

  - ./html_blog.latelee:/usr/local/apache2/htdocs/ 
environment:
  - VIRTUAL_HOST=blog.latelee.org
  - LETSENCRYPT_HOST=blog.latelee.org
  - LETSENCRYPT_EMAIL=li@latelee.org
  - ENABLE_ACME=true
networks:
  - nginxp-net

cst:
image: registry.cn-hangzhou.aliyuncs.com/latelee/lidch
container_name: cst
volumes:

  - ./html_cst:/usr/local/apache2/htdocs/ 
environment:
  - VIRTUAL_HOST=cststudio.com.cn,www.cststudio.com.cn
  - LETSENCRYPT_HOST=cststudio.com.cn,www.cststudio.com.cn
  - LETSENCRYPT_EMAIL=li@latelee.org
  - ENABLE_ACME=true
networks:
  - nginxp-net

lijj:
image: registry.cn-hangzhou.aliyuncs.com/latelee/lidch
container_name: lijj
volumes:

  - ./html_lijj:/usr/local/apache2/htdocs/
environment:
  - VIRTUAL_HOST=lijiangjin.cn,www.lijiangjin.cn
  - LETSENCRYPT_HOST=lijiangjin.cn,www.lijiangjin.cn
  - LETSENCRYPT_EMAIL=li@latelee.org
  - ENABLE_ACME=true
networks:
  - nginxp-net

lidch:
image: registry.cn-hangzhou.aliyuncs.com/latelee/lidch
container_name: lidch
volumes:

  - ./html_lidch:/usr/local/apache2/htdocs/ 
environment:
  - VIRTUAL_HOST=lidachui.cn,www.lidachui.cn
  - LETSENCRYPT_HOST=lidachui.cn,www.lidachui.cn
  - LETSENCRYPT_EMAIL=li@latelee.org
  - ENABLE_ACME=true
networks:
  - nginxp-net

networks:
nginxp-net:
driver: bridge