CentOS 7 安裝 Docker 和 Docker-Compose

環(huán)境檢查

進入 root

為了愉快,以下所有操作都是以 root 用戶執(zhí)行

sudo -i

查看 Linux 發(fā)行版本

[root@localhost ~]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

查看內(nèi)核版本

Docker 要求 CentOS 系統(tǒng)的內(nèi)核版本高于 3.10

[root@localhost /]# uname -r
3.10.0-123.el7.x86_64

結(jié)論

發(fā)行版本為 CentOS 7雏节,滿足 Docker 環(huán)境需求,接下來用 yum 安裝 docker-ce。

注意:

3.10 版本的內(nèi)核喧伞,可能無法正常運行 18.06.x 及以上版本的 docker(解決方法:升級內(nèi)核或者降低 docker 版本),可能報下面的錯誤:

docker: Error response from daemon: OCI runtime create failed: container_linux.go:344: starting container process caused "process_linux.go:293: copying bootstrap data to pipe caused "write init-p: broken pipe"": unknown.

安裝 Docker

# 更新到最新 yum 包
yum update -y

# 卸載舊版本(如果安裝過舊版本的話)
yum remove docker docker-common docker-selinux docker-engine docer-io

# 安裝需要的軟件包
# yum-util 提供 yum-config-manager 功能, 另外兩個是 devicemapper 驅(qū)動依賴
yum install -y yum-utils device-mapper-persistent-data lvm2

# 設(shè)置 yum 源
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

# 查看所有倉庫中所有 docker 版本等舔,并選擇特定版本安裝
yum list docker-ce --showduplicates | sort -r

* updates: centos.ustc.edu.cn
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror, langpacks

* extras: centos.ustc.edu.cn
docker-ce.x86_64 3:18.09.2-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.1-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.0-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.3.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.2.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.1.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.0.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.03.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 18.03.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.12.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.12.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.09.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.09.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.2.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.3.ce-1.el7 docker-ce-stable
docker-ce.x86_64 17.03.2.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable

* base: centos.ustc.edu.cn
Available Packages

# 由于 repo 中默認只開啟 stable 的倉庫牺荠,故這里安裝的是最新穩(wěn)定版(18.09.2)
# 由于內(nèi)核是 3.10 無法正常運行 18.06.x 及以上版本的 docker翁巍,所以不這么安裝
# yum install -y docker-ce

# 經(jīng)過測試發(fā)現(xiàn),3.10 內(nèi)核可以運行 18.03.1.ce
# yum install -y <FQPN>
yum install -y docker-ce-18.03.1.ce

# 啟動并加入開機啟動
systemctl start docker
systemctl enable docker

# 驗證安裝是否成功(有 client 和 service 兩部分表示 docker 安裝啟動都成功了)
docker version

Client:
Version: 18.03.1-ce
API version: 1.37
Go version: go1.9.5
Git commit: 9ee9f40
Built: Thu Apr 26 07:20:16 2018
OS/Arch: linux/amd64
Experimental: false
Orchestrator: swarm

Server:
Engine:
Version: 18.03.1-ce
API version: 1.37 (minimum version 1.12)
Go version: go1.9.5
Git commit: 9ee9f40
Built: Thu Apr 26 07:23:58 2018
OS/Arch: linux/amd64
Experimental: false

更改配置(如果有必要)

sudo vim /lib/systemd/system/docker.service

ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock --graph=/home/docker/data

# 你可能會用到代理(如果原先沒有Environment休雌,可以添加)
Environment="HTTP_PROXY=http://0.0.0.0:8123" "HTTPS_PROXY=http://0.0.0.0:8123" "NO_PROXY=localhost,127.0.0.1,::1"

重啟 docker 使配置生效

sudo systemctl daemon-reload
sudo systemctl restart docker

安裝 Docker-Compose

通過訪問 https://github.com/docker/compose/releases/latest 得到最新的 docker-compose 版本(例如:1.23.2)灶壶,然后執(zhí)行一下命令安裝 docker-compose

# 下載最新版本的 docker-compose 到 /usr/bin 目錄下
curl -L https://github.com/docker/compose/releases/download/1.23.2/docker-compose-`uname -s`-`uname -m` -o /usr/bin/docker-compose

# 給 docker-compose 授權(quán)
chmod +x /usr/bin/docker-compose

FAQ

1、yum update -y 失敗

[root@localhost ~]# yum update -y
Loaded plugins: fastestmirror, langpacks
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os error was
14: curl#6 - "Could not resolve host: mirrorlist.centos.org; Unknown error"


 One of the configured repositories failed (Unknown),
 and yum doesn't have enough cached data to continue. At this point the only
 safe thing yum can do is fail. There are a few ways to work "fix" this:

     1. Contact the upstream for the repository and get them to fix the problem.

     2. Reconfigure the baseurl/etc. for the repository, to point to a working
        upstream. This is most often useful if you are using a newer
        distribution release than is supported by the repository (and the
        packages for the previous distribution release still work).

     3. Disable the repository, so yum won't use it by default. Yum will then
        just ignore the repository until you permanently enable it again or use
        --enablerepo for temporary usage:

            yum-config-manager --disable <repoid>

     4. Configure the failing repository to be skipped, if it is unavailable.
        Note that yum will try to contact the repo. when it runs most commands,
        so will have to try and fail each time (and thus. yum will be be much
        slower). If it is a very temporary problem though, this is often a nice
        compromise:

            yum-config-manager --save --setopt=<repoid>.skip_if_unavailable=true

Cannot find a valid baseurl for repo: base/7/x86_64

如果遇到上面的錯誤杈曲,通過修改 /etc/resolv.conf驰凛,添加如下內(nèi)容,再次執(zhí)行 yum update -y

nameserver 8.8.8.8
search localdomain

2鱼蝉、yum install -y docker-ce-18.03.1.ce 失敗

Transaction check error:
  file /usr/bin/docker from install of docker-ce-18.03.1.ce-1.el7.centos.x86_64 conflicts with file from package docker-common-2:1.12.6-68.gitec8512b.el7.centos.x86_64
  file /usr/bin/docker-containerd from install of docker-ce-18.03.1.ce-1.el7.centos.x86_64 conflicts with file from package docker-common-2:1.12.6-68.gitec8512b.el7.centos.x86_64
  file /usr/bin/docker-containerd-shim from install of docker-ce-18.03.1.ce-1.el7.centos.x86_64 conflicts with file from package docker-common-2:1.12.6-68.gitec8512b.el7.centos.x86_64
  file /usr/bin/dockerd from install of docker-ce-18.03.1.ce-1.el7.centos.x86_64 conflicts with file from package docker-common-2:1.12.6-68.gitec8512b.el7.centos.x86_64

卸載舊版本的包

yum erase docker-common-2:1.12.6-68.gitec8512b.el7.centos.x86_64

再次安裝 docker

yum install -y docker-ce-18.03.1.ce
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末洒嗤,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子魁亦,更是在濱河造成了極大的恐慌渔隶,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,635評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件洁奈,死亡現(xiàn)場離奇詭異间唉,居然都是意外死亡,警方通過查閱死者的電腦和手機利术,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評論 3 399
  • 文/潘曉璐 我一進店門呈野,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人印叁,你說我怎么就攤上這事被冒。” “怎么了轮蜕?”我有些...
    開封第一講書人閱讀 168,083評論 0 360
  • 文/不壞的土叔 我叫張陵昨悼,是天一觀的道長。 經(jīng)常有香客問我跃洛,道長率触,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,640評論 1 296
  • 正文 為了忘掉前任汇竭,我火速辦了婚禮葱蝗,結(jié)果婚禮上穴张,老公的妹妹穿的比我還像新娘。我一直安慰自己两曼,他們只是感情好皂甘,可當我...
    茶點故事閱讀 68,640評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著合愈,像睡著了一般叮贩。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上佛析,一...
    開封第一講書人閱讀 52,262評論 1 308
  • 那天益老,我揣著相機與錄音,去河邊找鬼寸莫。 笑死捺萌,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的膘茎。 我是一名探鬼主播桃纯,決...
    沈念sama閱讀 40,833評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼披坏!你這毒婦竟也來了态坦?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,736評論 0 276
  • 序言:老撾萬榮一對情侶失蹤棒拂,失蹤者是張志新(化名)和其女友劉穎伞梯,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體帚屉,經(jīng)...
    沈念sama閱讀 46,280評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡谜诫,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,369評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了攻旦。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片喻旷。...
    茶點故事閱讀 40,503評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖牢屋,靈堂內(nèi)的尸體忽然破棺而出且预,到底是詐尸還是另有隱情,我是刑警寧澤烙无,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布锋谐,位于F島的核電站,受9級特大地震影響皱炉,放射性物質(zhì)發(fā)生泄漏怀估。R本人自食惡果不足惜狮鸭,卻給世界環(huán)境...
    茶點故事閱讀 41,870評論 3 333
  • 文/蒙蒙 一合搅、第九天 我趴在偏房一處隱蔽的房頂上張望多搀。 院中可真熱鬧,春花似錦灾部、人聲如沸康铭。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽从藤。三九已至,卻和暖如春锁蠕,著一層夾襖步出監(jiān)牢的瞬間夷野,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評論 1 272
  • 我被黑心中介騙來泰國打工荣倾, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留悯搔,地道東北人。 一個月前我還...
    沈念sama閱讀 48,909評論 3 376
  • 正文 我出身青樓舌仍,卻偏偏與公主長得像妒貌,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子铸豁,可洞房花燭夜當晚...
    茶點故事閱讀 45,512評論 2 359

推薦閱讀更多精彩內(nèi)容