Press "Enter" to skip to content

clash in docker 自己编译构建镜像

背景

作为想帮一切塞进容器里面的我,怎么能容忍clash独占一个虚拟机?

官方的镜像是可以用的,但是我使用的是自己修改过的版本,需要自己打包构建

操作

官方镜像 dreamacro/clash

官方的dockerfile在这 https://github.com/Dreamacro/clash/blob/master/Dockerfile

之前想通过修改官方的dockerfile来适配自己的需求,但是官方的写的太高级了,兼容了跨平台和自动构建等等。。 尝试了半天发现触及了很多自己的知识盲区,搞不下去了,于是乎尝试自己写了个。

  1. 我们克隆官方源码,在项目根目录创建几个文件

创建 docker-build.sh

go mod tidy
go build
wget https://github.com/Dreamacro/maxmind-geoip/releases/latest/download/Country.mmdb
cp -rf ../clash-dashboard/dist ui
docker build -t clash -f MyDockerfile .
rm -rf clash
rm -rf Country.mmdb*
rm -rf ui

这个文件就是我们要执行编译的脚本

简单说明下,脚本执行了我们clash项目的编译(当然你本地先要有go编译环境啊。。),然后下载了Country.mmdb,然后将我们clash-dashboard的编译文件拷贝过来,然后用我们自己的dockerfile文件构建镜像,然后执行清理动作。

创建 MyDOckerfile

FROM ubuntu:bionic

ADD clash /clash
ADD Country.mmdb /root/.config/clash/Country.mmdb
ADD ui /ui
ADD entrypoint.sh /

RUN chmod a+x /clash
CMD ["/clash"]

Dockerfile就很简单了,没啥说的,帮文件都放进来,然后给好执行权限,配置好入口即可。

最后用docker-compose执行我们项目:

version: '3.6'
services:
  clash:
    image: clash:latest
    volumes:
      - /clash/config.yaml:/root/.config/clash/config.yaml
      - /var/log/clash:/var/log/clash
    ports:
      - target: 7890
        published: 7890
        protocol: tcp
        mode: host
      - target: 7891
        published: 7891
        protocol: tcp
        mode: host
      - target: 9090
        published: 9090
        protocol: tcp
        mode: host
    restart: unless-stopped

网络模式要用host,不然你看不到访问者的源IP地址。

我们clash配置要注意的地方就是我们dashboard的位置是在 external-ui: /ui 别搞错了。别的就用标准配置就行了,没啥说的。

参考资料:

https://hub.docker.com/r/dreamacro/clash

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

© 2022 小菜菜. 皖ICP备18017830号-1