如需轉載,標記出處
本次我們將下載一個 Docker 鏡像,從鏡像中啟動容器
上一章,安裝 Docker 時,獲得兩個主要組件:
-
Docker 客戶端
-
Docker 守護進程(有時稱為“服務器”或“引擎”)
守護進程實現 Docker Remote API。 在默認的 Linux 安裝中,客戶端通過位于 /var/run/docker.sock
的本地 IPC/Unix 套接字與守護進程通信。
用 docker version
命令測試客戶端和守護進程是否正在運行并可以相互通信
從遠處拉取鏡像,使用容器
映像包含足夠的操作系統 (OS),以及運行其設計的任何應用程序所需的所有代碼和依賴項。
從Docker Hub 拉取最新版本的 ubuntu
鏡像到本地
將鏡像(一般在github上)放到 Docker 主機(本地這邊)稱為“拉取”。
其實就是下載
docker pull ubuntu:latest
(如果拉取失敗是十有八九是沒設置 Docker 的代理,看下面第一個問題集)
運行 docker image ls
命令以查看剛剛拉取的映像
docker container run
命令從中啟動容器。
docker container run -it ubuntu:latest /bin/bash root@d08b290c8882:/#
shell 提示都發生了變化
因為 shell 現在連接了新容器 shell 上
按下 Ctrl-PQ
退出容器
docker container ls查看容器狀態
docker container ls CONTAINER ID ? IMAGE ? ? ? ? ? COMMAND ? ? ? CREATED ? ? ? ? STATUS ? ? ? ? PORTS ? ? NAMES db489e5f4d94 ? ubuntu:latest ? "/bin/bash" ? 40 seconds ago ? Up 40 seconds ? ? ? ? ? ? wizardly_jackson d08b290c8882 ? ubuntu:latest ? "/bin/bash" ? 7 minutes ago ? Up 7 minutes ? ? ? ? ? ? festive_shannon
前面步驟中的容器仍在運行,用 docker container exec
命令將 shell 連接到正在運行的容器。
docker container exec -it wizardly_jackson bash
使用 docker container stop
和 docker container rm
命令停止容器/關閉容器
比如:
admin123@admin-virtual-machine:~$ docker container kill wizardly_jackson wizardly_jackson
從github上拉取到本地,手動構建容器
上面一部分和這部分有區別嗎
肯定的
Docker hub是專門的 容器鏡像倉庫,里面的鏡像是 預先構建好的,由官方或社區成員提前創建、打包好并上傳,可以直接用
從 Git 倉庫克隆一個應用程序,檢查其 Dockerfile,容器化,然后將其作為容器運行
git clone https://github.com/nigelpoulton/psweb.git
(如果報錯看問題集4)
admin123@admin-virtual-machine:~/learn$ ls psweb admin123@admin-virtual-machine:~/learn$ cd psweb admin123@admin-virtual-machine:~/learn/psweb$ ls app.js Dockerfile package.json README.md views
cd 進入目錄需要保證 ls 后出現 Dockerfile,Dockerfile 是一個純文本文檔,描述了如何構建 Docker 鏡像。 使用 docker image build
命令,按照 Dockerfile 中的說明創建新的映像
admin123@admin-virtual-machine:~/learn/psweb$ cat Dockerfile # Test web-app to use with Pluralsight courses and Docker Deep Dive book FROM alpine ? LABEL maintainer="nigelpoulton@hotmail.com" ? # Install Node and NPM RUN apk add --update nodejs npm curl ? # Copy app to /src COPY . /src ? WORKDIR /src ? # Install dependencies RUN npm install ? EXPOSE 8080
構建一個名字叫 test
的容器
docker image build -t test:latest .
上面的不行換這個
docker buildx build --build-arg http_proxy=http://你的代理ip:端口號\--build-arg https_proxy=你的代理ip:端口號 \-t test:latest .
運行容器
docker container run -d \--name web1 \-p 8080:8080 \test:latest
訪問 http://127.0.0.1:8080/
就可以看到
Hello Docker learners!!! Check out my other books Quick Start KubernetesThe Kubernetes BookAI ExplainedBe careful. The last time I updated the packages in this app was Dec 2024.
BUG報錯問題集
1.Error response from daemon: Get "https://registry-1.docker.io/v2/": net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
網絡問題,輸入ifconfig在ens33這個網卡下面沒有看到ipv4地址,說明網卡掉了
sudo ifconfig ens33 down sudo ifconfig ens33 up
我也不知道為啥虛擬機的網卡掉線是家常便飯,不過我的問題還是沒解決又碰到了下面的報錯
4.6補充:如果你一直在這邊卡著,網絡問題也解決了,剩下的問題就是代理問題了,我昨天寫到這里排查半天,查了兩本教程書籍,不同種拉取鏡像的方法都不行,今天除了代理的問題解決的,
我自己的ubuntu虛擬機的代理不知道啥時候也掉線了
1.永久設置代理(如果已經有這一步跳過即可)
編輯 ~/.bashrc
或 ~/.zshrc
文件:
sudo vim ~/.bashrc
在文件末尾添加:(這個只是 參考我也不知道你用的哪個代理,如果是nat用的是主機的ip,橋接就是ifcomfig查虛擬機自己的ip,端口號看使用的哪個軟件用的)
唯一需要注意的是下面不是我打錯,沒有https://
除了ip和端口號需要根據自身情況改其他的不用
export http_proxy="http://127.0.0.1:xxxx" export https_proxy="http://127.0.0.1:xxxx"
保存后運行:
source ~/.bashrc
虛擬機的代理配置好,docker的也要配置
配置過程如下:
2.創建docker代理配置文件:
編輯 http-proxy.conf
文件:
sudo vim /etc/systemd/system/docker.service.d/http-proxy.conf
在文件中加入代理環境變量:(這里還要細分你的虛擬機時)
[Service] Environment="HTTP_PROXY=http://127.0.0.1:xxxx/" Environment="HTTPS_PROXY=http://127.0.0.1:xxxx/"
2.Error response from daemon: Get "https://registry-1.docker.io/v2/": context deadline exceeded
解決方案
檢查 Docker 配置文件中的 --registry-mirror 設置: 如果你希望使用鏡像源,可以將該配置項移到 /etc/docker/daemon.json
中,而不是通過命令行啟動時設置。這樣可以避免與命令行啟動參數沖突。
打開 /etc/docker/daemon.json
并添加以下內容:
{"registry-mirrors": ["https://mirrors.ustc.edu.cn"] }
清除命令行中的 --registry-mirror 參數: 修改完 /etc/docker/daemon.json
后,刪除或注釋掉啟動時使用的 --registry-mirror 參數。
你可以編輯 Docker 服務文件來移除這個啟動參數:
sudo systemctl edit docker
在打開的編輯器中刪除或注釋掉 --registry-mirror
參數(如果存在),保存并退出。
重載并重啟 Docker 服務:
sudo systemctl daemon-reload sudo systemctl restart docker
3.*啟動docker失敗 Active: failed *
按照經驗,一般都是在配置文件里面寫的參數有問題,把改的東西還原回來再重啟就行了
上面的話還是不行,再看下面的一種法子
查看日志:
sudo journalctl -u docker.service -n 50
其中有一句:
unable to configure the Docker daemon with file /etc/docker/daemon.json: the following directives are specified both as a flag and in the configuration file.
分析:
錯誤的根本原因是 /etc/docker/daemon.json
配置文件中有一些選項是重復定義的,既通過配置文件設置,又通過命令行參數傳遞。這導致了 Docker 啟動失敗。
把文件中的內容全部刪除,日志還是顯示說重復了,最初能正常運行的時候這個文件就是空白的。
查看 Docker 服務的啟動參數:
ps aux | grep dockerd
我的顯示結果是這樣的:
admin123@admin-virtual-machine:~$ ps aux | grep dockerdroot 11349 0.1 0.4 1895628 69920 ? Ssl 19:02 0:00 /usr/bin/dockerd --registry-mirror=https://mirrors.ustc.edu.cn admin123 11613 0.0 0.0 12000 720 pts/0 S+ 19:03 0:00 grep --color=auto dockerd
dockerd 啟動時包含了 --registry-mirror=https://mirrors.ustc.edu.cn
參數。這意味著在啟動 Docker 時通過命令行顯式指定了 --registry-mirror
參數,可能與 daemon.json 文件中的配置發生了沖突。
解決方案:
將該配置項移至 /etc/docker/daemon.json
:
{"registry-mirrors": ["https://mirrors.ustc.edu.cn"] }
然后清除命令行中的 --registry-mirror 參數:
sudo systemctl edit docker
刪除掉 --registry-mirror
參數,保存并退出。
重新加載 Docker 服務并重啟:
sudo systemctl daemon-reload sudo systemctl restart docker
4.fatal: 無法訪問 'https://github.com/nigelpoulton/psweb.git/':Couldn't connect to server
Git 無法連接到 GitHub,最常見原因是 沒有正確配置代理
git config --global http.proxy http://127.0.0.1:xxxx
git config --global https.proxy http://127.0.0.1:xxxx
如果還是不行ifconfig看網卡掉線沒,這個bug老朋友了,是不是來一下
sudo git clone https://github.com/nigelpoulton/psweb.git 正克隆到 'psweb'... remote: Enumerating objects: 85, done. remote: Counting objects: 100% (39/39), done. remote: Compressing objects: 100% (19/19), done. remote: Total 85 (delta 28), reused 21 (delta 19), pack-reused 46 (from 2) 展開對象中: 100% (85/85), 17.12 KiB | 922.00 KiB/s, 完成.