陈同学
微服务
Accelerator
About
# Docker 常用命令 ## 常用命令 > [Docker commandline](https://docs.docker.com/engine/reference/commandline/docker/) ### docker build 构建镜像 ```shell docker build -t="cmsdev:v1.0" . #基于当前目录Dockerfile构建镜像 ``` ### docker cp 容器与本机文件复制 > docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH > docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH ```shell docker cp hscf-zabbix-agent-2.0:/etc/zabbix /zabbix ``` ### docker commit 将容器保存为镜像 > format: docker commit containerId repo/image:version ```shell docker commit 7f7045d72b2c repo.hjl.hscf.com:5000/zabbix-server:v1.0 ``` ### docker exec -it 到容器内执行命令 > format: docker exec -it containerId/containerName bash ```shell docker exec -it devdb bash ``` ### docker history 查看镜像的创建历史 ```shell docker history imageId ``` ### docker images 列出镜像 ```shell docker images | awk '/^.*none.*/ { print $3 }' #查看所有包含none的镜像 docker images --no-trunc #列出完整镜像ID的镜像 ``` ### docker inspect 分析镜像 ```shell docker inspect imageid ``` ### docker info 查看docker信息 ```shell docker info ``` ### docker load 将压缩包恢复为镜像 ```shell docker load < ubuntu_14.04.tar ``` ### docker login 登陆镜像库 ```shell docker login repo.hscf.com:5000 ``` ### docker logs 查看容器日志 ```shell docker logs containerId ``` ### docker kill 杀死容器 ```shell docker kill $(sudo docker ps -q) #停止所有已退出的容器 ``` ### docker ps 列出镜像 ```shell docker ps # 查看所有镜像 docker ps --no-trunc # 查看有完整容器ID的镜像 ``` ### docker push 推送镜像到镜像库 ```shell docker push repo.hscf.com:5000/cmsdev:v1.0 ``` ### docker restart 重启容器 ```shell docker restart containerId或containerName ``` ### docker rm 删除容器 ```shell docker rm $(sudo docker ps -a -q) # 删除所有容器 ``` ### docker rmi 删除镜像 删除所有包含 none的镜像 ```shell docker rmi -f $(docker images | awk '/^.*none.*/ { print $3 }') ``` ### docker run 运行容器 包含环境变量、磁盘挂载、端口映射、docker链接的例子 ```shell docker run --name zabbix-web-nginx-mysql \ --link zabbix-server-mysql:zabbix-server \ -e PHP_TZ="Asia/Shanghai" \ -v /u01/SCFDOCKER/registry:/var/lib/registry \ -p 8000:80 \ -d zabbix/zabbix-web-nginx-mysql ``` ### docker stop 停止容器运行 ```shell docker stop containerId ``` ### docker save 将镜像保存为压缩包 ```shell docker save -o ubuntu_14.04.tar ubuntu:14.04 ``` ### docker search 搜索镜像 ```shell docker search centos:latest ``` ### docker tag 镜像打tag 为镜像cmsdev:v1.0打一个tag ```shell docker tag cmsdev:v1.0 repo.hscf.com:5000/cmsdev:v1.0 ``` ### 添加host ```bash docker run -d --add-host host1:192.168.1.1 --add-host host2:192.168.1.2 nginx ``` ## 常用组合命令 ### 停止并删除所有容器 ```shell docker kill $(sudo docker ps -q) docker rm $(sudo docker ps -a -q) docker ps -a -q ``` ### 打tag并推送镜像 ```shell docker build -t="cmsdev:v1.0" docker tag cmsdev:v1.0 repo.hscf.com:5000/cmsdev:v1.0 docker push repo.hscf.com:5000/cmsdev:v1.0 ``` ### 删除含有 none的镜像 ```shell docker images | awk '/^.*none.*/ { print $3 }' docker rmi -f $(docker images | awk '/^.*none.*/ { print $3 }') ``` ### 保存镜像并恢复 ```shell docker save -o ubuntu_14.04.tar ubuntu:14.04 docker load < ubuntu_14.04.tar ``` ### 查看日志 ```shell docker logs --tail=200 containerId # 查看容器最后200行日志 docker logs -f containerId # 动态查看日志 ``` ```shell docker ps --no-trunc | grep cmsdev # 获取特定镜像的容器ID全程 cd /var/lib/docker/containers/containerFullId #进入容器所在目录 contailerFullId-json.log # 该文件就是容器的日志文件 ``` ### 查看资源使用情况 ```shell docker stats # 只查看当前状态 docker stats --no-stream # 只查看指定容器 docker stats --no-stream container1 container2 ```
本文由
cyj
创作,可自由转载、引用,但需署名作者且注明文章出处。
文章标题:
Docker常用命令
文章链接:
https://chenyongjun.vip/articles/26
扫码或搜索 cyjrun 关注微信公众号, 结伴学习, 一起努力