(二)、Docker 快速入门

文档:https://docs.docker.com/install/linux/docker-ce/centos/
中文文档:https://docs.docker-cn.com/engine/installation/linux/docker-ce/centos/#prerequisites

1、CentOS6.5安装Docker

Docker使用EPEL发布,RHEL系的OS首先要确保已经持有EPEL仓库,否则先检查OS的版本,然后安装相应的EPEL包。

yum install -y epel-release
yum install -y docker-io

安装后的配置文件:/etc/sysconfig/docker
启动Docker后台服务:service docker start
docker version验证

2、CentOS7安装Docker

yum安装gcc相关:

yum -y install gcc
yum -y install gcc-c++

卸载旧版本:

yum -y remove docker docker-common docker-selinux docker-engine
yum remove docker 
                  docker-client 
                  docker-client-latest 
                  docker-common 
                  docker-latest 
                  docker-latest-logrotate 
                  docker-logrotate 
                  docker-selinux 
                  docker-engine-selinux 
                  docker-engine

安装需要的软件包:

yum install -y yum-utils device-mapper-persistent-data lvm2

设置stable镜像仓库:

yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

更新yum软件包索引:

yum makecache fast

安装DOCKER CE:

yum -y install docker-ce

启动Docker:

systemctl start docker

测试是否安装成功:

docker version

配置镜像加速

mkdir -p /etc/docker
vim  /etc/docker/daemon.json
systemctl daemon-reload
systemctl restart docker

daemon.json:

#网易云
{"registry-mirrors": ["http://hub-mirror.c.163.com"] }

 #阿里云
{
  "registry-mirrors": ["https://{阿里云上查看}.mirror.aliyuncs.com"]
}	 

卸载:

systemctl stop docker 
yum -y remove docker-ce
rm -rf /var/lib/docker
3、快速开始

Docker运行Hello-World镜像:

docker run hello-world

输出信息

#在本地找不到Hello-world镜像
Unable to find image 'hello-world:latest' locally
#从远程仓库中拉
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete 
Digest: sha256:49a1c8800c94df04e9658809b006fd8a686cab8028d33cfba2cc049724254202
Status: Downloaded newer image for hello-world:latest

#代表Docker环境安装、运行、配置完成
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/
4、Docker Run

Docker Run执行流程:

  1. Docker查询镜像
    1. 本地找到则使用本地镜像
    2. 本地未找到去远程仓库查找镜像
  2. 找到镜像后以镜像生成容器进行运行
5、Docker运行原理

Docker是一个Client-Server结构的系统,Docker守护进程运行在主机上, 然后通过Socket连接从客户端访问,守护进程从客户端接受命令并管理运行在主机上的容器。 容器,是一个运行时环境,就是Docker Log 上的集装箱。

为什么Docker比较比VM快?

  1. docker有着比虚拟机更少的抽象层。由亍docker不需要Hypervisor实现硬件资源虚拟化,运行在docker容器上的程序直接使用的都是实际物理机的硬件资源。因此在CPU、内存利用率上docker将会在效率上有明显优势。

  2. docker利用的是宿主机的内核,而不需要Guest OS。因此,当新建一个容器时,docker不需要和虚拟机一样重新加载一个操作系统内核。仍而避免引寻、加载操作系统内核返个比较费时费资源的过程,当新建一个虚拟机时,虚拟机软件需要加载Guest OS,返个新建过程是分钟级别的。而docker由于直接利用宿主机的操作系统,则省略了返个过程,因此新建一个docker容器只需要几秒钟。

原文地址:https://www.cnblogs.com/SimpleWu/p/13384938.html