docker学习笔记.doc_第1页
docker学习笔记.doc_第2页
docker学习笔记.doc_第3页
docker学习笔记.doc_第4页
docker学习笔记.doc_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1 Docker是什么?Docker是一个云计算平台,它利用Linux的LXC、AUFU、Go语言、cgroup实现了资源的独立,可以很轻松的实现文件、资源、网络等隔离,其最终的目标是实现类似PaaS平台的应用隔离。Docker容器试图解决“依赖地狱”问题。现代的应用通常从已存在的组件组合而来,并且依赖其他服务和应用。比如,你的Python应用可能使用Postgre所为一个数据存储,用Redis缓存以及使用Apache作web服务器。每个这些组件都附带自身的一些依赖,这些依赖可能与其他组件产生冲突。通过打包每个组件及其依赖,Docker容器解决以下问题: 冲突依赖:需要在PHP4.3上运行一个web站点而另一个运行在PHP5.5上,如果你在一个独立的Docker容器中运行每个版本的PHP,那就没问题。 缺少依赖:在一个新环境上安装应用对Docker容器来说只是瞬间的事情,因为所有的依赖都和这个应用一起打包到一个容器中。 平台依赖:从一个发行版移动到另一个不再是一个麻烦。如果两个系统都运行了Docker容器,那么相同的容器执行起来将没有任何问题。使用Docker进行软件开发的工作流程举例:在这个例子里,公司的产品质量测试环境运行的Centos和Docker。它也从公共或者私有的注册中心下载映像,然后再环境更新的时候启动各种容器。最后,为了方便扩展和伸缩,公司把生产环境部署在云中,即部署在亚马逊的WEB服务上(AWS)。亚马逊Linux上也运行了管理不同容器的Docker。注意:上面的所有三个环境运行着不同版本的Linux,但这三个环境都与Docker兼容。而且每个环境都运行着不同的容器组合。然而,由于每个容器都把自己的依赖同其他容器分离开来,因此不存在任何冲突,所有容器都平安地并存着。图4.使用Docker进行软件开发的工作流程举例认识到Docker提供的是一个以应用为核心的容器模型是非常重要的。也就是说,容器运行的是单独的应用或者服务,而不是许多应用或者服务。我们已经知道:创建和运行容器非常快而且消耗的资源也很少。由于你所使用的系统遵循单一责任法则,而且每个容器运行一个主进程,所以系统组件之间就是松耦合的。基于这个理念,我们自己就可以创建属于自己的,可以启动容器的映像了。2 Docker安装2.1 在线安装2.2 离线安装1 下载离线版本的docker(二进制文件):/builds/Linux/x86_64/docker-latest.tgz2 注册环境变量下载后cd到docker程序,可以执行docker命令# start the docker in daemon mode from the directory you unpacked$ sudo ./docker -d &没有在环境变量中注册前,需要以 “./docker” 这种方式执行命令。在系统环境变量中配置路径后就可以以 “docker” 这种方式执行命令。以redhat7.0为例说明,打开etc/bashrc文件,在其中添加docker二进制文件的路径即可(假定docker二进制文件的路径是/data/docker)。打开文件:vi etc/bashrc滚动到文件最后,添加下列语句即可:PATH=$PATH:/dataexport PATH效果如下: else umask 022 fi SHELL=/bin/bash # Only display echos from profile.d scripts if we are no login shell # and interactive - otherwise just process them to set envvars for i in /etc/profile.d/*.sh; do if -r $i ; then if $PS1 ; then . $i else . $i /dev/null fi fi done unset i unset -f pathmungefi# vim:ts=4:sw=4PATH=$PATH:/dataexport PATH注册好环境变量后,查看docker的版本:rootlocalhost bin# docker versionClient version: 1.3.2Client API version: 1.15Go version (client): go1.3.3Git commit (client): 39fa2faOS/Arch (client): linux/amd64Server version: 1.3.2Server API version: 1.15Go version (server): go1.3.3Git commit (server): 39fa2farootlocalhost bin#3 在后台启动docker 守护进程离线下载的docker需要在后台启动守护进程。语句如下:nohup docker -d效果如下4 Docker命令能用后,使用save指令可以将docker hub上下载的image保存起来5 docker命令能用后,需要使用docker时使用load指令加载出image6 基于images创建出container7 Docker的命令使用7.1 查看子命令查看docker的子命令,直接敲docker或完整的docker help就可以了。能看到完整的子命令证明docker安装成功。rootlocalhost # docker helpUsage: docker OPTIONS COMMAND arg.A self-sufficient runtime for linux containers.Options: -api-enable-cors=false Enable CORS headers in the remote API -b, -bridge= Attach containers to a pre-existing network bridge use none to disable container networking -bip= Use this CIDR notation address for the network bridges IP, not compatible with -b -D, -debug=false Enable debug mode -d, -daemon=false Enable daemon mode -dns= Force Docker to use specific DNS servers -dns-search= Force Docker to use specific DNS search domains -e, -exec-driver=native Force the Docker runtime to use a specific exec driver -fixed-cidr= IPv4 subnet for fixed IPs (ex: /16) this subnet must be nested in the bridge subnet (which is defined by -b or -bip) -G, -group=docker Group to assign the unix socket specified by -H when running in daemon mode use (the empty string) to disable setting of a group -g, -graph=/var/lib/docker Path to use as the root of the Docker runtime -H, -host= The socket(s) to bind to in daemon mode or connect to in client mode, specified using one or more tcp:/host:port, unix:/path/to/socket, fd:/* or fd:/socketfd. -icc=true Enable inter-container communication -insecure-registry= Enable insecure communication with specified registries (no certificate verification for HTTPS and enable HTTP fallback) -ip= Default IP address to use when binding container ports -ip-forward=true Enable net.ipv4.ip_forward -ip-masq=true Enable IP masquerading for bridges IP range -iptables=true Enable Dockers addition of iptables rules -mtu=0 Set the containers network MTU if no value is provided: default to the default route MTU or 1500 if no default route is available -p, -pidfile=/var/run/docker.pid Path to use for daemon PID file -registry-mirror= Specify a preferred Docker registry mirror -s, -storage-driver= Force the Docker runtime to use a specific storage driver -selinux-enabled=false Enable selinux support. SELinux does not presently support the BTRFS storage driver -storage-opt= Set storage driver options -tls=false Use TLS; implied by tls-verify flags -tlscacert=/root/.docker/ca.pem Trust only remotes providing a certificate signed by the CA given here -tlscert=/root/.docker/cert.pem Path to TLS certificate file -tlskey=/root/.docker/key.pem Path to TLS key file -tlsverify=false Use TLS and verify the remote (daemon: verify client, client: verify daemon) -v, -version=false Print version information and quitCommands: attach Attach to a running container build Build an image from a Dockerfile commit Create a new image from a containers changes cp Copy files/folders from a containers filesystem to the host path create Create a new container diff Inspect changes on a containers filesystem events Get real time events from the server exec Run a command in an existing container export Stream the contents of a container as a tar archive history Show the history of an image images List images import Create a new filesystem image from the contents of a tarball info Display system-wide information inspect Return low-level information on a container kill Kill a running container load Load an image from a tar archive login Register or log in to a Docker registry server logout Log out from a Docker registry server logs Fetch the logs of a container port Lookup the public-facing port that is NAT-ed to PRIVATE_PORT pause Pause all processes within a container ps List containers pull Pull an image or a repository from a Docker registry server push Push an image or a repository to a Docker registry server restart Restart a running container rm Remove one or more containers rmi Remove one or more images run Run a command in a new container save Save an image to a tar archive search Search for an image on the Docker Hub start Start a stopped container stop Stop a running container tag Tag an image into a repository top Lookup the running processes of a container unpause Unpause a paused container version Show the Docker version information wait Block until a container stops, then print its exit codeRun docker COMMAND -help for more information on a command.rootlocalhost #7.2 常用命令其中括起来的参数为必选,括起来为可选 docker version查看docker的版本号,包括客户端、服务端、依赖的Go等 docker info查看系统(docker)层面信息,包括管理的images, containers数等 docker search 在docker index中搜索image docker pull 从docker registry server 中下拉image docker push 推送一个image或repository到registry docker push :TAG同上,指定tag docker inspect 查看image或container的底层信息 docker images 查看images列表(该命令不会列出所有的images) docker images -a列出所有的images docker ps默认显示正在运行中的container docker ps -l显示最后一次创建的container,包括未运行的 docker ps -a显示所有的container,包括未运行的 docker logs 查看container的日志,也就是执行命令的一些输出 docker rm 删除一个或多个container docker rmi 删除一个或多个image docker start/stop/restart 开启/停止/重启container docker start -i 启动一个container并进入交互模式 docker attach attach一个运行中的container docker run 使用image创建container并执行相应命令,然后停止 docker run -i -t /bin/bash使用image创建container并进入交互模式, login shell是/bin/bash docker run -i -t -p 将container的端口映射到宿主机的端口 docker commit repo:tag将一个container固化为一个新的image,后面的repo:tag可选。该命名会创建一个新的image docker build 寻找path路径下名为的Dockerfile的配置文件,使用此配置生成新的image docker build -t repo:tag同上,可以指定repo和可选的tag docker build - 使用指定的dockerfile配置文件,docker以stdin方式获取内容,使用此配置生成新的image docker port 查看本地哪个端口映射到container的指定端口,其实用docker ps也可以看到7.2.1 使用images新建一个container并登录使用image来创建container:rootlocalhost:# docker imagesREPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZEubuntu 13.10 5e019ab7bf6d 12 days ago 180 MBubuntu saucy 5e019ab7bf6d 12 days ago 180 MBubuntu 12.04 74fe38d11401 12 days ago 209.6 MBubuntu precise 74fe38d11401 12 days ago 209.6 MBrootlocalhost:# docker run -i -t 74fe38d11401 /bin/bashroot80c761d06a87:/# cat /etc/issueUbuntu 12.04.4 LTS n l使用repository来创建container, 这时默认使用tag为lastest的image:rootlocalhost:# docker run -i -t ubuntu /bin/bashroot442e1cc85a8d:/# uname -aLinux 442e1cc85a8d 3.8.0-25-generic #37precise1-Ubuntu SMP Fri Jun 7 16:27:35 UTC 2013 x86_64 x86_64 x86_64 GNU/Linuxroot442e1cc85a8d:/# cat /etc/issueUbuntu 14.04 LTS n lroot442e1cc85a8d:/# exit7.2.2 使用commit将一个container固化为一个imagerootlocalhost:# docker ps -lCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESf1fd375204af ubuntu:12.04 /bin/bash 10 minutes ago Exited (127) 48 seconds ago lonely_coldenrootlocalhost:# docker imagesREPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZEubuntu 13.10 5e019ab7bf6d 12 days ago 180 MBubuntu saucy 5e019ab7bf6d 12 days ago 180 MBubuntu 12.04 74fe38d11401 12 days ago 209.6 MB提交当前container为一个image,顺便带上作者信息,并指定repository 和 tagrootlocalhost:# docker commit -a Tanky Woo f1fd375204af ubuntu:testfe65a2781daea01c67c33f11868abe6d510833bca07b90fc681cdfe98a9196acrootlocalhost:# docker imagesREPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZEubuntu test fe65a2781dae 6 seconds ago 209.6 MBubuntu 13.10 5e019ab7bf6d 12 days ago 180 MBubuntu saucy 5e019ab7bf6d 12 days ago 180 MB7.2.3 attach一个运行中的容器该命令可以连接到一个正在运行的容器rootlocalhost:# docker ps -lCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESe2e6c95f0bf5 ubuntu:test /bin/bash 11 minutes ago Exited (0) 11 minutes ago suspicious_mccarthy启动一个container:rootlocalhost:# docker start e2e6c95f0bf5e2e6c95f0bf5可以看此container到正在运行中:rootlocalhost:# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESe2e6c95f0bf5 ubuntu:test /bin/bash 11 minutes ago Up 2 seconds suspicious_mccarthyattach这个container:rootlocalhost:# docker attach e2e6c95f0bf5进入container:roote2e6c95f0bf5:/#7.2.4 使用 docker run -p 的例子把 docker 虚拟机中的虚拟机中的 80 端口端口映射到我们物理机的映射到我们物理机的 80 端口:rootrhel7 # docker run -p 00:80:80 -d -i -t fedora/apache /bin/bash b7d6816689010b3972fedfd1d522235004f8d2143098519dc22bcf0792b53187 7.2.5 删除image/container遇到的依赖关系关于删除时的依赖关系,按照提示删除就行了。删除image时需要先删除引用image的containers。:rootlocalhost:# docker rmi 666c5d65f396 3494872e31a4 62fda5e450d5 5e1829f90d6e 89554a25c998Error: Conflict, cannot delete 666c5d65f396 because the container 43a7072bac7a is using itError: Conflict, cannot delete 3494872e31a4 because the container 40b3cd8b2e42 is using itError: Conflict, cannot delete 62fda5e450d5 because the container 5142a3d092a6 is using itUntagged: test:latestDeleted: 5e1829f90d6e9ac09645841fe6ab85a0b0f9b28f008a571299a624e566684afeDeleted: ae5ae236a8e1d946963a7c2c142cc892b1979cb9458e0ecac4d33d2283ace567Untagged: memchaced:latestDeleted: 89554a25c998d14c76ff885ddac7cc1a47ae4caf9edcddaa43408b402a1684fb2014/05/07 15:44:41 Error: failed to remove one or more imagesrootlocalhost:# docker rm 43a7072bac7a 40b3cd8b2e42 5142a3d092a643a7072bac7a40b3cd8b2e425142a3d092a6且删除images时也可能会遇到依赖其它的images,比如直接删除父镜像时,就会提示需要先删除子镜像。可以通过:docker images -tree来查看,不过官方提示-tree已经弃用了,会在以后的版本去掉。首先清空所有containers:rootlocalhost:# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES然后以树形结构查看依赖关系:rootlocalhost:# docker images -treeWarning: -tree is deprecated, it will be removed soon. See usage.511136ea3c5a Virtual Size: 0 B e2aa6665d371 Virtual Size: 106.1 MB f0ee64c4df74 Virtual Size: 106.3 MB 2209cbf9dcd3 Virtual Size: 106.3 MB 5e019ab7bf6d Virtual Size: 180 MB Tags: ubuntu:13.10, ubuntu:saucy f10ebce2c0e1 Virtual Size: 103.7 MB 82cdea7ab5b5 Virtual Size: 103.9 MB 5dbd9cb5a02f Virtual Size: 103.9 MB 74fe38d11401 Virtual Size: 209.6 MB Tags: ubuntu:precise, ubuntu:12.04 fe65a2781dae Virtual Size: 209.6 MB Tags: ubuntu:test 276cc641e40e Virtual Size: 388.3 MB Tags: ubuntu:newtest ef519c9ee91a Virtual Size: 100.9 MB 07302703becc Virtual Size: 101.2 MB cf8dc907452c Virtual Size: 101.2 MB a7cf8ae4e998 Virtual Size: 171.3 MB Tags: ubuntu:12.10, ubuntu:quantal 5e66087f3ffe Virtual Size: 192.5 MB 4d26dd3ebc1c Virtual Size: 192.7 MB d4010efcfd86 Virtual Size: 192.7 MB 99ec81b80c55 Virtual Size: 266 MB Tags: ubuntu:14.04, ubuntu:latest, ubuntu:trusty 02dae1c13f51 Virtual Size: 98.35 MB e7206bfc66aa Virtual Size: 98.54 MB cb12405ee8fa Virtual Size: 98.54 MB 316b678ddf48 Virtual Size: 169.4 MB Tags: ubuntu:raring, ubuntu:13.04 6cfa4d1f33fb Virtual Size: 0 B 3db9c44f4520 Virtual Size: 183 MB Tags: ubuntu:10.04, ubuntu:lucid现在准备删除12.10版本的父镜像 cf8dc907452c, 会提示有冲突,删不掉:rootlocalhost:# docker rmi cf8dc907452cError: Conflict, cf8dc907452c wasnt deleted2014/05/07 18:49:35 Error: failed to remove one or more images但是可以删除叶子节点 a7cf8ae4e998:rootlocalhost:# docker rmi a7cf8ae4e998Untagged: ubuntu:12.10Untagged: ubuntu:quantalDeleted: a7cf8ae4e998c5339e769d6cc466f9133bd4d330a549bb846cb1641cd638247cDeleted: cf8dc907452c970224551599da573c9e32897fc65286d942625c4c86dabd680dDeleted: 07302703beccc2ea25f34333decad32ed06446e8a14c020ffbd0be017364b9feDeleted: ef519c9ee91a06fc33cefbda1bce27686617761700252dff0397f2c0e269f3c57.2.6 containers之间共享数据docker 的 containers之间共享目录是通过volume。docker run命令使用-v可以绑定一个volume,-v可以使用多次,创建多个volume:rootlocalhost:# docker run -i -t -v /tmp/tankywoo -name data ubuntu:newtest /bin/bash 6/3516使用 mount 看到 /tmp/tankywoo 已经被mount了:rootfec65f523cef:/# mountnone on / type aufs (rw,relatime,si=f7ac8b1595d13ed9)./dev/disk/by-uuid/b77aed99-bb9b-4881-9702-4ed204fe5d46 on /tmp/tankywoo type ext3 (rw,relatime,errors=remount-ro,user_xattr,acl,barrier=1,data=ordered)查看 /tmp/tankywoo 目录下,是空的:rootfec65f523cef:/tmp/tankywoo# lsrootfec65f523cef:/tmp/tankywoo# 然后在宿主机新建一个container,来绑定这个volume:按照 docker run 的命令行参数: -volumes-from=: Mount volumes from the specified container(s)有问题:rootlocalhost:/tmp/tankywoo# docker run -i -t -volumes-from=data ubuntu:newtest /bin/bash 21/1582014/05/08 15:58:19 Error: Cannot start container 5d83dcaf8f0220024e0403a362c0512a8218cfcb45dc911df5d2cd37f9a4e8a4: Container data not found. Impossible to mount its volumes必须像short option的方式使用:rootlocalhost:/tmp/tankywoo# docker run -i -t -volumes-from data ubuntu:newtest /bin/bashrootd100d9604b4b:/# mountnone on / type aufs (rw,relatime,si=f7ac8b15b25036d9)./dev/disk/by-uuid/b77aed99-bb9b-4881-9702-4ed204fe5d46 on /tmp/tankywoo type ext3 (rw,relatime,errors=remount-ro,user_xattr,acl,barrier=1,data=ordered)也可以看到 /tmp/tankywoo 目录,并且是空的,然后新建一个文件:rootd100d9604b4b:/tmp/tankywoo# lsrootd100d9604b4b:/tmp/tankywoo# touch filerootd100d9604b4b:/tmp/tankywoo# lsfile再看看之前那个container:rootfec65f523cef:/tmp/tankywoo# lsfile也有这个文件了7.2.7 虚拟机和物理机数据的共享rootrhel7 # setenforce 0rootrhel7 #mkdir /sharerootrhel7 #to

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论