概況
掌握 Docker 在Centos的安裝:
- 操作系統(tǒng):Win 10 家庭中文版
- 虛擬機(jī)軟件:Oracle VirtualBox 5.0.26 及以上
- 虛擬機(jī)操作系統(tǒng): Centos 7
- Docker 最新版本的安裝
安裝VirtualBox 和 Centos7
參考網(wǎng)頁(yè):Win10 安裝 VirtualBox
參考網(wǎng)頁(yè):VirtualBox 安裝 Centos7 虛擬機(jī)
安裝 Docker
參考頁(yè)面 - Centos 安裝 Docker:Installation on Centos
參考頁(yè)面 - 二進(jìn)制包安裝方式:Installation from binaries
前置條件:
- 64位操作系統(tǒng)
- Linux 內(nèi)核是3.10或者更高(安裝Centos7的原因)
- iptables version 1.4 or later
- Git version 1.7 or later
- procps (or similar provider of a “ps” executable)
- XZ Utils 4.9 or later
- a properly mounted cgroupfs hierarchy
上述內(nèi)容只是讓我們清楚Docker的相關(guān)依賴(lài)固灵,在具體安裝的時(shí)候永品,我們可以通過(guò) yum 安裝的方式,自動(dòng)解決依賴(lài)包的下載和安裝。
安裝步驟
- 更新當(dāng)前軟件包到最新
[root@localhost ~]# yum update
- 添加 Docker 的 yum 倉(cāng)庫(kù)
[root@localhost ~]# 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
[root@localhost ~]# du -sh /etc/yum.repos.d/docker.repo
4.0K /etc/yum.repos.d/docker.repo
- 安裝 Docker 軟件包
[root@localhost ~]# yum install -y docker-engine
- 安裝成功之后校驗(yàn)檢查
- 查看 docker 軟件包是否已安裝包(
yum list installed | grep docker
) - 查看 docker 軟件包有哪些執(zhí)行文件(
ls /usr/bin/*docker*
) - 查看 docker 系統(tǒng)服務(wù)配置文件是否存在(
ls /usr/lib/systemd/system/docker.service -l
) - 查看 docker 版本(
docker version
) - 查看 docker 默認(rèn)配置文件目錄是否存在(
ls /etc/docker/ -l
) - 查看 docker 默認(rèn)運(yùn)行時(shí)目錄是否存在(
ls /var/lib/docker/ -l
) - 查看系統(tǒng)的邏輯卷組有哪些(
vgs -a
) - 查看系統(tǒng)的 DeviceMapper 機(jī)制映射的邏輯設(shè)備有哪些(
ls /dev/centos/ -l
:docker默認(rèn)方式會(huì)創(chuàng)建自己的邏輯設(shè)備) - 查看網(wǎng)絡(luò)接口配置信息(
ifconfig
:docker用到網(wǎng)絡(luò)接口) - 查看 iptables 規(guī)則配置信息(
iptables --list
:docker用到iptables)
下面是執(zhí)行結(jié)果
[root@localhost ~]# yum list installed | grep docker
docker-engine.x86_64 1.12.2-1.el7.centos @dockerrepo
docker-engine-selinux.noarch 1.12.2-1.el7.centos @dockerrepo
[root@localhost ~]# docker version
Client:
Version: 1.12.2
API version: 1.24
Go version: go1.6.3
Git commit: bb80604
Built:
OS/Arch: linux/amd64
Cannot connect to the Docker daemon. Is the docker daemon running on this host?
[root@localhost ~]# ls /usr/lib/systemd/system/docker.service -l
-rw-r--r--. 1 root root 974 9月 28 01:22 /usr/lib/systemd/system/docker.service
[root@localhost ~]# ls /usr/bin/*docker*
/usr/bin/docker /usr/bin/dockerd
/usr/bin/docker-containerd /usr/bin/docker-proxy
/usr/bin/docker-containerd-ctr /usr/bin/docker-runc
/usr/bin/docker-containerd-shim
[root@localhost ~]# ls /etc/docker/ -l
ls: 無(wú)法訪問(wèn)/etc/docker/: 沒(méi)有那個(gè)文件或目錄
[root@localhost ~]# ls /var/lib/docker/ -l
ls: 無(wú)法訪問(wèn)/var/lib/docker/: 沒(méi)有那個(gè)文件或目錄
[root@localhost ~]# vgs -a
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- 19.51g 40.00m
[root@localhost ~]# ls /dev/centos/ -l
總用量 0
lrwxrwxrwx. 1 root root 7 10月 16 23:18 root -> ../dm-0
lrwxrwxrwx. 1 root root 7 10月 16 23:18 swap -> ../dm-1
[root@localhost ~]# ls /dev/mapper/ -l
總用量 0
lrwxrwxrwx. 1 root root 7 10月 16 23:18 centos-root -> ../dm-0
lrwxrwxrwx. 1 root root 7 10月 16 23:18 centos-swap -> ../dm-1
crw-------. 1 root root 10, 236 10月 16 23:18 control
[root@localhost ~]# ifconfig
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.104 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::a00:27ff:fe2d:62c7 prefixlen 64 scopeid 0x20<link>
ether 08:00:27:2d:62:c7 txqueuelen 1000 (Ethernet)
RX packets 1084 bytes 119437 (116.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 778 bytes 113093 (110.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost ~]# iptables --list
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
- 啟動(dòng)服務(wù)
- 設(shè)置開(kāi)機(jī)自動(dòng)啟動(dòng) docker 服務(wù)(
systemctl enable docker
) - 手工啟動(dòng) docker 服務(wù)(
systemctl start docker
) - 查看 docker 版本(
docker version
) - 顯示 docker 詳細(xì)信息(
docker info
)
[root@localhost ~]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@localhost ~]# systemctl start docker
[root@localhost ~]# ps -ef| grep docker
root 2160 1 3 00:01 ? 00:00:00 /usr/bin/dockerd
root 2163 2160 0 00:01 ? 00:00:00 docker-containerd -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --shim docker-containerd-shim --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --runtime docker-runc
root 2278 2003 0 00:01 pts/0 00:00:00 grep --color=auto docker
[root@localhost ~]# docker version
Client:
Version: 1.12.2
API version: 1.24
Go version: go1.6.3
Git commit: bb80604
Built:
OS/Arch: linux/amd64
Server:
Version: 1.12.2
API version: 1.24
Go version: go1.6.3
Git commit: bb80604
Built:
OS/Arch: linux/amd64
[root@localhost ~]# docker info
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 1.12.2
Storage Driver: devicemapper
Pool Name: docker-253:0-67182400-pool
Pool Blocksize: 65.54 kB
Base Device Size: 10.74 GB
Backing Filesystem: xfs
Data file: /dev/loop0
Metadata file: /dev/loop1
Data Space Used: 11.8 MB
Data Space Total: 107.4 GB
Data Space Available: 17.52 GB
Metadata Space Used: 581.6 kB
Metadata Space Total: 2.147 GB
Metadata Space Available: 2.147 GB
Thin Pool Minimum Free Space: 10.74 GB
Udev Sync Supported: true
Deferred Removal Enabled: false
Deferred Deletion Enabled: false
Deferred Deleted Device Count: 0
Data loop file: /var/lib/docker/devicemapper/devicemapper/data
WARNING: Usage of loopback devices is strongly discouraged for production use. Use `--storage-opt dm.thinpooldev` to specify a custom block storage device.
Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata
Library Version: 1.02.107-RHEL7 (2016-06-09)
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: null host bridge overlay
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Security Options: seccomp
Kernel Version: 3.10.0-327.36.2.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 1.522 GiB
Name: localhost.localdomain
ID: VFSO:QWR6:ICKT:QGXY:UQXT:5VKN:L6E3:7JBE:OBVO:SQFK:PU2V:IBMS
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Insecure Registries:
127.0.0.0/8
- 看看Docker 啟動(dòng)之后有什么變化
- 查看 docker 版本(
docker version
) - 查看 docker 默認(rèn)配置文件目錄是否存在(
ls /etc/docker/ -l
) - 查看 docker 默認(rèn)運(yùn)行時(shí)目錄是否存在(
ls /var/lib/docker/ -l
) - 查看系統(tǒng)的 DeviceMapper 機(jī)制映射的邏輯設(shè)備有哪些(
ls /dev/centos/ -l
) - 查看 docker 啟動(dòng)后主程序的進(jìn)程文件[.pid文件]和本地套接字[.sock文件](
ls /run/docker.* -l
) - 查看 docker 啟動(dòng)之后子進(jìn)程文件和對(duì)應(yīng)的本地套接字(
ls /run/docker/ -l
) - 查看網(wǎng)絡(luò)接口配置信息(
ifconfig
) - 查看 iptables 規(guī)則配置信息(
iptables --list
)
命令運(yùn)行結(jié)果如下:
[root@localhost ~]# ls /etc/docker/ -l
總用量 4
-rw-------. 1 root root 244 10月 17 00:01 key.json
[root@localhost ~]# ls /var/lib/docker/ -l
總用量 0
drwx------. 2 root root 6 10月 17 00:01 containers
drwx------. 4 root root 40 10月 17 00:01 devicemapper
drwx------. 3 root root 25 10月 17 00:01 image
drwxr-x---. 3 root root 18 10月 17 00:01 network
drwx------. 2 root root 6 10月 17 00:01 swarm
drwx------. 2 root root 6 10月 17 00:01 tmp
drwx------. 2 root root 6 10月 17 00:01 trust
drwx------. 2 root root 24 10月 17 00:01 volumes
[root@localhost ~]# ls /dev/mapper/ -l
總用量 0
lrwxrwxrwx. 1 root root 7 10月 17 11:32 centos-root -> ../dm-0
lrwxrwxrwx. 1 root root 7 10月 17 11:32 centos-swap -> ../dm-1
crw-------. 1 root root 10, 236 10月 17 11:32 control
lrwxrwxrwx. 1 root root 7 10月 17 11:43 docker-253:0-67182400-pool -> ../dm-2
[root@localhost ~]# ls /run/docker.* -l
-rw-r--r--. 1 root root 4 10月 17 11:43 /run/docker.pid
srw-rw----. 1 root docker 0 10月 17 11:43 /run/docker.sock
[root@localhost ~]# ls /run/docker/ -l
總用量 0
drwx------. 3 root root 100 10月 17 11:43 libcontainerd
drw-------. 2 root root 60 10月 17 11:43 libnetwork
[root@localhost ~]# ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0
ether 02:42:e4:89:85:b8 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.161 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::a00:27ff:fe2d:62c7 prefixlen 64 scopeid 0x20<link>
ether 08:00:27:2d:62:c7 txqueuelen 1000 (Ethernet)
RX packets 1621 bytes 111194 (108.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 118 bytes 17109 (16.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost ~]# iptables --list
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
DOCKER-ISOLATION all -- anywhere anywhere
DOCKER all -- anywhere anywhere
ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED
ACCEPT all -- anywhere anywhere
ACCEPT all -- anywhere anywhere
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain DOCKER (1 references)
target prot opt source destination
Chain DOCKER-ISOLATION (1 references)
target prot opt source destination
RETURN all -- anywhere anywhere
- 運(yùn)行某個(gè)鏡像
[root@localhost ~]# docker run --rm hello-world
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/