Docker架构

Docker简介

Docker是在Linux容器里面运行应用的开源工具,是一种轻量级虚拟机,Docker的好处在于“一次封装,到处运行”,既可以是一个应用,也可以是一套服务,甚至是一个完整的操作系统。

安装Docker

Docker支持在主流的操作系统平台上使用,包括Windows、Linux、MacOS等 这里用CentOS 7系统

(1)配置Docker自己的安装源

配置仓库

[root@xyue yum.repos.d] cat /etc/yum.repos.d/docker.repo 
[docker-repo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/7/
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg

yun安装Docker

[root@xyue yum.repos.d] systemctl start docker.service -y
[root@xyue yum.repos.d] systemctl enable docker.service

查看版本

[root@xyue yum.repos.d] docker version
Client:
 Version:      17.05.0-ce
 API version:  1.29
 Go version:   go1.7.5
 Git commit:   89658be
 Built:        Thu May  4 22:06:25 2017
 OS/Arch:      linux/amd64

Server:
 Version:      17.05.0-ce
 API version:  1.29 (minimum version 1.12)
 Go version:   go1.7.5
 Git commit:   89658be
 Built:        Thu May  4 22:06:25 2017
 OS/Arch:      linux/amd64
 Experimental: false

Docker镜像操作

1):搜索镜像

格式: docker search [服务名称]

以搜索apache为例

[root@xyue yum.repos.d] docker search apache
[root@xyue yum.repos.d] docker search apache
NAME                                           DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
tomcat                                         Apache Tomcat is an open source implementa...   1971      [OK]       
httpd                                          The Apache HTTP Server Project                  1885      [OK]       
cassandra                                      Apache Cassandra is an open-source distrib...   835       [OK]       
maven                                          Apache Maven is a software project managem...   649       [OK]       
solr                                           Solr is the popular, blazing-fast, open so...   565       [OK]       
zookeeper                                      Apache ZooKeeper is an open-source server ...   433       [OK]   
#第一列代表服务所在的库,DESCRIPTION是产品描述,数字代表星级星级越高这个镜像越稳定,OFFICIAL代表通过官方审核没问题

下载镜像

格式:docker pull [库名(就是第一列显示出来的就是库名)]

#我以第二个http为例(下载镜像时绝对不能出错,一定要对应上库名)
[root@xyue ~] docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
d660b1f15b9b: Pull complete 
aa1c79a2fa37: Pull complete 
f5f6514c0aff: Pull complete 
676d3dd26040: Pull complete 
4fdddf845a1b: Pull complete 
520c4b04fe88: Pull complete 
5387b1b7893c: Pull complete 
Digest: sha256:8c84e065bdf72b4909bd55a348d5e91fe265e08d6b28ed9104bfdcac9206dcc8
Status: Downloaded newer image for httpd:latest

#镜像信息查询在/var/lib/docker/image/overlay repositories.json可以用cat vi去进行查看
[root@xyue overlay]# cat repositories.json 
{"Repositories":{"hello-world":{"hello-world:latest":"sha256:2cb0d9787c4dd17ef9eb03e512923bc4db10add190d3f84af63b744e353a9b34","hello-world@sha256:4b8ff392a12ed9ea17784bd3c9a8b1fa3299cac44aca35a85c90c5e3c7afacdc":"sha256:2cb0d9787c4dd17ef9eb03e512923bc4db10add190d3f84af63b744e353a9b34"},"httpd":{"httpd:latest":"sha256:11426a19f1a28d6491041aef1e1a7a2dcaa188d0165ae495de7d8fc1bf3e164f","httpd@sha256:8c84e065bdf72b4909bd55a348d5e91fe265e08d6b28ed9104bfdcac9206dcc8":"sha256:11426a19f1a28d6491041aef1e1a7a2dcaa188d0165ae495de7d8fc1bf3e164f"}}}

#也可以用docker images
[root@xyue overlay] docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
httpd               latest              11426a19f1a2        9 days ago          178MB
hello-world         latest              2cb0d9787c4d        4 weeks ago         1.85kB

给镜像加标签可以在后续工作中方便试使用

格式:docker tag [镜像名称] 名称:[标签]

[root@xyue ~] docker tag httpd h1:h1
[root@xyue ~] docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
h1                  h1                  11426a19f1a2        9 days ago          178MB
httpd               latest              11426a19f1a2        9 days ago          178MB
#可以看到标签h1和httpd的IMAGE ID号是一样的因为他们是同一个镜像

#删除镜像标签用 docker rmi 名称:[标签]
[root@xyue ~] docker rmi h1:h1
Untagged: h1:h1
[root@xyue ~] docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
httpd               latest              11426a19f1a2        9 days ago          178MB

存入镜像和载入镜像

格式:docker save -o 存储文件名 存储镜像

[root@xyue ~] docker save -o /opt/hrrpd httpd
[root@xyue ~] cd /opt/
[root@xyue opt] ls
hrrpd

载入镜像就是从A机器里把镜像导入到B机器里面

格式:docker load < 存出的镜像 docker --input 存出的镜像

[root@xyue ~]# docker load < /opt/hrrpd 
Loaded image: httpd:latest

[root@xyue opt]# docker --input hrrpd