[版权声明] 本站内容采用 知识共享署名-非商业性使用-相同方式共享 3.0 中国大陆 (CC BY-NC-SA 3.0 CN) 进行许可。
部分内容和资源来自网络,纯学习研究使用。如有侵犯您的权益,请及时联系我,我将尽快处理。
如转载请注明来自: Broly的博客,本文链接: docker常用命令收集
主要收集日常使用的Docker命令。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
[root@localhost ~]# docker --help Usage: docker [OPTIONS] COMMAND [arg...] docker [ --help | -v | --version ] A self-sufficient runtime for containers. Options: --config=~/.docker Location of client config files -D, --debug Enable debug mode -H, --host=[] Daemon socket(s) to connect to -h, --help Print usage -l, --log-level=info Set the logging level --tls Use TLS; implied by --tlsverify --tlscacert=~/.docker/ca.pem Trust certs signed only by this CA --tlscert=~/.docker/cert.pem Path to TLS certificate file --tlskey=~/.docker/key.pem Path to TLS key file --tlsverify Use TLS and verify the remote -v, --version Print version information and quit Commands: attach Attach to a running container build Build an image from a Dockerfile commit Create a new image from a container's changes cp Copy files/folders between a container and the local filesystem create Create a new container diff Inspect changes on a container's filesystem events Get real time events from the server exec Run a command in a running container export Export a container's filesystem as a tar archive history Show the history of an image images List images import Import the contents from a tarball to create a filesystem image info Display system-wide information inspect Return low-level information on a container, image or task kill Kill one or more running containers load Load an image from a tar archive or STDIN login Log in to a Docker registry. logout Log out from a Docker registry. logs Fetch the logs of a container network Manage Docker networks node Manage Docker Swarm nodes pause Pause all processes within one or more containers port List port mappings or a specific mapping for the container ps List containers pull Pull an image or a repository from a registry push Push an image or a repository to a registry rename Rename a container restart Restart a container rm Remove one or more containers rmi Remove one or more images run Run a command in a new container save Save one or more images to a tar archive (streamed to STDOUT by default) search Search the Docker Hub for images service Manage Docker services start Start one or more stopped containers stats Display a live stream of container(s) resource usage statistics stop Stop one or more running containers swarm Manage Docker Swarm tag Tag an image into a repository top Display the running processes of a container unpause Unpause all processes within one or more containers update Update configuration of one or more containers version Show the Docker version information volume Manage Docker volumes wait Block until a container stops, then print its exit code Run 'docker COMMAND --help' for more information on a command. |
常用命令:
镜像管理
docker images
列出本地所有镜像
docker search <IMAGE_ID/NAME>
查找image
docker pull <IMAGE_ID>
拉取image
docker push <IMAGE_ID>
推送image
docker rmi <IMAGE_ID>
删除image
容器管理
docker run -itd -p 8080:8080 --name my-container-name <IMAGE_ID>
-i:标准输入给容器
-t:分配一个虚拟终端 /bin/bash:执行bash脚本
-d:以守护进程方式运行(后台)
-P:默认匹配docker容器的5000端口号到宿主机的49153 to 65535端口
-p <HOT_PORT>:<CONTAINER_PORT>:指定端口号
--name: 指定容器的名称
--rm:退出时删除容器。需要注意的是容器运行在后台模式下,是不能使用--rm选项的
docker stop <CONTAINER_ID>
停止container
docker start <CONTAINER_ID>
重新启动container
docker ps -a:
-l:显示最后启动的容器
-a:同时显示停止的容器,默认只显示启动状态
docker attach <CONTAINER_ID>
连接到启动的容器
docker logs <CONTAINER_ID>
输出容器日志
-f:实时输出
docker cp <CONTAINER_ID>:path hostpath
复制容器内的文件到宿主机目录上
docker rm <CONTAINER_ID>
删除容器
docker rm docker ps -a -q
删除所有容器
docker kill <CONTAINER_ID>
强制关闭停止容器
docker wait <CONTAINER_ID>
阻塞对容器的其他调用方法,直到容器停止后退出
docker top <CONTAINER_ID>
查看容器中运行的进程
docker diff <CONTAINER_ID>
查看容器中的变化
docker inspect <CONTAINER_ID>
查看容器详细信息(输出为Json)
-f:查找特定信息,如docker inspect -f '{{.NetworkSettings.IPAddress }}'
docker commit -m "comment" -a "author" <CONTAINER_ID> ouruser/imagename:tag
提交容器
docker extc -it <CONTAINER> <COMMAND>
在容器里执行命令,并输出结果
网络管理
docker run -P
随机分配端口号
docker run -p 5000:5000
绑定特定端口号(主机的所有网络接口的5000端口均绑定容器的5000端口)
docker run -p 127.0.0.1:5000:5000
绑定主机的特定接口的端口号
docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py
绑定udp端口号
docker port <CONTAINER_ID> 5000
查看容器的5000端口对应本地机器的IP和端口号
docker run -d -P --name <CONTAINER_NAME> --link <CONTAINER_NAME_TO_LINK>:<ALIAS>
使用Docker Linking连接容器
数据管理
Data Volumes是在一个或多个容器里指定的特殊目录。数据卷可以在容器间共享和重复使用,可以直接修改容器卷的数据
容器卷里的数据不会被包含到镜像中,容器卷保持到没有容器再使用它。
可以在容器启动的时候添加-v参数指定容器卷,也可以在Dockerfile里用VOLUMN命令添加
docker run -d -P --name web -v /webapp training/webapp python app.py
也可以将容器卷挂载到宿主机目录或宿主机的文件上:
docker run -d -P --name web -v <宿主机目录>:<容器目录> training/webapp python app.py
可以通过指定ro,将权限改为只读
docker run -d -P --name web -v <宿主机目录>:<容器目录>:ro training/webapp python app.py
在一个容器创建容器卷后,其他容器便可以通过--volumes-from共享这个容器卷数据,如下:
docker run -d -v /dbdata --name db1 training/postgres echo Data-only container for postgres
首先启动了一个容器,并为这个容器增加一个数据卷/dbdata,然后启动另一个容器,共享这个数据卷
docker run -d --volumes-from db1 --name db2 training/postgres
此时db2使用了db1的容器卷,当容器db1被删除时,容器卷也不会被删除,只有所有容器不再使用此容器卷时,才会被删除
docker rm -v
除了共享数据外,容器卷另一个作用是用来备份、恢复和迁移数据
docker run --volumes-from db1 -v /home/backup:/backup ubuntu tar cvf /backup/backup.tar /dbdata
启动一个容器数据卷使用db1容器的数据卷,同时新建立一个数据卷指向宿主机目录/home/backup,将/dbdata目录的数据压缩为/backup/backup.tar
docker run -v /dbdata --name dbdata2 ubuntu /bin/bash
docker run --volumes-from dbdata2 -v /home/backup:/backup busybox tar xvf /backup/backup.tar
启动一个容器,同时把backup.tar的内容解压到容器的backup