您的位置:首页 > 运维架构 > Docker

Centos7中安装docker

2016-04-21 14:10 716 查看


CentOS

Docker 可以运行在CentOS 7系列,也可以安装在其他二进制兼容 EL7发行版的系统中,比如 科学Linux(Scientific Linux)可以成功安装,但是Docker没有做测试或者支持 Docker 在这些发行版上安装。

这一节会指导你去安装使用 Docker-managed 发布包和安装机制。确保使用的这些包是Docker
最新版的。如果你想要使用 CentOS-managed 包,请查询 CentOS 的文档 


前提

Docker要求64位的系统,对CentOS版本不做要求,并且你的内核必须是3.10及以上。

检查你当前内核的版本,可以在终端中使用 
uname
-r
 去显示你的内核版本: 
$ uname -r
3.10.0-229.el7.x86_64


最后,推荐完全更新你的系统,请牢记被打补丁来修复了任何潜在的内核 bugs。任何被上报的内核 bugs 应该已经完全使用最新的内核软件包被修复了。


安装

有两种方法安装 Docker Engine:

第一种:可以使用 
yum
 管理器安装,或者使用 
curl
  
get.docker.com
 ;

第二种:运行一个通过 
yum
 包管理的安装脚本安装;


Install with yum

登陆你的机器使用 
sudo
 or 
root
 特权.

确保你的yum包是最新的:

$ sudo yum update


添加yum repo.

$ sudo tee /etc/yum.repos.d/docker.repo <<-'EOF'
[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/$releasever/
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg
EOF


安装 Docker package.

$ sudo yum install docker-engine
 

启动Docker daemon.

$ sudo service docker
start
 

通过在容器中运行一个测试镜像确保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 itto 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/[/code] 


使用脚本安装

 登陆你的机器使用sudo或者root特权

确保你的yum是最新的

$ sudo yum update
 

运行Docker安装脚本

$ curl -sSL https://get.docker.com/ | sh
 

这个脚本添加docker.repo仓库,然后安装Docker

启动Docker daemon

$ sudo service docker
start
 

通过在容器中运行一个测试镜像确保docker安装成功

$ sudo docker run hello-world
 



创建一个docker组

Docker daemon绑定了一个Unix socket而不是TCP端口。默认root用户拥有Unix socket,其他用户可以通过sudo来访问它。出于这个原因,docker daemon总是以root用户身份运行。

当你用docker命令时,为了避免使用sudo命令,我们可以创建一个叫做 docker 的组 ,并且将用户添加进去。当再次启动docker daemon时,使得Unix socket 读写的所有权以docker组的身份运行。

Warning: 在进行docker操作时,这个docker 组和root用户是等价的;关于如何影响系统安全想要获取更多详细信息,可以参照Docker
Daemon Attack Surface。

接下来创建docker组并添加用户user:

登陆系统Centos。

创建docker组并添加用户

$sudo
usermod -aG docker your_username
 

  注销系统然后重新登录系统,确保你的用户正在运行并且有正确的权限

通过使用不加sudo的docker命令运行一个容器来验证以上几步的正确性:

$ docker run hello-world
 


设置docker daemon开机启动

为了确保Docker在系统启动时运行,可以这样做:
[code]$ sudo systemctl enable docker
 [/code]


卸载

你可以使用yum命令卸载Docker

列出已经安装的软件包。

$ yum list installed | grep docker
yum list installed | grep docker
docker-engine.x86_64   1.7.1-1.el7 @/docker-engine-1.7.1-1.el7.x86_64.rpm


删除软件包.

$ sudo yum -y remove docker-engine.x86_64 

这个命令不会删除主机上的镜像、容器、卷、用户创建的配置文件

删除所有镜像、容器和卷,可以使用如下命令:

$ rm -rf /var/lib/docker
 

查找删除用户创建的任何docker文件。.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: