{"id":36,"date":"2025-05-30T17:48:05","date_gmt":"2025-05-30T09:48:05","guid":{"rendered":"https:\/\/danchengjie.cn\/?p=36"},"modified":"2026-03-30T21:52:27","modified_gmt":"2026-03-30T13:52:27","slug":"ubuntu%e4%b8%8a%e9%83%a8%e7%bd%b2docker","status":"publish","type":"post","link":"https:\/\/danchengjie.cn\/index.php\/2025\/05\/30\/ubuntu%e4%b8%8a%e9%83%a8%e7%bd%b2docker\/","title":{"rendered":"ubuntu\u4e0a\u90e8\u7f72docker"},"content":{"rendered":"\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u7b2c\u4e00\u90e8\u5206\uff1a\u6781\u7b80\u5b89\u88c5 Docker<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>sudo apt remove docker docker-engine docker.io containerd runc<\/li>\n\n\n\n<li>sudo apt update sudo apt install apt-transport-https ca-certificates curl software-properties-common gnupg<\/li>\n\n\n\n<li>curl -fsSL <a href=\"https:\/\/download.docker.com\/linux\/ubuntu\/gpg\">https:\/\/download.docker.com\/linux\/ubuntu\/gpg<\/a> | sudo gpg &#8211;dearmor -o \/usr\/share\/keyrings\/docker-archive-keyring.gpg<\/li>\n\n\n\n<li>echo &#8220;deb [arch=amd64 signed-by=\/usr\/share\/keyrings\/docker-archive-keyring.gpg] <a href=\"https:\/\/download.docker.com\/linux\/ubuntu\">https:\/\/download.docker.com\/linux\/ubuntu<\/a> $(lsb_release -cs) stable&#8221; | sudo tee \/etc\/apt\/sources.list.d\/docker.list &gt; \/dev\/null<\/li>\n\n\n\n<li>sudo apt update sudo apt install docker-ce docker-ce-cli containerd.io<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u7b2c\u4e8c\u90e8\u5206\uff1a\u6838\u5fc3\u96be\u70b9\u2014\u2014\u89e3\u51b3\u201c\u62c9\u53d6\u4e0d\u5230\u955c\u50cf\u201d<\/h3>\n\n\n\n<p>\u8fd9\u662f\u6240\u6709\u56fd\u5185 Docker \u521d\u5b66\u8005\u9047\u5230\u7684<strong>\u6700\u5927\u62e6\u8def\u864e<\/strong>\u3002\u5f53\u4f60\u6267\u884c <code>docker pull<\/code> \u65f6\uff0c\u7ecf\u5e38\u4f1a\u5361\u4f4f\u6216\u62a5\u9519 <code>TLS handshake timeout<\/code>\u3002\u8fd9\u662f\u56e0\u4e3a Docker Hub \u7684\u5b98\u65b9\u670d\u52a1\u5668\u5728\u6d77\u5916\u3002<\/p>\n\n\n\n<p>\u51fa\u73b0\u7684\u62a5\u9519\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>root@gx-VMware-Virtual-Platform:~# docker pull nginx Using default tag: latest Error response from daemon: failed to resolve reference \"docker.io\/library\/nginx:latest\": failed to do request: Head \"https:\/\/registry-1.docker.io\/v2\/library\/nginx\/manifests\/latest\": dial tcp 128.242.240.117:443: connect: connection refused<\/code><\/pre>\n\n\n\n<p>\u9488\u5bf9\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4e3a\u4f60\u6574\u7406\u4e86<strong>\u4e09\u79cd<\/strong>\u4e0d\u540c\u5c42\u7ea7\u7684\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u65b9\u6848\u4e00\uff1a\u914d\u7f6e\u56fd\u5185\u955c\u50cf\u52a0\u901f\u5668\uff08\u57fa\u7840\u65b9\u6848\uff09<\/h4>\n\n\n\n<p>\u8fd9\u662f\u6700\u5e38\u89c4\u7684\u65b9\u6cd5\uff0c\u4f46\u8fd1\u671f\u56fd\u5185\u5f88\u591a\u955c\u50cf\u7ad9\uff08\u5982\u963f\u91cc\u4e91\u3001\u7f51\u6613\u7b49\uff09\u7ecf\u5e38\u4e0d\u7a33\u5b9a\u3002\u5efa\u8bae\u4f18\u5148\u5c1d\u8bd5\uff0c\u4e0d\u884c\u518d\u6362\u65b9\u6848\u4e8c\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u521b\u5efa\u6216\u4fee\u6539\u914d\u7f6e\u6587\u4ef6\uff1a\uff08\u7528vim\u7f16\u8f91\u4e5f\u53ef\uff0c\u53ea\u662f\u9700\u8981apt-get install vim\uff09sudo mkdir -p \/etc\/docker<br>sudo nano \/etc\/docker\/daemon.json<\/li>\n\n\n\n<li>\u5728\u6587\u4ef6\u4e2d\u7c98\u8d34\u4ee5\u4e0b\u5185\u5bb9\uff08\u8fd9\u91cc\u63d0\u4f9b\u51e0\u4e2a\u76ee\u524d\u76f8\u5bf9\u53ef\u7528\u7684\uff09\uff1a<code>{<br>\"registry-mirrors\": [<br>\u00a0 \"https:\/\/docker.m.daocloud.io\",<br>\u00a0 \"https:\/\/huecker.io\",<br>\u00a0 \"https:\/\/dockerhub.timeweb.cloud\"<br>]<br>}<\/code><\/li>\n\n\n\n<li>\u4fdd\u5b58\u9000\u51fa\uff08\u6309 <code>Ctrl+O<\/code> \u56de\u8f66\uff0c\u518d\u6309 <code>Ctrl+X<\/code>\uff09\u3002<\/li>\n\n\n\n<li>\u91cd\u542f Docker \u670d\u52a1\u8ba9\u914d\u7f6e\u751f\u6548\uff1asudo systemctl daemon-reload<br>sudo systemctl restart docker<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u65b9\u6848\u4e8c\uff1a\u914d\u7f6e Docker \u4ee3\u7406\uff08\u8fdb\u9636\u7a33\u59a5\u65b9\u6848\uff09<\/h4>\n\n\n\n<p><strong>\u5982\u679c\u4f60\u6709\u81ea\u5df1\u7684 VPN \u6216\u4ee3\u7406\u8282\u70b9<\/strong>\uff08\u6bd4\u5982\u4f60\u7684\u7269\u7406\u673a\u4e0a\u5f00\u4e86\u4ee3\u7406\uff0c\u6216\u8005\u5c40\u57df\u7f51\u5185\u6709\u4ee3\u7406\u670d\u52a1\u5668\uff09\uff0c\u8ba9 Docker \u8d70\u4ee3\u7406\u662f\u6700\u7a33\u7684\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u6ce8\u610f\uff1a<\/strong> Docker \u62c9\u53d6\u955c\u50cf\u4e0d\u8d70\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u7684\u4ee3\u7406\uff0c\u5fc5\u987b\u5355\u72ec\u914d\u7f6e systemd\u3002<\/p>\n<\/blockquote>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u521b\u5efa\u914d\u7f6e\u76ee\u5f55\uff1a<code>sudo mkdir -p \/etc\/systemd\/system\/docker.service.d<\/code><\/li>\n\n\n\n<li>\u521b\u5efa\u4ee3\u7406\u6587\u4ef6\uff1a<code>sudo nano \/etc\/systemd\/system\/docker.service.d\/http-proxy.conf<\/code><\/li>\n\n\n\n<li>\u7c98\u8d34\u4ee5\u4e0b\u5185\u5bb9\uff08<strong>\u5047\u8bbe\u4f60\u7684\u4ee3\u7406\u5728 192.168.1.100 \u7684 7890 \u7aef\u53e3<\/strong>\uff0c\u8bf7\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u4fee\u6539\uff09\uff1a<code>[Service]<br>Environment=\"HTTP_PROXY=http:\/\/192.168.1.100:7890\"<br>Environment=\"HTTPS_PROXY=http:\/\/192.168.1.100:7890\"<\/code><\/li>\n\n\n\n<li>\u91cd\u542f\u670d\u52a1\uff1a<code>sudo systemctl daemon-reload<\/code><br><code>sudo systemctl restart docker<\/code><\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u7b2c\u4e09\u90e8\u5206\uff1a\u6e17\u900f\u6d4b\u8bd5\u5e38\u7528 Docker \u64cd\u4f5c\u901f\u67e5<\/h3>\n\n\n\n<p>\u642d\u5efa\u597d\u73af\u5883\u540e\uff0c\u6211\u4eec\u6765\u5b9e\u6218\u3002\u4e0d\u8bb2\u865a\u7684\uff0c\u53ea\u8bb2\u4f60\u505a\u5b9e\u9a8c\u3001\u642d\u9776\u573a\u6700\u5e38\u7528\u7684\u547d\u4ee4\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. \u6982\u5ff5\u901f\u89e3<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u955c\u50cf (Image):<\/strong> \u76f8\u5f53\u4e8e\u201c\u5149\u76d8\u201d\u6216\u201c\u5b89\u88c5\u5305\u201d\u3002\uff08\u53ea\u8bfb\u7684\uff09<\/li>\n\n\n\n<li><strong>\u5bb9\u5668 (Container):<\/strong> \u76f8\u5f53\u4e8e\u201c\u8fd0\u884c\u8d77\u6765\u7684\u7cfb\u7edf\u201d\u3002\uff08\u53ef\u8bfb\u5199\u7684\uff09<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">2. \u53ea\u6709 5 \u4e2a\u547d\u4ee4\u7684\u201c\u751f\u5b58\u6307\u5357\u201d<\/h4>\n\n\n\n<p>\u5047\u8bbe\u6211\u4eec\u8981\u642d\u5efa\u4e00\u4e2a <code>nginx<\/code> web\u670d\u52a1\u5668\u505a\u6d4b\u8bd5\u3002<\/p>\n\n\n\n<p><strong>Step 1: \u8fd0\u884c\u5bb9\u5668 (Run)<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u683c\u5f0f: docker run -d -p [\u5bbf\u4e3b\u673a\u7aef\u53e3]:[\u5bb9\u5668\u5185\u90e8\u7aef\u53e3] --name [\u8d77\u4e2a\u540d\u5b57] [\u955c\u50cf\u540d]<br><br><code>sudo docker run -d -p 8080:80 --name my-web nginx<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>-d<\/code>: \u540e\u53f0\u8fd0\u884c\uff08\u4e0d\u5360\u7528\u4f60\u5f53\u524d\u7684\u7ec8\u7aef\uff09\u3002<\/li>\n\n\n\n<li><code>-p 8080:80<\/code>: \u6700\u5173\u952e\uff01\u628a\u4f60\u7684 Ubuntu \u7684 8080 \u7aef\u53e3\u6620\u5c04\u7ed9\u5bb9\u5668\u7684 80 \u7aef\u53e3\u3002<\/li>\n\n\n\n<li><strong>\u6d4b\u8bd5\uff1a<\/strong> \u6253\u5f00\u6d4f\u89c8\u5668\u8bbf\u95ee <code>http:\/\/localhost:8080<\/code>\uff0c\u4f60\u5e94\u8be5\u80fd\u770b\u5230 Nginx \u6b22\u8fce\u9875\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>Step 2: \u67e5\u770b\u6b63\u5728\u8fd0\u884c\u7684\u5bb9\u5668 (Ps)<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo docker ps<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f60\u80fd\u770b\u5230\u5bb9\u5668 ID\u3001\u72b6\u6001\u3001\u7aef\u53e3\u6620\u5c04\u60c5\u51b5\u3002\u5982\u679c\u4e0d\u52a0\u53c2\u6570\uff0c\u53ea\u663e\u793a\u8fd0\u884c\u4e2d\u7684\uff1b<code>docker ps -a<\/code> \u663e\u793a\u6240\u6709\u7684\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>Step 3: \u8fdb\u5165\u5bb9\u5668\u5185\u90e8 (Exec)<\/strong><\/p>\n\n\n\n<p>\u8fd9\u5c31\u597d\u6bd4 SSH \u8fde\u8fdb\u4e86\u4e00\u53f0\u865a\u62df\u673a\uff0c\u9002\u5408\u8fdb\u53bb\u4fee\u6539\u914d\u7f6e\u6587\u4ef6\u6216\u67e5\u770b Flag\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u683c\u5f0f:<code> docker exec -it [\u5bb9\u5668\u540d\/ID] \/bin\/bash<\/code><br><code>sudo docker exec -it my-web \/bin\/bash<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9000\u51fa\u5bb9\u5668\u8f93\u5165 <code>exit<\/code> \u5373\u53ef\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>Step 4: \u505c\u6b62\u4e0e\u542f\u52a8 (Stop\/Start)<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo docker stop my-web <\/code> # \u5173\u673a<br><code>sudo docker start my-web<\/code> # \u5f00\u673a<\/pre>\n\n\n\n<p>Step 5: \u5220\u9664\u5bb9\u5668 (Rm)<\/p>\n\n\n\n<p>\u505a\u5b8c\u5b9e\u9a8c\uff0c\u6e05\u7406\u73b0\u573a\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo docker stop my-web <\/code> # \u5148\u505c\u6b62<br><code>sudo docker rm my-web <\/code>\u00a0  # \u518d\u5220\u9664<br>\u5982\u679c\u60f3\u8fde\u955c\u50cf\u4e5f\u5220\u4e86\uff1a docker rmi nginx<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u7b2c\u56db\u90e8\u5206\uff1a\u5e38\u89c1\u201c\u5751\u70b9\u201d\u6392\u96f7\u6307\u5357<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u5751\u70b9 2\uff1a\u7aef\u53e3\u51b2\u7a81 (Bind for 0.0.0.0:xx failed)<\/h4>\n\n\n\n<p>\u73b0\u8c61\uff1a \u8fd0\u884c\u5bb9\u5668\u65f6\u62a5\u9519\uff1aport is already allocated\u3002<\/p>\n\n\n\n<p>\u539f\u56e0\uff1a \u4f60\u6307\u5b9a\u7684\u5bbf\u4e3b\u673a\u7aef\u53e3\uff08\u5192\u53f7\u5de6\u8fb9\u7684\u7aef\u53e3\uff09\u5df2\u7ecf\u88ab\u522b\u7684\u7a0b\u5e8f\u5360\u7528\u4e86\u3002<\/p>\n\n\n\n<p>\u89e3\u51b3\uff1a \u6362\u4e2a\u7aef\u53e3\u3002\u6bd4\u5982\u539f\u547d\u4ee4\u662f -p 80:80\uff0c\u6539\u6210 -p 8081:80 \u5373\u53ef\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5751\u70b9 3\uff1a\u540d\u5b57\u51b2\u7a81 (The container name &#8220;\/xxx&#8221; is already in use)<\/h4>\n\n\n\n<p>\u73b0\u8c61\uff1a \u518d\u6b21\u8fd0\u884c\u540c\u4e00\u4e2a docker run \u547d\u4ee4\u62a5\u9519\u3002<\/p>\n\n\n\n<p>\u539f\u56e0\uff1a \u5373\u4f7f\u5bb9\u5668\u505c\u6b62\u4e86\uff0c\u5b83\u8fd8\u5728\u90a3\u8eba\u7740\uff08\u7c7b\u4f3c\u5173\u673a\u7684\u7535\u8111\uff09\u3002\u4f60\u4e0d\u80fd\u521b\u5efa\u4e00\u4e2a\u540c\u540d\u7684\u65b0\u5bb9\u5668\u3002<\/p>\n\n\n\n<p>\u89e3\u51b3\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5220\u9664\u65e7\u7684\uff1a<code>docker rm [\u5bb9\u5668\u540d]<\/code><\/li>\n\n\n\n<li>\u6216\u8005\u7ed9\u65b0\u7684\u5bb9\u5668\u8d77\u4e2a\u4e0d\u540c\u7684\u540d\u5b57\uff1a<code>--name my-web-2<\/code><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u7b2c\u4e00\u90e8\u5206\uff1a\u6781\u7b80\u5b89\u88c5 Docker \u7b2c\u4e8c\u90e8\u5206\uff1a\u6838\u5fc3\u96be\u70b9\u2014\u2014\u89e3\u51b3\u201c\u62c9\u53d6\u4e0d\u5230\u955c\u50cf\u201d \u8fd9\u662f\u6240\u6709\u56fd\u5185 Docker \u521d\u5b66 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[11,12,13],"class_list":["post-36","post","type-post","status-publish","format-standard","hentry","category-linux","tag-docker","tag-linux","tag-ubuntu"],"_links":{"self":[{"href":"https:\/\/danchengjie.cn\/index.php\/wp-json\/wp\/v2\/posts\/36","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/danchengjie.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/danchengjie.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/danchengjie.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/danchengjie.cn\/index.php\/wp-json\/wp\/v2\/comments?post=36"}],"version-history":[{"count":5,"href":"https:\/\/danchengjie.cn\/index.php\/wp-json\/wp\/v2\/posts\/36\/revisions"}],"predecessor-version":[{"id":92,"href":"https:\/\/danchengjie.cn\/index.php\/wp-json\/wp\/v2\/posts\/36\/revisions\/92"}],"wp:attachment":[{"href":"https:\/\/danchengjie.cn\/index.php\/wp-json\/wp\/v2\/media?parent=36"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/danchengjie.cn\/index.php\/wp-json\/wp\/v2\/categories?post=36"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/danchengjie.cn\/index.php\/wp-json\/wp\/v2\/tags?post=36"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}