docker常用管理命令(上)

本文只记录docker命令在大部分情境下的使用,如果想了解每一个选项的细节,请参考官方文档,这里只作为自己以后的备忘记录下来。

根据自己的理解,总的来说分为以下几种:
容器生命周期管理 — docker [run|start|stop|restart|kill|rm|pause|unpause]
容器操作运维 — docker [ps|inspect|top|attach|events|logs|wait|export|port]
容器rootfs命令 — docker [commit|cp|diff]
镜像仓库 — docker [login|pull|push|search]
本地镜像管理 — docker [images|rmi|tag|build|history|save|import]
其他命令 — docker [info|version]

看一个变迁图

docker_cli_stage

1. 列出机器上的镜像(images)

1
2
3
4
# docker images 
REPOSITORY               TAG             IMAGE ID        CREATED         VIRTUAL SIZE
ubuntu                   14.10           2185fd50e2ca    13 days ago     236.9 MB
…

其中我们可以根据REPOSITORY来判断这个镜像是来自哪个服务器,如果没有 / 则表示官方镜像,类似于username/repos_name表示Github的个人公共库,类似于regsistory.example.com:5000/repos_name则表示的是私服。
IMAGE ID列其实是缩写,要显示完整则带上--no-trunc选项

2. 在docker index中搜索image(search)

Usage: docker search TERM

1
2
3
# docker search seanlo
NAME                DESCRIPTION           STARS     OFFICIAL   AUTOMATED
seanloook/centos6   sean's docker repos         0

 

搜索的范围是官方镜像和所有个人公共镜像。NAME列的 / 后面是仓库的名字。

3. 从docker registry server 中下拉image或repository(pull)

Usage: docker pull [OPTIONS] NAME[:TAG]

1
# docker pull centos

 

上面的命令需要注意,在docker v1.2版本以前,会下载官方镜像的centos仓库里的所有镜像,而从v.13开始官方文档里的说明变了:will pull the centos:latest image, its intermediate layers and any aliases of the same id,也就是只会下载tag为latest的镜像(以及同一images id的其他tag)。
也可以明确指定具体的镜像:

1
# docker pull centos:centos6

 

当然也可以从某个人的公共仓库(包括自己是私人仓库)拉取,形如docker pull username/repository<:tag_name> :

1
# docker pull seanlook/centos:centos6

 

如果你没有网络,或者从其他私服获取镜像,形如docker pull registry.domain.com:5000/repos:<tag_name>

1
# docker pull dl.dockerpool.com:5000/mongo:latest

 

4. 推送一个image或repository到registry(push)

与上面的pull对应,可以推送到Docker Hub的Public、Private以及私服,但不能推送到Top Level Repository。

1
2
# docker push seanlook/mongo
# docker push registry.tp-link.net:5000/mongo:2014-10-27

 

registry.tp-link.net也可以写成IP,172.29.88.222。
在repository不存在的情况下,命令行下push上去的会为我们创建为私有库,然而通过浏览器创建的默认为公共库。

5. 从image启动一个container(run)

docker run命令首先会从特定的image创之上create一层可写的container,然后通过start命令来启动它。停止的container可以重新启动并保留原来的修改。run命令启动参数有很多,以下是一些常规使用说明,更多部分请参考http://www.cnphp6.com/archives/24899
当利用 docker run 来创建容器时,Docker 在后台运行的标准操作包括:

  • 检查本地是否存在指定的镜像,不存在就从公有仓库下载
  • 利用镜像创建并启动一个容器
  • 分配一个文件系统,并在只读的镜像层外面挂载一层可读写层
  • 从宿主主机配置的网桥接口中桥接一个虚拟接口到容器中去
  • 从地址池配置一个 ip 地址给容器
  • 执行用户指定的应用程序
  • 执行完毕后容器被终止

Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

5.1 使用image创建container并执行相应命令,然后停止

1
2
# docker run ubuntu echo "hello world"
hello word

这是最简单的方式,跟在本地直接执行echo 'hello world' 几乎感觉不出任何区别,而实际上它会从本地ubuntu:latest镜像启动到一个容器,并执行打印命令后退出(docker ps -l可查看)。需要注意的是,默认有一个--rm=true参数,即完成操作后停止容器并从文件系统移除。因为Docker的容器实在太轻量级了,很多时候用户都是随时删除和新创建容器。
容器启动后会自动随机生成一个CONTAINER ID,这个ID在后面commit命令后可以变为IMAGE ID

使用image创建container并进入交互模式, login shell是/bin/bash

1
2
# docker run -i -t --name mytest centos:centos6 /bin/bash
bash-4.1#

上面的--name参数可以指定启动后的容器名字,如果不指定则docker会帮我们取一个名字。镜像centos:centos6也可以用IMAGE ID (68edf809afe7) 代替),并且会启动一个伪终端,但通过ps或top命令我们却只能看到一两个进程,因为容器的核心是所执行的应用程序,所需要的资源都是应用程序运行所必需的,除此之外,并没有其它的资源,可见Docker对资源的利用率极高。此时使用exit或Ctrl+D退出后,这个容器也就消失了(消失后的容器并没有完全删除?)
(那么多个TAG不同而IMAGE ID相同的的镜像究竟会运行以哪一个TAG启动呢

5.2 运行出一个container放到后台运行

1
2
# docker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 2; done"
ae60c4b642058fefcc61ada85a610914bed9f5df0e2aa147100eab85cea785dc

它将直接把启动的container挂起放在后台运行(这才叫saas),并且会输出一个CONTAINER ID,通过docker ps可以看到这个容器的信息,可在container外面查看它的输出docker logs ae60c4b64205,也可以通过docker attach ae60c4b64205连接到这个正在运行的终端,此时在Ctrl+C退出container就消失了,按ctrl-p ctrl-q可以退出到宿主机,而保持container仍然在运行
另外,如果-d启动但后面的命令执行完就结束了,如/bin/bashecho test,则container做完该做的时候依然会终止。而且-d不能与–rm同时使用
可以通过这种方式来运行memcached、apache等。

5.3 映射host到container的端口和目录

映射主机到容器的端口是很有用的,比如在container中运行memcached,端口为11211,运行容器的host可以连接container的 internel_ip:11211 访问,如果有从其他主机访问memcached需求那就可以通过-p选项,形如-p <host_port:contain_port>,存在以下几种写法:

1
2
3
4
-p 11211:11211 这个即是默认情况下,绑定主机所有网卡(0.0.0.0)的11211端口到容器的11211端口上
-p 127.0.0.1:11211:11211 只绑定localhost这个接口的11211端口
-p 127.0.0.1::5000
-p 127.0.0.1:80:8080

 

目录映射其实是“绑定挂载”host的路径到container的目录,这对于内外传送文件比较方便,在搭建私服那一节,为了避免私服container停止以后保存的images不被删除,就要把提交的images保存到挂载的主机目录下。使用比较简单,-v <host_path:container_path>,绑定多个目录时再加-v

1
-v /tmp/docker:/tmp/docker

 

另外在两个container之间建立联系可用--link,详见高级部分或官方文档
下面是一个例子:

1
2
3
4
# docker run --name nginx_test \
> -v /tmp/docker:/usr/share/nginx/html:ro \
> -p 80:80 -d \
> nginx:1.7.6

 

在主机的/tmp/docker下建立index.html,就可以通过http://localhost:80/http://host-ip:80访问了。

6. 将一个container固化为一个新的image(commit)

当我们在制作自己的镜像的时候,会在container中安装一些工具、修改配置,如果不做commit保存起来,那么container停止以后再启动,这些更改就消失了。
docker commit <container> [repo:tag]
后面的repo:tag可选
只能提交正在运行的container,即通过docker ps可以看见的容器,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
查看刚运行过的容器
# docker ps -l
CONTAINER ID   IMAGE     COMMAND      CREATED       STATUS        PORTS   NAMES
c9fdf26326c9   nginx:1   nginx -g..   3 hours ago   Exited (0)..     nginx_test

启动一个已存在的容器(run是从image新建容器后再启动),以下也可以使用docker start nginx_test代替  
[[email protected] docker]# docker start c9fdf26326c9
c9fdf26326c9


docker run -i -t --sig-proxy=false 21ffe545748baf /bin/bash
nginx服务没有启动


# docker commit -m "some tools installed" fcbd0a5348ca seanlook/ubuntu:14.10_tutorial
fe022762070b09866eaab47bc943ccb796e53f3f416abf3f2327481b446a9503

 

-a “[email protected]
请注意,当你反复去commit一个容器的时候,每次都会得到一个新的IMAGE ID,假如后面的repository:tag没有变,通过docker images可以看到,之前提交的那份镜像的repository:tag就会变成<none>:<none>,所以尽量避免反复提交。
另外,观察以下几点

  • commit container只会pause住容器,这是为了保证容器文件系统的一致性,但不会stop。如果你要对这个容器继续做其他修改:

     

    • 你可以重新提交得到新image2,删除次新的image1
    • 也可以关闭容器用新image1启动,继续修改,提交image2后删除image1
    • 当然这样会很痛苦,所以一般是采用Dockerfilebuild得到最终image,参考[]
  • 虽然产生了一个新的image,并且你可以看到大小有100MB,但从commit过程很快就可以知道实际上它并没有独立占用100MB的硬盘空间,而只是在旧镜像的基础上修改,它们共享大部分公共的“片”。

下文继续:

参考

转:http://seanlook.com/2014/10/31/docker-command-best-use-1/#rd

Share This:

61 thoughts on “docker常用管理命令(上)”

  1. I simply want to tell you that I am just new to weblog and certainly loved your blog site. Almost certainly I’m likely to bookmark your blog post . You surely have outstanding article content. Bless you for sharing with us your webpage.

  2. Auto Tech Center has been providing car maintenance and auto repair in Ann Arbor MI and Dexter MI for over 26 years. Our mechanics provide quality work for oil change, brakes, and general car maintenance.

  3. What’s Happening i’m new to this, I stumbled upon this I’ve found It positively useful and it has aided me out loads. I hope to contribute & help other users like its aided me. Great job.

  4. I just couldn’t leave your web site prior to suggesting that I really enjoyed the standard info an individual supply to your guests? Is going to be again continuously in order to inspect new posts

  5. I am also writing to let you know of the really good experience my cousin’s girl had viewing yuor web blog. She realized so many pieces, including what it’s like to have a great helping style to let a number of people without problems fully understand selected tricky issues. You truly did more than my desires. I appreciate you for presenting those essential, trusted, revealing as well as unique thoughts on your topic to Mary.

  6. My wife and i have been now fortunate Albert managed to conclude his reports from the ideas he received from your own web site. It’s not at all simplistic to simply happen to be making a gift of tips and hints that people today might have been selling. And now we remember we’ve got the website owner to appreciate for that. The most important explanations you’ve made, the simple site menu, the friendships you will give support to instill – it’s got all exceptional, and it’s really helping our son and our family consider that this topic is thrilling, and that’s unbelievably vital. Many thanks for everything!

  7. Thank you for every one of your work on this web site. Kim loves setting aside time for investigation and it’s really simple to grasp why. My partner and i know all of the lively form you render insightful ideas by means of your web site and even foster participation from website visitors on this subject then our own simple princess is undoubtedly being taught a great deal. Have fun with the remaining portion of the new year. You’re performing a good job.

  8. I must show my appreciation for your kindness in support of those individuals that must have assistance with in this idea. Your personal dedication to passing the solution around had become certainly informative and have usually empowered regular people much like me to attain their desired goals. Your valuable report indicates much to me and especially to my office workers. Warm regards; from everyone of us.

  9. I am just commenting to make you be aware of what a fabulous discovery my friend’s daughter went through using your web site. She figured out such a lot of pieces, which included how it is like to have an ideal teaching heart to let many more easily know just exactly specific extremely tough matters. You actually did more than people’s desires. Thanks for presenting these priceless, healthy, revealing and also unique guidance on the topic to Lizeth.

  10. Thanks for all of the hard work on this blog. Kate really likes carrying out research and it is easy to understand why. We all know all about the powerful ways you produce valuable guidance by means of the web site and boost response from people on this situation and my girl is without question being taught a lot of things. Enjoy the rest of the year. You are always carrying out a fabulous job.

  11. I simply wanted to post a quick remark in order to say thanks to you for all of the unique tactics you are showing here. My time consuming internet lookup has now been honored with really good ideas to exchange with my family and friends. I ‘d assume that most of us readers actually are rather endowed to dwell in a really good website with many lovely professionals with beneficial techniques. I feel extremely privileged to have come across your entire webpage and look forward to some more cool moments reading here. Thanks again for all the details.

  12. I must show my appreciation to this writer for bailing me out of this type of scenario. Right after scouting throughout the the web and getting recommendations which were not helpful, I thought my entire life was over. Living without the presence of solutions to the difficulties you’ve fixed through the short post is a serious case, as well as ones that might have in a wrong way affected my career if I hadn’t come across your web page. Your competence and kindness in playing with all the pieces was crucial. I am not sure what I would have done if I hadn’t encountered such a point like this. It’s possible to at this point look forward to my future. Thanks a lot very much for your professional and result oriented help. I will not hesitate to propose the website to any person who would need guidelines about this situation.

  13. Thanks a lot for giving everyone an extraordinarily wonderful chance to check tips from this website. It is usually very lovely and as well , jam-packed with amusement for me and my office co-workers to visit the blog the equivalent of three times a week to read the newest items you will have. And indeed, I am at all times pleased with all the mind-boggling suggestions served by you. Some two facts in this article are absolutely the finest I have had.

  14. I in addition to my pals came checking out the best techniques from the website and so the sudden developed a terrible suspicion I never thanked you for those tips. Most of the ladies came as a consequence very interested to read them and have now truly been making the most of these things. I appreciate you for being indeed considerate and for picking out variety of fantastic resources millions of individuals are really desperate to be aware of. My sincere apologies for not saying thanks to sooner.

  15. I precisely wished to thank you so much all over again. I’m not certain the things I might have implemented without those recommendations discussed by you regarding such theme. It was actually the distressing dilemma for me, but being able to view your well-written technique you resolved the issue forced me to weep over contentment. I am just happier for the help and as well , hope you really know what a powerful job that you’re undertaking teaching most people via your web site. I am sure you have never encountered all of us.

  16. I as well as my pals happened to be taking note of the excellent things from your web page then the sudden came up with a terrible feeling I never expressed respect to the website owner for those techniques. My young boys were definitely totally happy to study them and have now truly been using these things. Appreciate your really being indeed helpful and also for obtaining varieties of remarkable subject matter most people are really desperate to be aware of. My personal honest apologies for not expressing appreciation to sooner.

  17. Thank you so much for giving everyone an exceptionally splendid possiblity to read critical reviews from this web site. It is always very brilliant and as well , full of amusement for me personally and my office acquaintances to visit your website more than three times weekly to find out the fresh guides you will have. And of course, I’m so always impressed considering the gorgeous knowledge you serve. Selected 2 areas in this post are rather the most impressive we’ve ever had.

  18. Thanks so much for providing individuals with an extraordinarily spectacular opportunity to check tips from this blog. It is usually very awesome and stuffed with a great time for me and my office friends to search your web site minimum three times weekly to see the new tips you have. Not to mention, we’re at all times impressed with the splendid methods served by you. Some 4 tips in this posting are surely the most efficient I’ve had.

  19. I must show my appreciation to the writer for bailing me out of such a difficulty. Just after looking throughout the world-wide-web and coming across views which were not beneficial, I thought my entire life was gone. Living minus the strategies to the problems you have resolved by way of your good short article is a crucial case, as well as the ones that would have in a negative way damaged my career if I had not noticed your blog. Your main mastery and kindness in touching all the pieces was vital. I am not sure what I would’ve done if I hadn’t come across such a stuff like this. I can now look forward to my future. Thank you so much for the reliable and sensible help. I will not be reluctant to endorse your web blog to any person who ought to have assistance on this situation.

  20. Thanks for each of your hard work on this web site. My daughter takes pleasure in participating in investigations and it’s obvious why. My spouse and i learn all about the lively method you make vital tips and hints on your web blog and as well as increase contribution from other individuals on this idea so our child is in fact becoming educated a whole lot. Have fun with the rest of the year. You have been doing a dazzling job.

  21. Thanks a lot for providing individuals with a very pleasant possiblity to read in detail from here. It’s usually very cool and as well , packed with a lot of fun for me personally and my office acquaintances to search the blog particularly thrice in one week to read the latest secrets you have. Of course, I’m usually motivated for the striking hints you give. Selected two ideas in this article are particularly the best I have ever had.

  22. I simply wanted to write down a quick note to be able to thank you for all the unique tips you are placing at this site. My time consuming internet research has at the end been rewarded with beneficial knowledge to talk about with my neighbours. I would believe that we site visitors actually are truly blessed to live in a good site with very many perfect people with interesting tricks. I feel quite grateful to have seen your website and look forward to many more brilliant minutes reading here. Thank you once again for all the details.

  23. Thanks for all of your effort on this blog. Kim take interest in carrying out internet research and it’s easy to understand why. A lot of people know all of the dynamic manner you make effective techniques via the web site and as well as foster contribution from some others on this article then our own child is actually learning a great deal. Take pleasure in the rest of the year. You are always performing a dazzling job.

  24. My spouse and i felt ecstatic Albert could complete his reports through the ideas he gained through your web site. It is now and again perplexing just to happen to be making a gift of steps that many the rest might have been selling. And we already know we need the website owner to appreciate for that. The explanations you made, the straightforward site navigation, the relationships your site assist to instill – it’s everything overwhelming, and it’s really leading our son and us understand the matter is fun, and that is pretty essential. Many thanks for all!

  25. Thank you for every one of your work on this web page. My mom delights in doing investigations and it’s really easy to see why. We all hear all concerning the compelling form you give simple steps through this web site and as well as encourage contribution from other people on the situation and our girl is undoubtedly discovering a great deal. Have fun with the remaining portion of the new year. You are performing a powerful job.

  26. I wanted to put you a tiny observation in order to thank you so much again for all the beautiful strategies you have shared on this page. This is really remarkably generous with you to give easily what exactly a few individuals would have made available as an electronic book to help make some money for their own end, primarily now that you might well have done it if you ever decided. These good tips as well worked as a easy way to be certain that other individuals have the same fervor much like mine to figure out a great deal more regarding this condition. I am certain there are lots of more pleasurable moments up front for many who looked over your site.

  27. My wife and i ended up being very excited Raymond could conclude his researching out of the ideas he gained when using the web page. It’s not at all simplistic to just find yourself making a gift of secrets that many others may have been selling. So we already know we’ve got the writer to give thanks to for this. These illustrations you made, the easy website navigation, the relationships you will help to promote – it’s all incredible, and it’s aiding our son and us feel that this situation is exciting, and that is very vital. Thanks for all the pieces!

  28. My spouse and i were really happy Edward managed to do his analysis from your precious recommendations he received from your very own web site. It’s not at all simplistic to just choose to be giving freely procedures which usually men and women have been selling. We really consider we now have the writer to be grateful to for this. The type of explanations you have made, the easy site menu, the friendships you can make it easier to create – it’s everything exceptional, and it’s really helping our son and our family do think this subject matter is fun, and that’s wonderfully fundamental. Many thanks for all the pieces!

  29. I precisely desired to thank you so much again. I’m not certain what I might have gone through in the absence of the actual creative ideas shown by you on my situation. It was actually a fearsome setting for me personally, nevertheless finding out the very skilled fashion you resolved it took me to weep with happiness. Now i am happier for the advice and have high hopes you are aware of an amazing job you are always accomplishing teaching most people all through your websites. I know that you’ve never met any of us.

  30. I simply wished to thank you very much all over again. I’m not certain the things I would have accomplished in the absence of the secrets contributed by you regarding such a industry. It had been the scary concern for me, however , considering a specialized avenue you managed it made me to leap with fulfillment. I am grateful for your work and even wish you find out what an amazing job you’re putting in teaching most people thru your web blog. More than likely you have never met all of us.

  31. Thank you so much for providing individuals with a very special opportunity to read critical reviews from this website. It is always so pleasing and stuffed with amusement for me personally and my office fellow workers to search the blog minimum 3 times in 7 days to learn the latest things you will have. Of course, I’m at all times satisfied considering the perfect ideas you give. Selected 2 facts on this page are without a doubt the most suitable I have had.

  32. I not to mention my friends appeared to be analyzing the great secrets on your web page and then at once came up with a horrible suspicion I never thanked you for those tips. Most of the young men are already absolutely warmed to see all of them and have in effect simply been tapping into these things. We appreciate you genuinely considerably kind and for making a choice on certain decent themes millions of individuals are really wanting to learn about. My very own sincere regret for not saying thanks to you earlier.

  33. I intended to post you that very small observation to be able to say thanks once again for the lovely tips you have featured in this article. This is certainly wonderfully generous with you to grant freely all that most of us could possibly have offered for sale for an ebook to end up making some money for their own end, especially given that you might well have tried it in case you considered necessary. The ideas additionally served to become great way to realize that most people have a similar desire similar to my own to learn a lot more in respect of this matter. Certainly there are some more pleasurable moments ahead for many who find out your site.

  34. I discovered your blog web site on google and test a number of of your early posts. Continue to maintain up the very good operate. I just further up your RSS feed to my MSN News Reader. In search of ahead to studying extra from you later on!?

  35. I precisely needed to thank you very much once again. I do not know what I would have tried in the absence of the secrets revealed by you regarding that question. It previously was a real difficult condition in my position, but spending time with this expert avenue you solved that took me to jump over delight. I’m happy for your guidance and hope that you recognize what an amazing job you happen to be providing teaching other individuals through the use of your web page. Probably you have never come across any of us.

  36. I and also my buddies were checking out the great procedures located on the website and before long developed a horrible suspicion I never expressed respect to the blog owner for them. All of the ladies became totally passionate to learn all of them and have in effect quite simply been having fun with these things. We appreciate you indeed being really considerate and also for opting for varieties of quality issues most people are really desirous to understand about. Our own sincere apologies for not expressing appreciation to earlier.

  37. I really wanted to write down a quick word to appreciate you for some of the magnificent recommendations you are posting on this site. My extensive internet search has finally been honored with wonderful suggestions to share with my friends and classmates. I would believe that many of us readers actually are unequivocally endowed to exist in a fantastic network with so many brilliant professionals with great ideas. I feel quite privileged to have used your site and look forward to so many more brilliant moments reading here. Thank you once more for a lot of things.

  38. I was looking for a solid, practical sword for self defense purposes. After scouring Amazon, this is where I decided to take my chances. I am VERY pleased! This sword is definitely well made. I have no doubt it will hold up if and when I need to fully utilize it’s potential. Solidly sharp edge out of the box. A little heavier than I had expected, but this is my first katana. The balance is slightly forward, but has a fantastic overall feel and great look. For the price, I couldn’t be happier! I have full confidence this is capable of taking an intruders arm clean off.

  39. Howdy! This post could not be written any better! Looking through this article reminds me of my previous roommate!
    He always kept talking about this. I most certainly
    will forward this post to him. Fairly certain he will have a great read.
    Many thanks for sharing! http://yahoo.co.uk

  40. You …… You…… Yu Chengdu hand clutching his left chest wound, just clank has been bleeding to scare, eyes looked terrified boa, Qieqie whisper beg for mercy: as long as the little hero didn’t kill me, I promise to give you what you want to……

  41. Can not over two or three days, early in the morning, he and Dong Dong three wife just got up, then a servant said: three yes at the door, don’t come to the village, said to be in a hurry.

发表评论

电子邮件地址不会被公开。 必填项已用*标注

4 + 5 =