docker:docker命令总结与实战
简介
docker是内核级虚拟化容器。
docker 常用命令
1 | docker version 查看docker的版本号,包括客户端、服务端、依赖的Go等 |
例子
从image运行一个容器
docker run <image名>
具体:
1
2
3
4
5
6
7
8
9
10
11
12 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。1
2
3docker 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进入:1
2docker attach a76a9cbce0a0
daemon@a76a9cbce0a0:/var/atlassian/confluence$
命令有时候并不方便。当多个窗口同时 attach 到同一个容器的时候,所有窗口都会同步显示。当某个窗口因命令阻塞时,其他窗口也无法执行操作了
从image新建一个container执行指定命令
1 | docker run -ti cptactionhank/atlassian-confluence /bin/bash |
-i 交互
-t 终端
退出container但是保持运行
默认情况下,如果使用ctrl-c退出container,那么container也会stop
按ctrl-p ctrl-q可以退出到宿主机,而保持container仍然在运行
docker端口映射
docker run -p用法: docker run -ti -p [宿主机监听地址]:宿主机端口:container端口
1 | docker run -ti -p 0.0.0.0:88:89 cptactionhank/atlassian-confluence bash |
docker ps 查看端口映射情况1
2CONTAINER 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 <port>
与docker run -p <port>
区别:
-p 映射宿主机到container端口。expose不映射端口只在docker虚拟网卡上,打开对指定端口的访问。
docker文件共享
docker 宿主机与container共享文件docker run -v <宿主路径>:[container路径]
1 | docker run -ti -v /tmp:/tmp1 cptactionhank/atlassian-confluence bash |
查看已经挂载的路径:1
2
3
4
5docker 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
1 | docker commit -a='test' 7f157e173dad cptactionhank/atlassian-confluence:test |