Docker学习笔记1:CentOS7 下安装Docker

本文内容摘自官网:https://docs.docker.com/engine/installation/linux/centos/#/create-a-docker-group

注:本文是介绍Linux 上的分布式版本CentOs上安装.

一、安装前的准备工作

Docker 需要安装在64位系统的CentOS上,而且linux内核至少在3.10版本以上,这个版本的内核在CenOS7上运行. 所以只要安装一个CenOS7 64位的系统就可以了.作为学习,大家只需安装个虚拟机就OK.

可以通过uname -r 来查看linux内核版本.

[root@localhost ~]# uname -r
3.10.0-327.el7.x86_64
最后,建议你全面更新你的系统. 请记住,你已经完全修复任何潜在的内核bug.

二、安装Docker

有两种方式来安装Docker,这里只介绍其中的一种,通过yum来安装Docker.

1、用一个用户登录你的机器,这个用户必须拥有sudo或root权限.

2、为了确保你的yum包是最新的,用下面脚本做下更新.

[root@localhost ~]# sudo yum update
3、加入yum repo
$ sudo 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
4、安装Docker包
$ sudo yum install docker-engine
5、启动Docker守护进程
$ sudo service docker start
6、通过运行容器中的一个测试image,来验证你的Docker安装正确.
$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
    latest: Pulling from hello-world
    a8219747be10: Pull complete
    91c95931e552: Already exists
    hello-world:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.
    Digest: sha256:aa03e5d0d5553b4c3473e89c8619cf79df368babd1.7.1cf5daeb82aab55838d
    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.
            (Assuming it was not already locally available.)
     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

    For more examples and ideas, visit:
     http://docs.docker.com/userguide/
至此,Docker就已安装好了.

三、自动启动Docker

若要保证,你开机时,自动启动Docker,可以执行如下的命令:

[root@localhost ~]# sudo chkconfig docker on
Note: Forwarding request to 'systemctl enable docker.service'.
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

四、Docker 卸载

1、列出你已安装的Docker package.

[root@localhost ~]# yum list installed | grep docker
docker-engine.x86_64                   1.12.1-1.el7.centos             @dockerrepo
docker-engine-selinux.noarch           1.12.1-1.el7.centos             @dockerrepo
2 、移除这个package
$ sudo yum -y remove docker-engine.x86_64
这个命令没有移除images、containers、volumes或者你主机上用户创建的配置文件.
[root@localhost ~]# ll /var/lib/docker/
total 4
drwx------. 5 root root 4096 8月  23 10:03 containers
drwx------. 5 root root   50 8月  23 09:30 devicemapper
drwx------. 3 root root   25 8月  23 09:30 image
drwxr-x---. 3 root root   18 8月  23 09:30 network
drwx------. 2 root root    6 8月  23 09:30 swarm
drwx------. 2 root root    6 8月  23 09:30 tmp
drwx------. 2 root root    6 8月  23 09:30 trust
drwx------. 2 root root   24 8月  23 09:30 volumes

3、要删除所有的images、containers、volumes,运行如下命令.

$ rm -rf /var/lib/docker
4、查找和删除任何用户创建的配置文件.