美文网首页docker
2.Docker 的第一次亲密接触

2.Docker 的第一次亲密接触

作者: andy0898 | 来源:发表于2016-10-17 15:07 被阅读471次

    概况

    掌握 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/
    

    相关文章

      网友评论

      • f0c08489aca6:为什么不直接用Docker for Windows 呢?Linux虚拟机是多余的吗?

      本文标题:2.Docker 的第一次亲密接触

      本文链接:https://www.haomeiwen.com/subject/ijpiyttx.html