Docker的安装及镜像的获取、查看、搜索、删除、创建
(1)docker简介:
起源
-
环境管理复杂 - 从各种OS到各种中间件到各种app, 一款产品能够成功作为开发者需要关心的东西太多,且难于管理,这个问题几乎在所有现代IT相关行业都需要面对。
-
云计算时代的到来 - AWS的成功, 引导开发者将应用转移到 cloud 上, 解决了硬件管理的问题,然而中间件相关的问题依然存在 (所以openstack HEAT和 AWS cloudformation 都着力解决这个问题)。开发者思路变化提供了可能性。
-
虚拟化手段的变化 - cloud 时代采用标配硬件来降低成本,采用虚拟化手段来满足用户按需使用的需求以及保证可用性和隔离性。然而无论是KVM还是Xen在 docker 看来,都在浪费资源,因为用户需要的是高效运行环境而非OS, GuestOS既浪费资源又难于管理, 更加轻量级的LXC更加灵活和快速
-
LXC的移动性 - LXC在 linux 2.6 的 kernel 里就已经存在了,但是其设计之初并非为云计算考虑的,缺少标准化的描述手段和容器的可迁移性,决定其构建出的环境难于迁移和标准化管理(相对于KVM之类image和snapshot的概念)。docker 就在这个问题上做出实质性的革新。这是docker最独特的地方。
-
Automating the packaging and deployment of applications
-
Creation of lightweight, private PAAS environments
-
Automated testing and continuous integration/deployment
-
Deploying and scaling web apps, databases and backend services
-
构建标准化的运行环境,现有的方案大多是在一个baseOS上运行一套puppet/chef,或者一个image文件,其缺点是前者需要base OS许多前提条件,后者几乎不可以修改(因为copy on write 的文件格式在运行时rootfs是read only的)。并且后者文件体积大,环境管理和版本控制本身也是一个问题。
-
PaaS环境是不言而喻的,其设计之初和dotcloud的案例都是将其作为PaaS产品的环境基础
-
因为其标准化构建方法(buildfile)和良好的REST API,自动测试和持续集成/部署能够很好的集成进来
-
因为LXC轻量级的特点,其启动快,而且docker能够只加载每个container变化的部分,这样资源占用小,能够在单机环境下与KVM之类的虚拟化方案相比能够更加快速和占用更少资源
(2) docker的安装
1.1 如果是Centos7,直接使用sudo yum install -y docker
1.2 如果是Centos6,sudo yum install -y http://mirrors.yun-idc.com/epel/6/i386/epel-release-6-8.noarch.rpm
sudo yum install -y docker-io
(3)docker的基本操作
1.1 系统条件,本系统使用的linux操作系统是Centos7_x64
1.2 启动docker,service docker start
1.3 演示从Docker Hub上下载最新版本的ubuntu
命令: sudo docker pull ubuntu,会有如下的信息提示:
[kass@localhost ~]$ sudo docker pull ubuntu
Using default tag: latest
Trying to pull repository docker.io/library/ubuntu ... latest: Pulling from library/ubuntu
c2a0e656b13b: Pull complete
a1918483ff2d: Pull complete
19f3070b96b2: Pull complete
0930cd7fba20: Pull complete
8e5b7248472b: Pull complete
Digest: sha256:46fb5d001b88ad904c5c732b086b596b92cfb4a4840a3abd0e35dbb6870585e4
Status: Downloaded newer image for docker.io/ubuntu:latest
1.4 使用下载的镜像创建一个容器,在其中运行bash应用,具体执行结果如下:
[kass@localhost ~]$ sudo docker run -t -i ubuntu /bin/bash
[sudo] password for kass:
Usage of loopback devices is strongly discouraged for production use. Either use `--storage-opt dm.thinpooldev` or use `--storage-opt dm.no_warn_on_loop_devices=true` to suppress this warning.
1.5 使用docker images 命令查看本地主机上已有的镜像,及添加一个新的ubuntu:latest镜像标签
具体的操作命令及信息如下:
[kass@localhost ~]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
docker.io/ubuntu latest 8e5b7248472b 2 weeks ago 122 MB
[kass@localhost ~]$ sudo docker tag docker.io/ubuntu:latest ubuntu:latest
[kass@localhost ~]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu latest 8e5b7248472b 2 weeks ago 122 MB
docker.io/ubuntu latest 8e5b7248472b 2 weeks ago 122 MB
1.6 查看镜像的详细信息
[kass@localhost ~]$ sudo docker inspect 8e5b7248472b
[sudo] password for kass:
[
{
"Id": "8e5b7248472b1cf370e8b181c357148a5aff33c0b8da2ba133701f599de15a84",
"RepoTags": [
"docker.io/ubuntu:latest",
"ubuntu:latest"
],
"RepoDigests": [],
"Parent": "0930cd7fba20b2f6a84c3abc1918b4e159b123c1a3b787c0a9bea874e4f7e40b",
"Comment": "",
"Created": "2016-05-27T14:15:02.359284074Z",
"Container": "b8bd6a8e8874a87f626871ce370f4775bdf598865637082da2949ee0f4786432",
"ContainerConfig": {
"Hostname": "914cf42a3e15",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) CMD [\"/bin/bash\"]"
],
"Image": "b873f334fa5259acb24cf0e2cd2639d3a9fb3eb9bafbca06ed4f702c289b31c0",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"DockerVersion": "1.9.1",
"Author": "",
"Config": {
"Hostname": "914cf42a3e15",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [],
"Cmd": [
"/bin/bash"
],
"Image": "b873f334fa5259acb24cf0e2cd2639d3a9fb3eb9bafbca06ed4f702c289b31c0",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"Architecture": "amd64",
"Os": "linux",
"Size": 0,
"VirtualSize": 121963951,
"GraphDriver": {
"Name": "devicemapper",
"Data": {
"DeviceId": "6",
"DeviceName": "docker-253:1-34403904-8e5b7248472b1cf370e8b181c357148a5aff33c0b8da2ba133701f599de15a84",
"DeviceSize": "107374182400"
}
}
}
]
1.7 详细搜索
sudo docker -f {{".Architecture"}} 8e5 //8e5就是8e5b7248472b 是镜像的ID
1.8 搜索镜像
[kass@localhost ~]$ sudo docker search mysql
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/mysql MySQL is a widely used, open-source relati... 2493 [OK]
docker.io docker.io/mysql/mysql-server Optimized MySQL Server Docker images. Crea... 158 [OK]
docker.io docker.io/centurylink/mysql Image containing mysql. Optimized to be li... 45 [OK]
docker.io docker.io/sameersbn/mysql 36 [OK]
docker.io docker.io/appcontainers/mysql Centos/Debian/Ubuntu Based Customizable My... 7 [OK]
docker.io docker.io/marvambass/mysql MySQL Server based on Ubuntu 14.04 6 [OK]
docker.io docker.io/alterway/mysql Docker Mysql 2 [OK]
docker.io docker.io/azukiapp/mysql Docker image to run MySQL by Azuki - http:... 2 [OK]
docker.io docker.io/drupaldocker/mysql MySQL for Drupal 2 [OK]
docker.io docker.io/yfix/mysql Yfix docker built mysql 2 [OK]
docker.io docker.io/bahmni/mysql Mysql container for bahmni. Contains the ... 1 [OK]
docker.io docker.io/phpmentors/mysql MySQL server image 1 [OK]
docker.io docker.io/sin30/mysql MySQL images with my own config files. 1 [OK]
docker.io docker.io/tetraweb/mysql 1 [OK]
docker.io docker.io/ahmet2mir/mysql This is a Debian based image with MySQL se... 0 [OK]
docker.io docker.io/akilli/mysql akilli/base based MySQL image 0 [OK]
docker.io docker.io/cloudposse/mysql Improved `mysql` service with support for ... 0 [OK]
docker.io docker.io/dockerizedrupal/mysql docker-mysql 0 [OK]
docker.io docker.io/jeffutter/mysql-no_volumes Images based on the official mysql images ... 0 [OK]
docker.io docker.io/kardasz/mysql Debian 8, MySQL 5.7 0 [OK]
docker.io docker.io/lancehudson/docker-mysql MySQL is a widely used, open-source relati... 0 [OK]
docker.io docker.io/nanobox/mysql MySQL service for nanobox.io 0 [OK]
docker.io docker.io/projectomakase/mysql Docker image for MySQL 0 [OK]
docker.io docker.io/tozd/mysql MySQL (MariaDB fork) Docker image. 0 [OK]
docker.io docker.io/vukor/mysql Build for MySQL. Project available on http... 0 [OK]
1.9 删除镜像
1.9.1 通过name删除镜像
[kass@localhost ~]$ sudo docker rmi docker.io/ubuntu
Untagged: docker.io/ubuntu:latest
[kass@localhost ~]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu latest 8e5b7248472b 2 weeks ago 122 MB
1.9.2 通过镜像ID删除文件
这种方式,会先尝试删除所有指向该镜像的标签,然后删除该镜像本身
[kass@localhost ~]$ sudo docker rmi docker.io/ubuntu
2.0 创建镜像
[kass@localhost ~]$ sudo docker run -ti ubuntu /bin/bash
Usage of loopback devices is strongly discouraged for production use. Either use `--storage-opt dm.thinpooldev` or use `--storage-opt dm.no_warn_on_loop_devices=true` to suppress this warning.
root@bfda0c922239:/# touch test
root@bfda0c922239:/# exit
exit
[kass@localhost ~]$ sudo docker commit -m "Add a new file" -a "Docker Newbee" 8e5b7248472b test
Error response from daemon: no such id: 8e5b7248472b
[kass@localhost ~]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu latest 8e5b7248472b 2 weeks ago 122 MB
[kass@localhost ~]$ sudo docker commit -m "Add a new file" -a "Docker Newbee" bfda0c922239 test
4521126f01b2bdf6816929a82cd428bfc2da50119f4c1afc19f751fec71d510c
[kass@localhost ~]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
test latest 4521126f01b2 6 seconds ago 122 MB
ubuntu latest 8e5b7248472b 2 weeks ago 122 MB
2.1 存储镜像、载入镜像
2.1.1 存储镜像
[kass@localhost ~]$ sudo docker save -o test.zip test
2.1.2 载入镜像
[kass@localhost ~]$ sudo docker load < test.zip
或者
[kass@localhost ~]$ sudo docker load --input test.zip
note:使用sudo的时候如果提示permission denied,将当前用户切换为root身份,
然后编辑/etc/sudoers 文件, 在root ALL=(ALL) ALL下面添加 kass ALL=(ALL) ALL