{"id":94,"date":"2025-08-13T10:55:32","date_gmt":"2025-08-13T02:55:32","guid":{"rendered":"http:\/\/blog.thirdbody.cn\/?p=94"},"modified":"2025-08-13T10:55:32","modified_gmt":"2025-08-13T02:55:32","slug":"jenkins-pipeline-docker%e6%9e%84%e5%bb%ba%e5%92%8c%e7%ae%a1%e7%90%86%e6%9c%8d%e5%8a%a1","status":"publish","type":"post","link":"http:\/\/blog.thirdbody.cn\/index.php\/2025\/08\/13\/jenkins-pipeline-docker%e6%9e%84%e5%bb%ba%e5%92%8c%e7%ae%a1%e7%90%86%e6%9c%8d%e5%8a%a1\/","title":{"rendered":"Jenkins pipeline + docker\u6784\u5efa\u548c\u7ba1\u7406\u670d\u52a1"},"content":{"rendered":"<h2>Jenkins pipeline + docker\u6784\u5efa\u548c\u7ba1\u7406\u670d\u52a1<\/h2>\n<h3>1. \u90e8\u7f72Jenkins\u670d\u52a1<\/h3>\n<p>Jenkins \u662f\u4e00\u6b3e\u5f00\u6e90\u7684 <strong>\u6301\u7eed\u96c6\u6210 \/ \u6301\u7eed\u4ea4\u4ed8\uff08CI\/CD\uff09<\/strong> \u81ea\u52a8\u5316\u670d\u52a1\u5668\uff0c\u7528 Java \u7f16\u5199\uff0cMIT \u8bb8\u53ef\u8bc1\uff0c\u53ef\u72ec\u7acb\u8fd0\u884c\u5728 Windows\u3001macOS\u3001Linux \u751a\u81f3 Docker\/K8s \u4e0a\u3002\u5b83\u7684\u6838\u5fc3\u4f5c\u7528\u662f\uff1a<strong>\u628a\u201c\u5199\u4ee3\u7801 \u2192 \u6784\u5efa \u2192 \u6d4b\u8bd5 \u2192 \u6253\u5305 \u2192 \u90e8\u7f72\u201d\u8fd9\u4e00\u7cfb\u5217\u91cd\u590d\u52b3\u52a8\u81ea\u52a8\u5316<\/strong>\uff0c\u8ba9\u56e2\u961f\u66f4\u5feb\u3001\u66f4\u53ef\u9760\u5730\u4ea4\u4ed8\u8f6f\u4ef6<\/p>\n<p>\u672c\u6587\u91c7\u7528<code>docker-compose<\/code>\u7684\u65b9\u5f0f\u90e8\u7f72<strong>Jenkins<\/strong>\uff0c\u6309\u7167\u5982\u4e0b\u6b65\u9aa4\u8fdb\u884c\uff1a<\/p>\n<h4>1.1 \u62c9\u53d6LTS(\u957f\u671f\u652f\u6301\u7248)\u7248<strong>Jenkins Docker<\/strong>\u955c\u50cf<\/h4>\n<pre><code class=\"language-shell\">docker pull jenkins:lts<\/code><\/pre>\n<h4>1.2 \u521b\u5efa\u6301\u4e45\u5316\u5b58\u50a8\u76ee\u5f55(\u975e\u5e38\u91cd\u8981)<\/h4>\n<pre><code class=\"language-shell\">mkdir -p \/home\/data\/jenkins<\/code><\/pre>\n<h4>1.3 \u6302\u8f7d\u5bbf\u4e3b\u673a\u7684docker\u548cdocker-compose(\u53ef\u9009)<\/h4>\n<p>\u4e3a\u4e86\u80fd\u591f\u5728<strong>Jenkins<\/strong>\u6d41\u6c34\u7ebf\u4e2d\u4f7f\u7528<code>docker<\/code>\u6216<code>docker compose<\/code>\u6784\u5efa\u670d\u52a1\uff0c\u9700\u8981\u5728<strong>Jenkins<\/strong>\u5bb9\u5668\u4e2d\u6302\u8f7d\u5bbf\u4e3b\u673a\u7684<code>docker<\/code>\u3001<code>docker.sock<\/code>\u4ee5\u53ca<code>docker compose<\/code><\/p>\n<p>\u76ee\u524d\u6d4b\u8bd5\u60c5\u51b5\uff0c<strong>Jenkins<\/strong>\u652f\u6301docker-compose v2.24\u53ca\u4ee5\u4e0a\u7248\u672c\uff0c\u53ef\u4ecegithub\u6216\u4ee5\u4e0b\u94fe\u63a5\u83b7\u53d6\uff1a<\/p>\n<p><a href=\"http:\/\/ftp.thirdbody.cn\/study\/docker-compose-linux-x86_64\">\u56fd\u5185\u76f4\u94fe<\/a>       <a href=\"https:\/\/github.com\/docker\/compose\/releases\">github\u5730\u5740<\/a><\/p>\n<p>\u4fee\u6539<code>docker-compose<\/code>\u6743\u9650\uff1a<\/p>\n<pre><code>chmod +x \/home\/docker-file\/docker-compose-v2.24.0\/docker-compose-linux-x86_64<\/code><\/pre>\n<h4>1.4 <code>docker-compose.yml<\/code>\u6587\u4ef6\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b<\/h4>\n<pre><code class=\"language-yaml\">version: &#039;3&#039;\nservices:\n  jenkins:\n    image: jenkins\/jenkins:lts\n    privileged: true\n    container_name: jenkins-server\n    ports:\n      - &quot;8080:8080&quot;\n      - &quot;50000:50000&quot;\n    volumes:\n      - \/home\/data\/jenkins:\/var\/jenkins_home\n      - \/var\/run\/docker.sock:\/var\/run\/docker.sock\n      - \/usr\/bin\/docker:\/usr\/bin\/docker:ro\n      - \/home\/docker-file\/docker-compose-v2.24.0\/docker-compose-linux-x86_64:\/usr\/local\/bin\/docker-compose\n    restart: always<\/code><\/pre>\n<h4>1.5 \u542f\u52a8<strong>Jenkins<\/strong>\u670d\u52a1<\/h4>\n<pre><code>docker-compose up -d<\/code><\/pre>\n<h4>1.6 \u4f7f\u7528\u6d4f\u89c8\u5668\u8bbf\u95ee<\/h4>\n<p><a href=\"http:\/\/115.190.149.0:8080\">http:\/\/115.190.149.0:8080<\/a><\/p>\n<h4>1.7 \u83b7\u53d6\u521d\u59cb\u5316\u5bc6\u7801\u5e76\u5b8c\u6210\u521d\u59cb\u5316\u6d41\u7a0b<\/h4>\n<p>\u521d\u59cb\u5bc6\u7801\u5728\u4ee5\u4e0b\u6587\u4ef6\u4e2d\uff1a<\/p>\n<p>\/home\/data\/jenkins\/secrets\/initialAdminPassword<\/p>\n<h4>1.8 \u521d\u59cb\u5316\u5b8c\u6210\u540e\u8fdb\u5165\u767b\u5f55\u9875\u9762<\/h4>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/blog\/Jenkins-login.png\" alt=\"Jenkins\u767b\u5f55\u9875\u9762\" \/><\/p>\n<h4>1.9 \u8f93\u5165\u8bbe\u7f6e\u7684\u8d26\u53f7\u5bc6\u7801\u767b\u5f55\u540e\u80fd\u770b\u5230\u4ee5\u4e0b\u9875\u9762\u8bf4\u660e\u90e8\u7f72\u6210\u529f<\/h4>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/blog\/Jenkins-main.png\" alt=\"Jnekins-main\" \/><\/p>\n<h3>2. \u4f7f\u7528Jenkins + docker compose \u542f\u52a8Python http\u670d\u52a1\u6848\u4f8b<\/h3>\n<p>\u672c\u7ae0\u5c06\u7528\u4e00\u4e2a\u6848\u4f8b\u8bf4\u660e\u5982\u4f55\u4f7f\u7528<strong>Jenkins pipeline<\/strong>(\u6d41\u6c34\u7ebf)\u4efb\u52a1\u6784\u5efa\u670d\u52a1<\/p>\n<h4>2.1 \u9879\u76ee\u76ee\u5f55\u7ed3\u6784\u5982\u4e0b<\/h4>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/blog\/Jenkins-sample-structure.png\" alt=\"\u76ee\u5f55\u7ed3\u6784\" \/><\/p>\n<h4>2.2 \u5404\u6587\u4ef6\u6837\u4f8b<\/h4>\n<h5>2.2.1 app.py<\/h5>\n<pre><code class=\"language-python\">from fastapi import FastAPI, Query, Request\nfrom fastapi.middleware.cors import CORSMiddleware\nimport uvicorn\n\napp = FastAPI()\napp.add_middleware(\n    CORSMiddleware,\n    allow_origins=[&quot;*&quot;],\n    allow_methods=[&quot;*&quot;],\n    allow_headers=[&quot;*&quot;]\n)\n\n@app.get(&quot;\/test\/&quot;)\nasync def read_items(name: str):\n    return f&quot;Hello {name}, this is a test page ! ! ! ! ! !&quot;\n\nif __name__ == &quot;__main__&quot;:\n    uvicorn.run(app, host=&quot;0.0.0.0&quot;, port=5000)<\/code><\/pre>\n<h5>2.2.2 Dockfile<\/h5>\n<pre><code class=\"language-dockerfile\">FROM python:3.9\nCOPY docker\/libs \/libs\nRUN pip install -r \/libs\/requirements.txt -i https:\/\/pypi.tuna.tsinghua.edu.cn\/simple\nWORKDIR \/jk_test\nCOPY app.py .\nCMD [&quot;python&quot;, &quot;app.py&quot;]<\/code><\/pre>\n<h5>2.2.3 docker-compose.yml<\/h5>\n<pre><code>version: &quot;3.3&quot;\n\nservices:\n  jk-test-worker:\n    build:\n      context: ..\n      dockerfile: docker\/Dockerfile\n    container_name: jk-test-service-1\n    ports:\n      - &quot;5002:5000&quot;\n    environment:\n      PYTHONPATH: \/code\n\nnetworks:\n  default:\n    driver: bridge\n    ipam:\n      driver: default\n      config:\n        - subnet: &quot;172.24.0.0\/24&quot;<\/code><\/pre>\n<h5>2.2.4 requirements.lib<\/h5>\n<pre><code>fastapi==0.101.1\nuvicorn==0.23.1<\/code><\/pre>\n<h4>2.3 \u5728<code>gitee<\/code>\u521b\u5efa\u4ed3\u5e93<\/h4>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/blog\/Jenkins-create-gitee.png\" alt=\"\" \/><\/p>\n<h4>2.4 \u672c\u5730\u521b\u5efagit\u4ed3\u5e93\u5e76\u5173\u8054\u8fdc\u7a0b\u7684gitee\u4ed3\u5e93<\/h4>\n<h5>2.4.1 \u521d\u59cb\u5316git\u4ed3\u5e93<\/h5>\n<pre><code class=\"language-shell\">cd jenkins_test\ngit init<\/code><\/pre>\n<h5>2.4.2 \u5173\u8054\u8fdc\u7a0bgitee\u4ed3\u5e93<\/h5>\n<pre><code class=\"language-shell\">git remote add origin https:\/\/gitee.com\/DerrickChiu\/jk_test.git<\/code><\/pre>\n<h5>2.4.3 \u63d0\u4ea4\u4fee\u6539\u5e76\u63a8\u9001\u5230gitee\u4ed3\u5e93<\/h5>\n<pre><code class=\"language-shell\">git add .   # \u6dfb\u52a0\u4fee\u6539\u7684\u5185\u5bb9\ngit commit -m &quot;\u7b2c\u4e00\u6b21\u63d0\u4ea4&quot; # \u63d0\u4ea4\u4fee\u6539\ngit push origin master  # \u63a8\u9001\u5230gitee\u4ed3\u5e93<\/code><\/pre>\n<h4>2.5 \u6784\u5efa<strong>Jenkins Pipeline<\/strong>\u4efb\u52a1<\/h4>\n<h5>2.5.1 \u521b\u5efa\u4efb\u52a1<\/h5>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/blog\/Jenkins-create-pipeline-1.png\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/blog\/Jenkins-create-pipeline-2.png\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/blog\/Jenkins-create-pipeline-3.png\" alt=\"\" \/><\/p>\n<h5>2.5.2 \u6784\u5efa\u4efb\u52a1<\/h5>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/blog\/Jenkins-build-pipeline-1.png\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/blog\/Jenkins-build-pipeline-2.png\" alt=\"\" \/><\/p>\n<p><strong>\u7eff\u8272\u5bf9\u53f7<\/strong>\u8bf4\u660e\u4efb\u52a1\u6784\u5efa\u6210\u529f<\/p>\n<h5>2.5.3 \u67e5\u770b\u65e5\u5fd7<\/h5>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/blog\/Jenkins-pipeline-log.png\" alt=\"\" \/><\/p>\n<h5>2.5.4 \u6d4b\u8bd5\u670d\u52a1<\/h5>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/blog\/Jenkins-pipeline-test.png\" alt=\"\" \/><\/p>\n<p>\u670d\u52a1\u542f\u52a8\u6210\u529f\uff01\uff01\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Jenkins pipeline + docker\u6784\u5efa\u548c\u7ba1\u7406\u670d\u52a1 1. \u90e8\u7f72Jenkins\u670d\u52a1 Jenkins [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":95,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[50,49,8,51,52],"class_list":["post-94","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-other","tag-docker","tag-jenkins","tag-python","tag-51","tag-52"],"_links":{"self":[{"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/posts\/94","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/comments?post=94"}],"version-history":[{"count":1,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/posts\/94\/revisions"}],"predecessor-version":[{"id":96,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/posts\/94\/revisions\/96"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/media\/95"}],"wp:attachment":[{"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/media?parent=94"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/categories?post=94"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/tags?post=94"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}