官網(wǎng)文檔:https://docs.docker.com/engine/installation/linux/centos/ ,本文大部分是照搬官方文檔寫的,如果你英文還不錯(cuò),那么就直接移步官方文檔吧,如果你英文實(shí)在是不行,那就勉強(qiáng)看一下本人這生澀的翻譯~
以下操作均在root用戶下完成
docker的安裝要求64位系統(tǒng)且內(nèi)核版本大于3.10。所以如果是centos的話,必須安裝CentOS7.0或以上版本。
我們這里使用的是CentOS7.2 mininul。
uname -r
3.10.0-327.28.3.el7.x86_64
安裝docker前執(zhí)行一下全系統(tǒng)的軟件版本升級(jí):
yum -y update
1.配置yum軟件庫(kù)
為保證安裝的成功,首先使用yum update更新Yum包,表示我的好多yum包都需要更新,1500+的包,如果你像我一樣好久沒(méi)有更新過(guò),那就耐心等候吧。
然后在yum軟件庫(kù)中新增docker的配置:
1
2
3
4
5
6
7
8
|
# tee /etc/yum.repos.d/docker.repo <<-'EOF' [dockerrepo] name=Docker Repository baseurl=https://yum.dockerproject.org/repo/main/centos/7/ enabled=1 gpgcheck=1 gpgkey=https://yum.dockerproject.org/gpg EOF |
2.安裝Docker
有了yum軟件庫(kù)的配置之后,安裝也變得異常的簡(jiǎn)單,只需要以下一句即可:
# yum install docker-engine
3.啟動(dòng)Docker
一切就緒之后,使用start命令來(lái)啟動(dòng)Docker守護(hù)進(jìn)程:
# service docker start
4.輸出hello-world
程序員貌似跟hello-world有仇,有事兒沒(méi)事就打印人家一下,玩docker咱們當(dāng)前也不例外,先來(lái)個(gè)hello-world吧,這里的基本原理是利用人家已經(jīng)寫好的hello-world鏡像,下載到本地,然后把他運(yùn)行起來(lái)~
使用以下命令:
# docker run hello-world
然后控制端會(huì)輸出類似于如下的信息,就證明我們的docker環(huán)境安裝成功了~
在這里,我第一次失敗了~顯示:
1
2
3
|
Unable to find image 'hello-world:latest' locally docker: Error response from daemon: Get https://registry-1.docker.io/v2/library/hello-world/manifests/latest: Get https://auth.docker.io/token?scope=repository%3Alibrary%2Fhello-world%3Apull&service=registry.docker.io: net/http: TLS handshake timeout. See 'docker run --help'. |
然后我又來(lái)了一次就好了,應(yīng)該是墻的原因吧,看著是網(wǎng)絡(luò)訪問(wèn)失敗了~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world c04b14da8d14: Pull complete Digest: sha256:0256e8a36e2070f7bf2d0b0763dbabdd67798512411de4cdcf9431a1feb60fd9 Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker Hub account: https://hub.docker.com For more examples and ideas, visit: https://docs.docker.com/engine/userguide/ |
設(shè)置為自啟動(dòng):
chkconfig docker on
調(diào)整docker數(shù)據(jù)目錄:
把一個(gè)獨(dú)立的數(shù)據(jù)分區(qū)設(shè)置為docker數(shù)據(jù)目錄,需手工把docker原目錄的數(shù)據(jù)都移到新的存儲(chǔ)分區(qū)上去,然后以新的存儲(chǔ)分區(qū)掛載到/var/lib/docker目錄下。
service docker stop
拷數(shù)據(jù)及掛分區(qū):
service docker start
4、創(chuàng)建一個(gè)專用的docker group
docker是需要使用root權(quán)限運(yùn)行的,但仍然可以通過(guò)創(chuàng)建一個(gè)專用的用戶組的方式,讓一個(gè)具備sudo權(quán)限的普通用戶管理docker服務(wù)。
# groupadd docker
# usermod -aG docker bjxtb
退出當(dāng)前會(huì)話,重新登錄后使用bjxtb直接管理docker:
$ docker run hello-world
運(yùn)行一個(gè) Docker 容器:
1
2
|
[root@localhost ~]# docker run -i -t centos /bin/bash [root@dbf66395436d /]# |
我們可以看到,CentOS 容器已經(jīng)被啟動(dòng),并且我們得到了 bash 提示符。在 docker 命令中我們使用了 “-i 捕獲標(biāo)準(zhǔn)輸入輸出”和 “-t 分配一個(gè)終端或控制臺(tái)”選項(xiàng)。若要斷開(kāi)與容器的連接,輸入 exit。
1
2
3
4
5
|
[root@cd05639b3f5c /]# cat /etc/RedHat-release CentOSLinux release 7.0.1406(Core) [root@cd05639b3f5c /]#exit exit [root@localhost ~]# |
我們還可以搜索基于 Fedora 和 Ubuntu 操作系統(tǒng)的容器。
1
2
|
[root@localhost ~]# docker search ubuntu [root@localhost ~]# docker search fedora |
顯示當(dāng)前正在運(yùn)行容器的列表