Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口。 Docker的面世让我们解决很多工程上面的问题,下面来看一下Docker的特性和优点
`特性`* 仓库 - 顾名思义,就是存放docker的一个整体仓库,可以通过redis、mysql、nginx等进行分类,也可以通过工程a1、a2、a3进行分类。* 镜像 - 运行环境的静态体现,就是整合好但是没起来的“运行环境”。* 容器 - 通过启动镜像跑起来的“运行环境”,这个时候就是一个装在容器里的小型操作系统。`优点`* 隔离性 - (分离测试环境) 虚拟机时代经常会遇到测试环境公用一套无法独立测试而导致多人同时开发阻塞在测试环境上。* 持续集成 - (快速扩容)只需要简单的导入导出操作,既可快速搭建第二套环境,还有比这更方便的么?
一.仓库
1.下载仓库
由于docker镜像源在海外,需要借助来进行加速
➜ docker pull centosUsing default tag: latestlatest: Pulling from library/centos45a2e645736c: Pull completeDigest: sha256:6e45de12a92ce6fcc0e3d0ffca8000833675f96679b3f1ecf71485c339826f27Status: Downloaded newer image for centos:latest
二.镜像
1.查看镜像
➜ docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEubuntu latest 104bec311bcd 2 weeks ago 129 MB
2.创建镜像
创建镜像有3种方法
1.基于已有镜像的容器创建
参考保存镜像
例子
2.基于本地模板的导入
下载模板
➜ cat ubuntu-14.04-x86_64-minimal.tar.gz | docker import - ubuntu:14.04
3.基于Dockerfile创建(存处镜像到本地文件)
➜ docker save -o ubuntu_14.04.tar ubuntu:latest➜ docker ubuntu:latest➜ docker load --input ubuntu_14.04.tar or➜ docker load < ubuntu_14.04.tar
3.编辑镜像
编辑退出过后,不会保存修改的内容,需要自行保存镜像(类似maven的提交)。
➜ docker run -t -i ubuntu /bin/bashroot@a61446f099f3:/# lsbin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr varroot@a61446f099f3:/# touch aroot@a61446f099f3:/# lsa bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr varroot@a61446f099f3:/# exit
4.保存镜像
docker commit -m [提交版本信息] -a [作者] [编辑镜像的镜像的容器的ID:a61446f099f3
] [目标镜像的仓库名:tag]
可压缩镜像大小
docker export [提交版本信息] | docker import - [目标镜像的仓库名:tag]
➜ docker commit -m 'first commit' -a 'ellis' a61446f099f3 ubuntu:testsha256:9a0c4d4bb4e8766cf250bb5620fa4d8364fc59bb3ea5919cd7b72f751ddb4019➜ docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEubuntu test 9a0c4d4bb4e8 3 minutes ago 129 MBubuntu latest 104bec311bcd 2 weeks ago 129 MB
5.查询镜像
➜ docker search mysqlNAME DESCRIPTION STARS OFFICIAL AUTOMATEDmysql MySQL is a widely used, open-source relati... 3625 [OK]mysql/mysql-server Optimized MySQL Server Docker images. Crea... 237 [OK]centurylink/mysql Image containing mysql. Optimized to be li... 47 [OK]sameersbn/mysql 41 [OK]zabbix/zabbix-server-mysql Zabbix Server with MySQL database support 20 [OK]zabbix/zabbix-web-nginx-mysql Zabbix frontend based on Nginx web-server ... 11 [OK]appcontainers/mysql Centos/Debian Based Customizable MySQL Con... 8 [OK]marvambass/mysql MySQL Server based on Ubuntu 14.04 7 [OK]dnhsoft/mysql-utf8 Inherits the official MySQL image configur... 4 [OK]bitnami/mysql Bitnami MySQL Docker Image 3 [OK]alterway/mysql Docker Mysql 3 [OK]frodenas/mysql A Docker Image for MySQL 3 [OK]drupaldocker/mysql MySQL for Drupal 2 [OK]yfix/mysql Yfix docker built mysql 2 [OK]coscale/mysql CoScale custom configuration of the offici... 1 [OK]newrelic/mysql-plugin New Relic Plugin for monitoring MySQL data... 1 [OK]lysender/mysql MySQL base image using Ubuntu 16.04 Xenial 1 [OK]projectomakase/mysql Docker image for MySQL 0 [OK]nanobox/mysql MySQL service for nanobox.io 0 [OK]dockerizedrupal/mysql mysql-for-docker 0 [OK]cloudposse/mysql Improved `mysql` service with support for ... 0 [OK]1maa/mysql MySQL database 0 [OK]captomd/mysql CaptoMD mysql configuration 0 [OK]tozd/mysql MySQL (MariaDB fork) Docker image. 0 [OK]treenity/mysql Mysql5.7 with OSx permission fixs 0 [OK]
6.删除镜像
- 当同一个镜像存在多个标签的时候,只是删除指定的标签,而不影响镜像文件。
- 当镜像只存在一个标签的时候,删除则会执行操作删除镜像文件的层。
➜ docker rmi mysql:latestUntagged: mysql:latestUntagged: mysql@sha256:116171866ef2a194368a1760c9ef8c80a75ff6bd28fd25d836d29ce1fcc8173aDeleted: sha256:594dc21de8de7cdae01ecbd4d8a4dedead73756984896a00fce13cbc8c24f38eDeleted: sha256:597ae92858cafe843838c2f7b5768fb657d8e67aa46fa9313267c8fed1e5339fDeleted: sha256:31913f2e2b61577119ac02c90a7c803355dc471880c6b0b9da09aef6efb60979Deleted: sha256:f6d702dc6a290c6e5d7a53879f3b0251daa24d41924841f90543666d3e237ff5Deleted: sha256:5105103084f4c6ec91437ffa86e7d2929a3ac53805eb585c378c7935c03c6b42Deleted: sha256:50a36ecefa69b886268dd91785e88074b3f48447f9b9a3d90c7d56ad2b76a8c3Deleted: sha256:12c2fd29169d6fb77177a6c892ac289df304a0694bc79c5f9939035c5f695f1eDeleted: sha256:d8e052f68f5ab9766b5927174e52b8ff23e5c7427afcc30aef6cc53c24a4d4e3Deleted: sha256:9042efb93af5246dbc36ffd103a8b87e8d39e797e06656fb5df053ca77378eb9Deleted: sha256:c08bac17c414ebfb8d22b46b79d4c1fc41d16a4452c924c306cbec673d215d43Deleted: sha256:30499d7d4e67556242cabe7be7b1bac2a94d7ff00972c6236475858a91b4b2a7Deleted: sha256:b6ca02dfe5e62c58dacb1dec16eb42ed35761c15562485f9da9364bb7c90b9b3
- 删除none镜像
➜ docker rmi $(docker images -f "dangling=true" -q)
- 当镜像创建的容器存在时,无法删除镜像文件,需要先删除依赖该镜像的所有容器,再删除镜像。
➜ docker run ubuntu:test echo 'hello world'hello world➜ docker rmi ubuntu:testError response from daemon: conflict: unable to remove repository reference "ubuntu:test" (must force) - container 2761fed757c1 is using its referenced image 9a0c4d4bb4e8➜ docker docker rm 2761fed757c18d1488449db9➜ docker rmi -f 9a0c4d4bb4e8Untagged: ubuntu:testDeleted: sha256:9a0c4d4bb4e8766cf250bb5620fa4d8364fc59bb3ea5919cd7b72f751ddb4019
7.上传镜像
首先需要登陆,然后设置对应账户的tag才能上传,下面举例子上传的是test
的仓库到用户xxxx
里。
➜ docker loginLogin with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.Username: ${xxxx}Password:Login Succeeded➜ docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEtest latest 104bec311bcd 2 weeks ago 129 MBubuntu latest 104bec311bcd 2 weeks ago 129 MBmysql 5.7 594dc21de8de 2 weeks ago 400.2 MB➜ docker tag test:latest xxxx/test:latest➜ docker docker push xxxx/test:latestThe push refers to a repository [docker.io/xxxx/test]5972ebe5b524: Preparing3d515508d4eb: Pushing [==================================================>] 4.608 kBbbe6cef52379: Preparing87f743c24123: Pushing [==================================================>] 15.87 kB32d75bc97c41: Preparing
三.容器
1.创建/启动/关闭
从启动到关闭
➜ docker create -it test:latest11ad937149a8750487e72cf0a696b85927388acbadcba3138d821fbe06ba9c18➜ docker start 11ad937149a811ad937149a8➜ docker stop 11ad937149a811ad937149a8等价于➜ docker run ubuntu /bin/echo "Hello world"Hello world
2.进入容器
-t 让Docker分配一个伪终端并绑定到容器的标准输入上
-i 让容器的标准输入保持打开➜ docker run -t -i test:latest /bin/bashroot@b7a06bcfe3e2:/#
3.后台运行
执行指令➜ docker run -d test:latest /bin/sh -c "while true;do echo hello world; sleep 1; done"7a964175acc1b91a9358c6a4ae7eff53e57d053ffabbd956de4ad8212298ed08查看容器进程➜ docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES7a964175acc1 test:latest "/bin/sh -c 'while tr" 9 seconds ago Up 8 seconds desperate_fermat查看容器日志➜ docker logs 7a964175acc1hello worldhello world终止容器➜ docker stop 7a964175acc17a964175acc1or ➜ docker kill 7a964175acc17a964175acc1重启容器➜ docker restart ecb24cdd42f2ecb24cdd42f2
4.回到容器
使用-d参数的时候,容器启动就后台运行了,需要进入这些后台运行中的容器会用到attach。
➜ docker attach 7a964175acc1hello worldhello world
但是当使用attach退出来的时候,后台进程也会跟着结束。自Docker1.3版本起,可以使用exec直接启动一个bash。
➜ docker exec -ti 7a964175acc1 /bin/bashroot@7a964175acc1:/#
5.删除容器
-f --force=false强行终止并删除一个运行中的容器
-l --link=false删除容器的连接,但保留容器-v --volumes=false删除容器挂载的数据卷➜ docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESecb24cdd42f2 test:latest "/bin/sh -c 'while tr" 19 minutes ago Exited (0) 8 minutes ago small_wescoff➜ docker rm ecb24cdd42f2ecb24cdd42f2➜ docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES7a964175acc1 test:latest "/bin/sh -c 'while tr" 20 minutes ago Up 5 minutes desperate_fermat➜ docker rm 7a964175acc1Error response from daemon: You cannot remove a running container 7a964175acc1b91a9358c6a4ae7eff53e57d053ffabbd956de4ad8212298ed08. Stop the container before attempting removal or use -f➜ docker rm -f 7a964175acc17a964175acc1
6.导出容器
➜ docker docker export 67926c2742d8 > test_for_run.tar➜ docker lstest_for_run.tar
7.导入容器
➜ cat test_for_run.tar| docker import - mdxdjh/test:v1.0sha256:22673bf214f309ebdcb129380bb71d6af0a0fae1053fa9863b73484d61926798➜ docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEmdxdjh/test v1.0 22673bf214f3 2 seconds ago 110.9 MB
四.数据卷
1.创建数据卷
/tmp为本地路径,/var/tmp为容器
ro 只读,rw 读写➜ ls /tmpa➜ docker run -tid --name myTmp -v /tmp:/var/tmp:ro test1:latest31f0d584d5592c5bf147b4dba3a80f6dff3bac7da79ae94ec8b5efc90f1ee05b➜ docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES31f0d584d559 test1:latest "/bin/bash" 10 seconds ago Up 8 seconds elegant_golick➜ docker exec -ti 31f0d584d559 /bin/bashroot@31f0d584d559:/# ls /var/tmp/a
2.数据卷共享
- 先创建一个数据卷myTmp,通过绑定共享到给anotherTmp1,再通过anotherTmp1共享给anotherTmp2达到级联共享的效果
- 删除anotherTmp1和anotherTmp2不会造成myTmp数据卷内容丢失,删除myTmp(父)会导致anotherTmp1和anotherTmp2数据级联删除
➜ docker run -ti --volumes-from myTmp --name anotherTmp1 test1:latestroot@fe2e007b1f96:/# ls /var/tmp/a➜ docker run -ti --volumes-from anotherTmp1 --name anotherTmp2 test1:latestroot@9a35e7fc68cb:/# ls /var/tmp/a
3.删除数据卷
-v 删除关联的容器
➜ docker rm -v 9a35e7fc68cb