CentOS下Docker安装与配置指南
采用YUM方式进行安装
操作前需具备root权限或拥有sudo执行权限
首先将系统中的yum软件包更新至最新版本
$ sudo yum update
接着配置并添加Docker的官方YUM软件源
$ 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相关软件包
$ sudo yum install docker-engine
安装完成后,启动Docker后台守护进程
$ 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 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/
采用官方提供的脚本进行安装
同样需要root权限或sudo权限支持
第一步也是将yum包更新到最新状态
$ sudo yum update
执行Docker官方的安装脚本程序
$ curl -sSL https://get.docker.com/ | sh
该脚本会自动添加docker.repo仓库源并完成Docker的安装
脚本执行完毕后,启动Docker守护进程
$ sudo service docker start
通过运行测试镜像来检查Docker安装是否正确无误
$ sudo docker run hello-world
建立名为docker的Unix用户组
Docker守护进程默认绑定在Unix socket而非TCP端口上。由于Unix socket默认归root用户及sudo组成员所有,因此Docker守护进程必须以root身份运行。
To avoid having to use sudo when you use the docker command, create a Unix group called docker and add users to it. When the docker daemon starts, it makes the ownership of the Unix socket read/writable by the docker group.
为避免日常使用docker命令时频繁输入sudo,可创建一个名为docker的Unix用户组,并将目标用户加入其中。当Docker守护进程启动后,该组成员将获得Unix socket的读写权限。
具体创建docker用户组并添加用户的操作如下:
1. 确保拥有root或sudo权限 2. 创建docker用户组并将指定用户添加至该组
sudo usermod -aG docker your_username
3. 退出当前会话并重新登录your_username账户;若已在your_username下,建议再次注销重登以确保权限生效。 4. 验证此时运行docker命令已无需sudo权限
$ docker run hello-world
设置Docker服务随系统开机自动启动
$ sudo chkconfig docker on