2.Docker 的第一次亲密接触

概况

掌握 Docker 在Centos的安装:

  • 操作系统:Win 10 家庭中文版
  • 虚拟机软件:Oracle VirtualBox 5.0.26 及以上
  • 虚拟机操作系统: Centos 7
  • Docker 最新版本的安装

安装VirtualBox 和 Centos7

参考网页:Win10 安装 VirtualBox
参考网页:VirtualBox 安装 Centos7 虚拟机

安装 Docker

参考页面 - Centos 安装 Docker:Installation on Centos
参考页面 - 二进制包安装方式:Installation from binaries

前置条件:

  • 64位操作系统
  • Linux 内核是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

上述内容只是让我们清楚Docker的相关依赖,在具体安装的时候,我们可以通过 yum 安装的方式,自动解决依赖包的下载和安装。

安装步骤

  1. 更新当前软件包到最新
[root@localhost ~]# yum update
  1. 添加 Docker 的 yum 仓库
[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
  1. 安装 Docker 软件包
[root@localhost ~]# yum install -y docker-engine
  1. 安装成功之后校验检查
  • 查看 docker 软件包是否已安装包(yum list installed | grep docker
  • 查看 docker 软件包有哪些执行文件(ls /usr/bin/*docker*
  • 查看 docker 系统服务配置文件是否存在(ls /usr/lib/systemd/system/docker.service -l
  • 查看 docker 版本(docker version
  • 查看 docker 默认配置文件目录是否存在(ls /etc/docker/ -l
  • 查看 docker 默认运行时目录是否存在(ls /var/lib/docker/ -l
  • 查看系统的逻辑卷组有哪些(vgs -a
  • 查看系统的 DeviceMapper 机制映射的逻辑设备有哪些(ls /dev/centos/ -l:docker默认方式会创建自己的逻辑设备)
  • 查看网络接口配置信息(ifconfig:docker用到网络接口)
  • 查看 iptables 规则配置信息(iptables --list:docker用到iptables)

下面是执行结果

[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: 无法访问/etc/docker/: 没有那个文件或目录
[root@localhost ~]# ls /var/lib/docker/ -l
ls: 无法访问/var/lib/docker/: 没有那个文件或目录
[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
  1. 启动服务
  • 设置开机自动启动 docker 服务(systemctl enable docker
  • 手工启动 docker 服务(systemctl start docker
  • 查看 docker 版本(docker version
  • 显示 docker 详细信息(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
  1. 看看Docker 启动之后有什么变化
  • 查看 docker 版本(docker version
  • 查看 docker 默认配置文件目录是否存在(ls /etc/docker/ -l
  • 查看 docker 默认运行时目录是否存在(ls /var/lib/docker/ -l
  • 查看系统的 DeviceMapper 机制映射的逻辑设备有哪些(ls /dev/centos/ -l
  • 查看 docker 启动后主程序的进程文件[.pid文件]和本地套接字[.sock文件](ls /run/docker.* -l
  • 查看 docker 启动之后子进程文件和对应的本地套接字(ls /run/docker/ -l
  • 查看网络接口配置信息(ifconfig
  • 查看 iptables 规则配置信息(iptables --list

命令运行结果如下:

[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
  1. 运行某个镜像
[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/
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,699评论 6 513
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,124评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 167,127评论 0 358
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,342评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,356评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,057评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,654评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,572评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,095评论 1 318
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,205评论 3 339
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,343评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,015评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,704评论 3 332
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,196评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,320评论 1 271
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,690评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,348评论 2 358

推荐阅读更多精彩内容