## 简介
---
docker是内核级虚拟化容器。
## docker 常用命令 --- ``` 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 过滤掉中间镜像(现有镜像的父镜像)
docker images -a 列出所有的images
docker ps 默认显示正在运行中的container
docker ps -l 显示最后一次创建的container,包括未运行的
docker ps -a 显示所有的container,包括未运行的
docker logs 查看container的日志,也就是执行命令的一些输出
docker rm 删除一个或多个container
docker rm `docker ps -a -q` 删除所有的container
docker ps -a -q | xargs docker rm 同上, 删除所有的container
docker rmi 删除一个或多个image
docker start/stop/restart 开启/停止/重启container
docker start -i 启动一个container并进入交互模式
docker attach attach一个运行中的container(进入一个正在运行的container)
使用 attach 命令有时候并不方便。当多个窗口同时 attach 到同一个容器的时候,所有窗口都会同步显示。当某个窗口因命令阻塞时,其他窗口也无法执行操作了
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可选
docker build 寻找path路径下名为的Dockerfile的配置文件,使用此配置生成新的image
docker build -t repo[:tag] 同上,可以指定repo和可选的tag
docker build - < 使用指定的dockerfile配置文件,docker以stdin方式获取内容,使用此配置生成新的image
docker port 查看本地哪个端口映射到container的指定端口,或者用docker ps 也可以看到。
```
## 例子
---
### 从image运行一个容器
---
`docker run `
具体: ``` docker run cptactionhank/atlassian-confluence If you encounter issues starting up Confluence, please see the Installation guide at http://confluence.atlassian.com/display/DOC/Confluence+Installation+Guide Server startup logs are located in /opt/atlassian/confluence/logs/catalina.out 22-Mar-2017 01:42:46.438 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8090"] 22-Mar-2017 01:42:46.465 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read 22-Mar-2017 01:42:46.467 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 561 ms 22-Mar-2017 01:42:46.474 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Tomcat-Standalone 22-Mar-2017 01:42:46.474 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.0.41 2017-03-22 01:42:48,622 INFO [localhost-startStop-1] [com.atlassian.confluence.lifecycle] contextInitialized Starting Confluence 6.0.6 [build 7104 based on commit hash 194f920205d6dc337c4fd055ad1d0a7b56409ec8] 22-Mar-2017 01:42:56.603 INFO [localhost-startStop-2] org.apache.catalina.startup.HostConfig.deployDescriptor Deploying configuration descriptor /opt/atlassian/confluence/conf/Standalone/localhost/synchrony-proxy.xml 22-Mar-2017 01:42:56.608 WARNING [localhost-startStop-2] org.apache.catalina.startup.SetContextPropertiesRule.begin [SetContextPropertiesRule]{Context} Setting property 'debug' to '0' did not find a matching property. ``` ### 进入一个正在运行的容器 --- 先查看运行的容器ID。 ``` docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a76a9cbce0a0 cptactionhank/atlassian-confluence:latest "/docker-entrypoint. 5 minutes ago Up 5 minutes 8090/tcp silly_hopper ``` 然后用attach进入: ``` docker attach a76a9cbce0a0 daemon@a76a9cbce0a0:/var/atlassian/confluence$ ``` 命令有时候并不方便。当多个窗口同时 attach 到同一个容器的时候,所有窗口都会同步显示。当某个窗口因命令阻塞时,其他窗口也无法执行操作了 ### 从image新建一个container执行指定命令 --- ``` docker run -ti cptactionhank/atlassian-confluence /bin/bash daemon@a76a9cbce0a0:/var/atlassian/confluence$ ``` -i 交互
-t 终端 ### 退出container但是保持运行 --- 默认情况下,如果使用ctrl-c退出container,那么container也会stop 按ctrl-p ctrl-q可以退出到宿主机,而保持container仍然在运行 ### docker端口映射 --- docker run -p用法: `docker run -ti -p [宿主机监听地址]:宿主机端口:container端口` ``` docker run -ti -p 0.0.0.0:88:89 cptactionhank/atlassian-confluence bash ``` docker ps 查看端口映射情况 ``` CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0ca4efd74460 cptactionhank/atlassian-confluence:latest "/docker-entrypoint. 11 minutes ago Up 11 minutes 8090/tcp, 0.0.0.0:88->89/tcp ``` #### docker run --expose 与 docker run -p 区别 --- `docker run --expose`与`docker run -p `区别:
-p 映射宿主机到container端口。expose不映射端口只在docker虚拟网卡上,打开对指定端口的访问。
### docker文件共享
---
docker 宿主机与container共享文件`docker run -v <宿主路径>:[container路径]`
```bash
docker run -ti -v /tmp:/tmp1 cptactionhank/atlassian-confluence bash
daemon@480a375cf60c:/var/atlassian/confluence$
```
查看已经挂载的路径:
```bash
docker run -ti -v /tmp:/tmp1 cptactionhank/atlassian-confluence bash
daemon@480a375cf60c:/var/atlassian/confluence$ mount|grep -i tmp
tmpfs on /dev type tmpfs (rw,nosuid,mode=755)
shm on /dev/shm type tmpfs (rw,nosuid,nodev,noexec,relatime,size=65536k)
/dev/dm-1 on /tmp1 type ext4 (rw,relatime,errors=remount-ro,data=ordered)
```
## docker commit 固化container为images
---
```bash
docker commit -a='test' 7f157e173dad cptactionhank/atlassian-confluence:test
32855aa7f8ca15a38b19c21639282d0001ec5bb2f609dd82f5fd0d9199b88dbb
```
## docker 常用命令 --- ``` docker version 查看docker的版本号,包括客户端、服务端、依赖的Go等 docker info 查看系统(docker)层面信息,包括管理的images, containers数等 docker search
具体: ``` docker run cptactionhank/atlassian-confluence If you encounter issues starting up Confluence, please see the Installation guide at http://confluence.atlassian.com/display/DOC/Confluence+Installation+Guide Server startup logs are located in /opt/atlassian/confluence/logs/catalina.out 22-Mar-2017 01:42:46.438 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8090"] 22-Mar-2017 01:42:46.465 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read 22-Mar-2017 01:42:46.467 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 561 ms 22-Mar-2017 01:42:46.474 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Tomcat-Standalone 22-Mar-2017 01:42:46.474 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.0.41 2017-03-22 01:42:48,622 INFO [localhost-startStop-1] [com.atlassian.confluence.lifecycle] contextInitialized Starting Confluence 6.0.6 [build 7104 based on commit hash 194f920205d6dc337c4fd055ad1d0a7b56409ec8] 22-Mar-2017 01:42:56.603 INFO [localhost-startStop-2] org.apache.catalina.startup.HostConfig.deployDescriptor Deploying configuration descriptor /opt/atlassian/confluence/conf/Standalone/localhost/synchrony-proxy.xml 22-Mar-2017 01:42:56.608 WARNING [localhost-startStop-2] org.apache.catalina.startup.SetContextPropertiesRule.begin [SetContextPropertiesRule]{Context} Setting property 'debug' to '0' did not find a matching property. ``` ### 进入一个正在运行的容器 --- 先查看运行的容器ID。 ``` docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a76a9cbce0a0 cptactionhank/atlassian-confluence:latest "/docker-entrypoint. 5 minutes ago Up 5 minutes 8090/tcp silly_hopper ``` 然后用attach进入: ``` docker attach a76a9cbce0a0 daemon@a76a9cbce0a0:/var/atlassian/confluence$ ``` 命令有时候并不方便。当多个窗口同时 attach 到同一个容器的时候,所有窗口都会同步显示。当某个窗口因命令阻塞时,其他窗口也无法执行操作了 ### 从image新建一个container执行指定命令 --- ``` docker run -ti cptactionhank/atlassian-confluence /bin/bash daemon@a76a9cbce0a0:/var/atlassian/confluence$ ``` -i 交互
-t 终端 ### 退出container但是保持运行 --- 默认情况下,如果使用ctrl-c退出container,那么container也会stop 按ctrl-p ctrl-q可以退出到宿主机,而保持container仍然在运行 ### docker端口映射 --- docker run -p用法: `docker run -ti -p [宿主机监听地址]:宿主机端口:container端口` ``` docker run -ti -p 0.0.0.0:88:89 cptactionhank/atlassian-confluence bash ``` docker ps 查看端口映射情况 ``` CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0ca4efd74460 cptactionhank/atlassian-confluence:latest "/docker-entrypoint. 11 minutes ago Up 11 minutes 8090/tcp, 0.0.0.0:88->89/tcp ``` #### docker run --expose 与 docker run -p 区别 --- `docker run --expose