标签 : dockergit
[TOC]github
本文主要对Docker commands和Dockerfile的相关知识进行整理docker
官网传送门:shell
首先,固然是配置命令自动补全,只须要把一个文件用curl下载copy到特定路径便可,具体操做参考Command-line Completionbash
其实docker有很完备的命令帮助提示,对哪一个指令不清楚,只须要在后面加--help
就能看到帮助说明。例如:app
docker --help
能够看到全部可执行的命令。run
命令,则输入docker run --help
又能看到run
的相关帮助了。经常使用命令:curl
docker images
docker run [OPTIONS] IMAGE [COMMAND] [ARG...] //i.e. docker run image docker run -it image /bin/bash
经常使用的一些参数:ide
--rm
:container退出后自动删除post
-i
和-t
经常一块儿用,-it
:以超级管理员权限打开一个命令行窗口网站
-d
: 后台运行container
--name
:给container命名
查看当前container
docker ps -a
docker rm $(docker ps -a -q)
docker exec -it ${container_id} /bin/bash
docker inspect ${container_id}
另外, 在以上指令中,容器名和容器的container_id都是能够使用的,若是用户没有指定容器名,docker会默认给每一个容器分配一个比较友好的随机名称,像fervent_perlman,high_galileo等等
官网传送门:
感受文档里说了很全了,这里稍微提几个容易困惑的点
1.exec form vs shell form
在CMD
和ENTRYPOINT
都涉及到着两种形式(CMD
多一种彻底做为参数的形式),例如:
CMD ["executable","param1","param2"]
(exec形式,推荐)CMD command param1 param2
(shell形式)至于两种形式的区别,官方的几点说明挺详细的,主要就是变量替换,脚本环境等方面有差异:
- Note: If CMD is used to provide default arguments for the ENTRYPOINT instruction, both the CMD and ENTRYPOINT instructions should be specified with the JSON array format.
- Note: The exec form is parsed as a JSON array, which means that you must use double-quotes (“) around words not single-quotes (‘).
- Note: Unlike the shell form, the exec form does not invoke a command shell. This means that normal shell processing does not happen. For example, CMD [ "echo", "$HOME" ] will not do variable substitution on $HOME. If you want shell processing then either use the shell form or execute a shell directly, for example: CMD [ "sh", "-c", "echo $HOME" ].
2.ENTRYPOINT vs CMD
读完官方的Understand how CMD and ENTRYPOINT interact ,以为这二者特别类似,对这二者有什么区别和联系仍是有些困惑,阅读下面这篇文章:
简而言之,ENTRYPOINT更像一个写死的可执行指令,CMD更像默认的一个可选项。
一个image只作一个单一的用途,就像一个可执行的命令时,建议使用ENTRYPOINT,把CMD做为默认参数(第三种形式CMD ["param1","param2"] (as default parameters to ENTRYPOINT)
)。由于通常而言,ENTRYPOINT是不被覆盖的(除非在run时显式使用--entrypoit),而CMD是defaults的选项,从前文的run命令格式docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
可知,用户能够在运行images时输入本身的COMMAND来覆盖默认的CMD。
3.ADD vs COPY
这两个好像都是把东西从host拷贝到docker的container里,官方比较以下:
Although ADD and COPY are functionally similar, generally speaking, COPY is preferred. That’s because it’s more transparent than ADD. COPY only supports the basic copying of local files into the container, while ADD has some features (like local-only tar extraction and remote URL support) that are not immediately obvious. Consequently, the best use for ADD is local tar file auto-extraction into the image, as in ADD rootfs.tar.xz /.
简单来讲,主要就两点区别:
官方建议用COPY(preferred)
参考连接