This repository has been archived on 2024-09-06. You can view files and clone it, but cannot push or open issues or pull requests.
clash/docs/introduction/service.md
Birkhoff Lee ca42ca2ca8
Docs: new documentation site (#2723)
This commit adds a VitePress build to the main repository,
aiming to ditch GitHub Wiki. Moving further, we're going to
host our own documentation site eithor on GitHub Pages or
something alike.
2023-05-15 21:47:01 +08:00

2.9 KiB

sidebarTitle, sidebarOrder
sidebarTitle sidebarOrder
Clash as a Service 3

Clash as a Service

While Clash is meant to be run in the background, there's currently no elegant way to implement daemons with Golang, hence we recommend you to daemonize Clash with third-party tools.

systemd

Copy Clash binary to /usr/local/bin and configuration files to /etc/clash:

cp clash /usr/local/bin
cp config.yaml /etc/clash/
cp Country.mmdb /etc/clash/

Create the systemd configuration file at /etc/systemd/system/clash.service:

[Unit]
Description=Clash daemon, A rule-based proxy in Go.
After=network-online.target

[Service]
Type=simple
Restart=always
ExecStart=/usr/local/bin/clash -d /etc/clash

[Install]
WantedBy=multi-user.target

After that you're supposed to reload systemd:

systemctl daemon-reload

Launch clashd on system startup with:

systemctl enable clash

Launch clashd immediately with:

systemctl start clash

Check the health and logs of Clash with:

systemctl status clash
journalctl -xe

Credits to ktechmidas for this guide. (#754)

Docker

We provide pre-built images of Clash and Clash Premium. Therefore you can deploy Clash with Docker Compose if you're on Linux. However, you should be advised that it's not recommended to run Clash Premium in a container.

::: warning This setup will not work on macOS systems due to the lack of host networking and TUN support in Docker for Mac. :::

::: code-group

services:
  clash:
    image: ghcr.io/dreamacro/clash
    restart: always
    volumes:
      - ./config.yaml:/root/.config/clash/config.yaml:ro
      # - ./ui:/ui:ro # dashboard volume
    ports:
      - "7890:7890"
      - "7891:7891"
      # - "8080:8080" # The External Controller (RESTful API)
    network_mode: "bridge"
services:
  clash:
    image: ghcr.io/dreamacro/clash-premium
    restart: always
    volumes:
      - ./config.yaml:/root/.config/clash/config.yaml:ro
      # - ./ui:/ui:ro # dashboard volume
    ports:
      - "7890:7890"
      - "7891:7891"
      # - "8080:8080" # The External Controller (RESTful API)
    cap_add:
      - NET_ADMIN
    devices:
      - /dev/net/tun
    network_mode: "host"

:::

Save as docker-compose.yaml and place your config.yaml in the same directory.

::: tip Before proceeding, refer to your platform documentations about time synchronisation - things will break if time is not in sync. :::

When you're ready, run the following commands to bring up Clash:

docker-compose up -d

You can view the logs with:

docker-compose logs

Stop Clash with:

docker-compose stop