Docker在線安裝(包括私有鏡像源配置)及使用

centos docker安裝

  • 系統(tǒng)版本:cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
  • 安裝需要的軟件包
sudo yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2

安裝成功結(jié)果


image.png
  • 配置源地址
sudo yum-config-manager \
    --add-repo \
    https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

執(zhí)行結(jié)果


image.png
  • 安裝 Docker Engine-Community (選擇特定版本進(jìn)行安裝)
# 查看docker可安裝版本
yum list docker-ce --showduplicates | sort -r
  • 選擇版本進(jìn)行安裝
#其中 3:20.10.0-3.el7 為上個(gè)步驟查出來(lái)的版本列表
sudo yum install docker-ce-3:20.10.0-3.el7 docker-ce-cli-3:20.10.0-3.el7 containerd.io
  • 啟動(dòng)docker
sudo systemctl start docker
  • 查看docker版本
docker --version

docker開(kāi)通遠(yuǎn)程api端口

詳細(xì)參考 : http://www.itdecent.cn/p/4f49c5b40d15

ubuntu docker 安裝 (版本信息: Ubuntu 22.04 LTS)

  • 更新 apt
# 挨個(gè)執(zhí)行安裝

 # update 命令只會(huì)獲得系統(tǒng)上所有包的最新信息,并不會(huì)下載或者安裝任何一個(gè)包
 sudo apt update
 # 更新已安裝軟件包的命令 
 sudo apt upgrade
 sudo apt full-upgrade
  • 添加Docker庫(kù)
# 安裝必要的證書(shū)
sudo apt install apt-transport-https ca-certificates curl software-properties-common gnupg lsb-release
# 添加Docker官方GPG密鑰
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
# 添加docker官方庫(kù)
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
#更新源列表 ,如果出現(xiàn) ' E: Method https has died unexpectedly!' 問(wèn)題,參考下面 ' ubuntu添加存儲(chǔ)庫(kù)執(zhí)行 apt update 異常'
sudo apt update

上面兩個(gè)步驟,“添加Docker官方GPG密鑰”、“添加docker官方庫(kù)” 如果出現(xiàn)443等連接超時(shí)問(wèn)題,找下文“問(wèn)題 - docker相關(guān)文件下載超時(shí)” 進(jìn)行配置解決

  • 安裝Docker
#安裝 Docker CE
sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin
#查看docker版本
apt-cache madison docker-ce
#選擇上述列表中任意版本安裝
sudo apt install docker-ce=5:20.10.16~3-0~ubuntu-jammy docker-ce-cli=5:20.10.16~3-0~ubuntu-jammy containerd.io
  • 檢驗(yàn)是否安裝成功
# 查看docker安裝狀態(tài)
systemctl status docker
# 查看docker 容器進(jìn)程
docker ps -a
  • 修改 Docker APT源
    如果碰到連接 download.docker.com錯(cuò)誤,錯(cuò)誤信息如下
Could not connect to download.docker.com:443 (13.224.163.26), 
connection timed out Could not connect to download.docker.com:443 (13.224.163.81), 
connection timed out Could not connect to download.docker.com:443 (13.224.163.23), 

可以修改 docker 源,源信息

# 阿里源
echo \
"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
  • 安裝docker-compose
sudo apt install docker-compose

常規(guī)使用方法

# docker pull 容器名:版本號(hào)
docker pull redis:6.0.20

如果出現(xiàn) “error pulling image configuration: download failed after attempts=6: dial tcp 185.45.7.189:443: i/o timeout” 問(wèn)題,是因?yàn)閲?guó)內(nèi)主流的鏡像源都不能使用了,可以通過(guò)以下步驟,快速的搭建自己的鏡像源

  1. 注冊(cè) Cloudflare 賬號(hào),地址:https://dash.cloudflare.com/
  2. 注冊(cè)成功后,進(jìn)入首頁(yè),找到 Workers & Pages , 點(diǎn)擊 pages綁定github賬號(hào)
  3. fork CF-Workers-docker.io 項(xiàng)目到自己的github賬戶(剛剛綁定的賬號(hào))
  4. 刷新2中的頁(yè)面,選擇3中fork的項(xiàng)目,按步驟設(shè)置,無(wú)需做其他配置變更
  5. 配置成功后的界面如下,標(biāo)注地方即新的鏡像地址,配置成功等幾分鐘后即生效,將地址配置進(jìn)docker鏡像,即可使用
  6. vim /etc/docker/daemon.json 進(jìn)行編輯,格式如下
    {
    "registry-mirrors": ["你的鏡像地址"]
    }
  7. systemctl daemon-reload && systemctl restart docker


    image.png
  • 常規(guī)安裝 -- redis
# 創(chuàng)建需要掛載的文件目錄
# 創(chuàng)建本地目錄
mkdir -pv /opt/db_data/redis/data
# 創(chuàng)建容器
# 參數(shù)說(shuō)明  -d 后臺(tái)運(yùn)行,運(yùn)行成功返回ID ; -p 端口號(hào)映射 ; --restart always docker重啟時(shí)該容器會(huì)重啟; -v 掛載目錄,宿主機(jī)目錄:容器安裝成功后內(nèi)部目錄; --requirepass redis參數(shù),redis的密碼  
docker run -d -p 6379:6379 --name redis --restart always -v /opt/db_data/redis/data/:/data redis:6.0.20 --requirepass eb887f0a2794
  • 常規(guī)安裝 -- mysql
# 創(chuàng)建目錄
mkdir -p ~/data/var/data/mysql8/db
# 創(chuàng)建容器
docker run \
--restart always \
--name=mysql8.0.20 \
-e MYSQL_ROOT_PASSWORD=mypassword \
-v ~/data/var/data/mysql8/db:/var/lib/mysql \
-p 3306:3306 \
-d mysql:8.0.20 --default-authentication-plugin=mysql_native_password --character-set-server=UTF8MB4
  • 常規(guī)安裝 -- nginx
# 創(chuàng)建目錄,用于掛載
mkdir -pv /opt/nginx/logs
mkdir -pv /opt/nginx/html
mkdir -pv /opt/nginx/logs
mkdir -pv /var/www/html
# 創(chuàng)建容器
# 1 :--privileged=true 表示以root權(quán)限,讀取宿主機(jī)的掛載目錄
# 2 :/var/www/html 作為前端項(xiàng)目訪問(wèn)根目錄
docker run -d \
--restart always \
--name nginx-train36 -p 8097:8097 \
-v /opt/nginx-train36/nginx.conf:/etc/nginx/nginx.conf \
-v /opt/nginx-train36/conf.d:/etc/nginx/conf.d \
-v /opt/nginx-train36/logs:/var/log/nginx \
-v /opt/nginx-train36/html:/usr/share/nginx/html \
-v /var/www/html:/var/www/html \
--privileged=true nginx
  • 常規(guī)安裝 -- postgre
# 1下載鏡像
docker pull postgres:12.10
# 2 創(chuàng)建掛載目錄
mkdir -pv /opt/volume/postgre_data
# 3 創(chuàng)建容器
docker run -it -d --name postgresql \
-v /opt/volume/postgre_data:/var/lib/postgresql/data \
-p 5432:5432 -e POSTGRES_PASSWORD=我的密碼 postgres:12.10
# 4 連接
賬號(hào) postgres 密碼 我的密碼
  • 關(guān)于網(wǎng)絡(luò)
# 查看docker網(wǎng)絡(luò)
docker network ls
# 查看某個(gè)網(wǎng)絡(luò)容器情況
docker network inspect bridge

問(wèn)題

  • docker相關(guān)文件下載超時(shí)
    執(zhí)行 “ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg” 報(bào)錯(cuò):
    curl: (28) Failed to connect to download.docker.com port 443 after 131114 ms: Connection timed out
    gpg: no valid OpenPGP data found.

用以下方法更換源

sudo curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
  • ubuntu添加存儲(chǔ)庫(kù)執(zhí)行 apt update 異常 (Method https has died unexpectedly!)
    添加“echo "deb [arch=(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu \(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null” 報(bào)錯(cuò)如下
    image.png

    解決辦法
#臨時(shí)
sudo GNUTLS_CPUID_OVERRIDE=0x1 apt-get update
#永久
# 將 GNUTLS_CPUID_OVERRIDE=0x1 添加到環(huán)境變量中
vim /etc/environment
# 環(huán)境變量生效
source /etc/profile
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容