Docker简介&&安装

前言

本文整理自:http://www.docker.org.cn/book/docker以及自己进行操作的部分内容。

Docker是一个开源的引擎,可以轻松的为任何应用创建一个轻量级的、可移植的、自给自足的容器。开发者在笔记本上编译测试通过的容器可以批量地在生产环境中部署,包括VMs(虚拟机)、bare metal、OpenStack集群和其他的基础应用平台。

Docker通常用于如下的场景:

  • web应用的自动化打包和发布;
  • 自动化测试和持续集成、发布;
  • 在服务型环境中部署和调整数据库或其他的后台应用;
  • 从头编译或者扩展现有的OpenShift或Cloud Foundry平台来搭建自己的PaaS环境。

在CentOS7中安装Docker

依赖性检查

Docker需要一个64位系统的红帽系统,内核的版本必须大于3.10。可以用下面的命令来检查是否满足docker的要求。CentOS是红帽系统的开源社区版,所以基本一致。

安装Docker

方式一

sudo yum update # 确认yum包最新

curl -sSL https://get.docker.com/ | sh # 执行安装

sudo systemctl start docker.service # 启动docker

方式二

sudo yum update # 更新yum包

编辑docker.repo的源到/etc/yum.repo.d/

[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/7
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg

执行命令如下:

yum clean all
yum makecache
yum install docker-engine

然后启动服务。

确认安装完成

配置Docker用户组

为什么需要创建docker用户组?

Docker守候进程绑定的是一个unix socket,而不是TCP端口。这个套接字默认的属主是root,其他是用户可以使用sudo命令来访问这个套接字文件。因为这个原因,docker服务进程都是以root帐号的身份运行的。

为了避免每次运行docker命令的时候都需要输入sudo,可以创建一个docker用户组,并把相应的用户添加到这个分组里面。当docker进程启动的时候,会设置该套接字可以被docker这个分组的用户读写。这样只要是在docker这个组里面的用户就可以直接执行docker命令了。

操作步骤

使用sudo权限的账号登录系统,比如:root

创建docker分组,并且将相应的用户添加到这个分组里面

sudo usermod -aG docker your_username

退出,重新登录用户,以便权限生效。

使用添加到Docker的用户运行Docker命令。

docker run hello-world

此时出现的结果:

[hzhiping@slave01 root]$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
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.
    (amd64)
 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 ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

设置Docker服务自启动

[root@slave01 ~]# 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.

上面是老版本的CentOS,CentOS 7请使用:

systemctl enable docker.service

CentOS删除Docker

可以使用yum来删除docker。

列出docker包的具体的名字

$ yum list installed | grep docker
yum list installed | grep docker
docker-engine.x86_64 1.7.1-0.1.el7

删除docker

$ sudo yum -y remove docker-engine.x86_64 

备注:该命令只是删除docker运行环境,并不会删除镜像,容器,卷文件,以及用户创建的配置文件。

清除镜像和容器文件

$ rm -rf /var/lib/docker

然后手工查找并删除用户创建的配置文件。

坚壁清野

猜你喜欢

转载自www.cnblogs.com/hzhiping/p/10244214.html