{"id":608,"date":"2023-02-13T10:43:37","date_gmt":"2023-02-13T02:43:37","guid":{"rendered":"https:\/\/scutvk.cn\/?p=608"},"modified":"2023-02-13T10:43:48","modified_gmt":"2023-02-13T02:43:48","slug":"linux%e8%bf%90%e8%a1%8c%e5%a4%9a%e4%b8%aa-docker-%e5%ae%b9%e5%99%a8%ef%bc%8c%e5%b9%b6%e4%bd%bf%e7%94%a8%e4%b8%8d%e5%90%8c%e7%9a%84-clash-%e8%ae%a2%e9%98%85","status":"publish","type":"post","link":"https:\/\/scutvk.cn\/?p=608","title":{"rendered":"Linux\u8fd0\u884c\u591a\u4e2a Docker \u5bb9\u5668\uff0c\u5e76\u4f7f\u7528\u4e0d\u540c\u7684 Clash \u8ba2\u9605"},"content":{"rendered":"\n<p class=\"has-text-align-center\" style=\"font-size:17px\">\u6ce8\uff1a\u539f\u521b\u5185\u5bb9\uff0c\u7981\u6b62\u79c1\u81ea\u8f6c\u8f7d<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u603b\u4f53\u6d41\u7a0b\uff1a<\/strong><\/p>\n\n\n\n<p>\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u6b65\u9aa4\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a Docker \u5bb9\u5668\u7684\u6a21\u677f\uff0c\u8be5\u6a21\u677f\u5df2\u914d\u7f6e\u4e3a\u4f7f\u7528 Clash\uff0c\u5e76\u5305\u542b\u5fc5\u8981\u7684\u914d\u7f6e\u6587\u4ef6\u3002<\/li>\n\n\n\n<li>\u4e3a\u6bcf\u4e2a\u8981\u4f7f\u7528\u7684 Clash \u8ba2\u9605\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 Docker \u5bb9\u5668\uff0c\u5e76\u5c06\u5176\u914d\u7f6e\u4e3a\u4f7f\u7528\u8be5\u8ba2\u9605\u3002<\/li>\n\n\n\n<li>\u4e3a\u6bcf\u4e2a Docker \u5bb9\u5668\u5206\u914d\u4e00\u4e2a\u4e0d\u540c\u7684\u7aef\u53e3\u53f7\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 Docker API \u6216\u547d\u4ee4\u884c\u5de5\u5177\u542f\u52a8\u5e76\u8fd0\u884c\u8fd9\u4e9b\u5bb9\u5668\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u8bf7\u6ce8\u610f\uff0c\u8fd8\u9700\u8981\u914d\u7f6e\u8d1f\u8f7d\u5747\u8861\u4ee5\u786e\u4fdd\u6bcf\u4e2a\u5bb9\u5668\u7684\u6d41\u91cf\u901a\u8fc7\u4e0d\u540c\u7684 Clash \u8282\u70b9\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u521b\u5efa\u4e00\u4e2a Docker \u5bb9\u5668\u7684\u6a21\u677f\uff0c\u8be5\u6a21\u677f\u5df2\u914d\u7f6e\u4e3a\u4f7f\u7528 Clash\uff0c\u5e76\u5305\u542b\u5fc5\u8981\u7684\u914d\u7f6e\u6587\u4ef6\u3002<\/strong><\/p>\n\n\n\n<p>\u4e3a\u4e86\u521b\u5efa Docker \u5bb9\u5668\u6a21\u677f\uff0c\u60a8\u9700\u8981\u521b\u5efa\u4e00\u4e2a Dockerfile\u3002Dockerfile \u662f\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\uff0c\u7528\u4e8e\u6307\u793a Docker \u5982\u4f55\u6784\u5efa\u955c\u50cf\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684 Dockerfile \u793a\u4f8b\uff0c\u7528\u4e8e\u6784\u5efa\u5177\u6709 Clash \u548c\u5fc5\u8981\u914d\u7f6e\u6587\u4ef6\u7684 Docker \u5bb9\u5668\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash line-numbers\"># Use an existing image as the base image\r\nFROM alpine:latest\r\n\r\n# Install Clash\r\nRUN apk add --no-cache curl &amp;&amp; \\\r\n    curl https:\/\/dl.luminati.io\/clash-binaries\/linux\/clash-linux-amd64-0.13.0.gz --output clash.gz &amp;&amp; \\\r\n    gzip -d clash.gz &amp;&amp; \\\r\n    chmod +x clash-linux-amd64-0.13.0 &amp;&amp; \\\r\n    mv clash-linux-amd64-0.13.0 \/usr\/bin\/clash\r\n\r\n# Copy the configuration file to the container\r\nCOPY config.yaml \/etc\/clash\/config.yaml\r\n\r\n# Set the working directory\r\nWORKDIR \/etc\/clash\r\n\r\n# Start Clash when the container is launched\r\nCMD [\"clash\"]\r<\/code><\/pre>\n\n\n\n<p>\u8bf7\u6ce8\u610f\uff0c\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\u4f7f\u7528\u7684\u662f Clash \u7684 0.13.0 \u7248\u672c\uff0c\u56e0\u6b64\u8bf7\u6839\u636e\u8981\u4f7f\u7528\u7684\u7248\u672c\u8fdb\u884c\u76f8\u5e94\u7684\u4fee\u6539\u3002<\/p>\n\n\n\n<p>\u5728\u4fdd\u5b58 Dockerfile \u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6784\u5efa Docker \u955c\u50cf\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash line-numbers\">docker build -t clash-template .\r<\/code><\/pre>\n\n\n\n<p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fd0\u884c\u8be5\u955c\u50cf\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash line-numbers\">docker run --name clash -d clash-template\r<\/code><\/pre>\n\n\n\n<p>\u8bf7\u6ce8\u610f\uff0c\u5728\u6784\u5efa\u955c\u50cf\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u5728\u5f53\u524d\u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201cconfig.yaml\u201d\u7684\u6587\u4ef6\uff0c\u8be5\u6587\u4ef6\u5305\u542b\u60a8\u7684 Clash \u914d\u7f6e\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u4e3a\u6bcf\u4e2a\u8981\u4f7f\u7528\u7684 Clash \u8ba2\u9605\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 Docker \u5bb9\u5668\uff0c\u5e76\u5c06\u5176\u914d\u7f6e\u4e3a\u4f7f\u7528\u8be5\u8ba2\u9605\u3002<\/strong><\/p>\n\n\n\n<p>\u4e3a\u4e86\u4e3a\u6bcf\u4e2a Clash \u8ba2\u9605\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 Docker \u5bb9\u5668\uff0c\u9700\u8981\u5728\u6784\u5efa\u955c\u50cf\u65f6\u4f20\u9012\u4e0d\u540c\u7684\u914d\u7f6e\u6587\u4ef6\u3002<\/p>\n\n\n\n<p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u521b\u5efa\u4e0e\u4e0a\u9762\u793a\u4f8b\u76f8\u540c\u7684\u955c\u50cf\uff0c\u4f46\u662f\u4f7f\u7528\u81ea\u5df1\u7684\u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash line-numbers\">docker build --build-arg config_file=&lt;config-file-name> -t clash-template .\r<\/code><\/pre>\n\n\n\n<p>\u8bf7\u6ce8\u610f\uff0c\u9700\u8981\u5c06\u81ea\u5df1\u7684\u914d\u7f6e\u6587\u4ef6\u540d\u79f0\u66ff\u6362\u4e3a <code>&lt;config-file-name><\/code>\u3002<\/p>\n\n\n\n<p>\u7136\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fd0\u884c\u8be5\u955c\u50cf\uff0c\u4ee5\u4f7f\u7528\u7684 Clash \u8ba2\u9605\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash line-numbers\">docker run --name clash -d clash-template\r<\/code><\/pre>\n\n\n\n<p>\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u5bb9\u5668\u540d\u79f0\u548c\u914d\u7f6e\u6587\u4ef6\u6765\u521b\u5efa\u591a\u4e2a\u5bb9\u5668\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u5bb9\u5668\u914d\u7f6e\u4e0d\u540c\u7684 Clash \u8ba2\u9605\u3002<\/p>\n\n\n\n<p>\u8bf7\u6ce8\u610f\uff0c\u8fd9\u4ec5\u662f\u521b\u5efa Docker \u5bb9\u5668\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u8fdb\u884c\u4fee\u6539\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u4e3a\u6bcf\u4e2a Docker \u5bb9\u5668\u5206\u914d\u4e00\u4e2a\u4e0d\u540c\u7684\u7aef\u53e3\u53f7\u3002<\/strong><\/p>\n\n\n\n<p>\u4e3a\u6bcf\u4e2a Docker \u5bb9\u5668\u5206\u914d\u4e00\u4e2a\u4e0d\u540c\u7684\u7aef\u53e3\u53f7\uff0c\u53ef\u4ee5\u4f7f\u7528 Docker \u7684 <code>-p<\/code> \u9009\u9879\u3002\u8be5\u9009\u9879\u5141\u8bb8\u5c06\u4e3b\u673a\u7684\u7aef\u53e3\u6620\u5c04\u5230\u5bb9\u5668\u7684\u7aef\u53e3\uff0c\u4ee5\u4fbf\u901a\u8fc7 Internet \u8bbf\u95ee\u5bb9\u5668\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c\u4ee5\u4e0b\u547d\u4ee4\u5c06\u4e3b\u673a\u7684\u7aef\u53e3 8080 \u6620\u5c04\u5230\u5bb9\u5668\u7684\u7aef\u53e3 6800\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash line-numbers\">docker run --name clash -p 8080:6800 -d clash-template\r<\/code><\/pre>\n\n\n\n<p>\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u7aef\u53e3\u53f7\uff08\u4f8b\u5982 8081\u30018082 \u7b49\uff09\u4e3a\u6bcf\u4e2a\u5bb9\u5668\u5206\u914d\u4e0d\u540c\u7684\u7aef\u53e3\u3002<\/p>\n\n\n\n<p>\u8bf7\u6ce8\u610f\uff0c\u8fd9\u4ec5\u662f\u4e3a Docker \u5bb9\u5668\u5206\u914d\u7aef\u53e3\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u8fdb\u884c\u4fee\u6539\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u4f7f\u7528 Docker API \u6216\u547d\u4ee4\u884c\u5de5\u5177\u542f\u52a8\u5e76\u8fd0\u884c\u8fd9\u4e9b\u5bb9\u5668\u3002<\/strong><\/p>\n\n\n\n<p>\u53ef\u4ee5\u4f7f\u7528 Docker API \u6216\u547d\u4ee4\u884c\u5de5\u5177\u542f\u52a8\u548c\u8fd0\u884c\u8fd9\u4e9b\u5bb9\u5668\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528 Docker API \u542f\u52a8\u548c\u8fd0\u884c\u5bb9\u5668\u7684\u793a\u4f8bPython\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python line-numbers\">import docker\r\n\r\nclient = docker.DockerClient(base_url='unix:\/\/var\/run\/docker.sock')\r\n\r\ncontainer = client.containers.run(\"clash-template\", name=\"clash1\", ports={'6800\/tcp': ('0.0.0.0', 8080)}, detach=True)\r<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\u542f\u52a8\u548c\u8fd0\u884c\u5bb9\u5668\u7684\u793a\u4f8b\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash line-numbers\">docker run --name clash1 -p 8080:6800 -d clash-template\r<\/code><\/pre>\n\n\n\n<p>\u53ef\u4ee5\u901a\u8fc7\u91cd\u590d\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u5e76\u5c06\u5bb9\u5668\u7684\u540d\u79f0\u548c\u6620\u5c04\u7684\u7aef\u53e3\u53f7\u66f4\u6539\u4e3a\u4e0d\u540c\u7684\u503c\uff0c\u542f\u52a8\u548c\u8fd0\u884c\u591a\u4e2a\u5bb9\u5668\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6ce8\uff1a\u539f\u521b\u5185\u5bb9\uff0c\u7981\u6b62\u79c1\u81ea\u8f6c\u8f7d \u603b\u4f53\u6d41\u7a0b\uff1a \u53ef\u4ee5\u4f7f\u7528\u5982&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[28,11],"tags":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/scutvk.cn\/index.php?rest_route=\/wp\/v2\/posts\/608"}],"collection":[{"href":"https:\/\/scutvk.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/scutvk.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/scutvk.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/scutvk.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=608"}],"version-history":[{"count":1,"href":"https:\/\/scutvk.cn\/index.php?rest_route=\/wp\/v2\/posts\/608\/revisions"}],"predecessor-version":[{"id":609,"href":"https:\/\/scutvk.cn\/index.php?rest_route=\/wp\/v2\/posts\/608\/revisions\/609"}],"wp:attachment":[{"href":"https:\/\/scutvk.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=608"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/scutvk.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=608"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/scutvk.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=608"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}