》命令列表

  • 帮助命令

    # 查看Docker版本信息
    docker version
    # 查看Docker系统信息,镜像、容器数量等
    docker info
    # 查看帮助文档
    docker --help
    docker <xxx> --help # xxx命令帮助
    
  • 镜像命令

    # 从远程仓库搜索镜像
    docker search mysql
    docker search mysql --filter=stars=1000 # 表示stars大于1000的
    
    # 从远程仓库拉取指定镜像到本地
    docker pull nginx
    docker pull nginx:1.15 	# 指定版本号
    
    # 查看本地镜像
    docker images
    docker images -a # 所有
    docker images -q # 只显示名字
    
    # 删除本地镜像(-f:强制)
    docker rmi -f nginx:1.15
    docker rmi -f nginx mysql          # 删除多个
    docker rmi -f $(docker images -aq) # 删除所有
    
    # 通过Dockerfile构建本地镜像
    docker build .
    docker build -t yasin/centos .                     # 指定tag:name[:tag]
    docker build -f ./Dockerfile .                     # 指定Dockerfile,默认当前目录下的Dockerfile
    docker build -f ./Dockerfile -t yasin/centos:0.1 . # 综合案例
    
    # 查看镜像构建过程
    docker history yasin/centos:0.1
    
    # 给本地镜像打标签tag
    docker tag yasin/centos:0.1 yasin/centos:0.2
    
  • 容器命令

    # 启动容器
    docker run centos:7
    docker run --name centos001 centos:7         # 指定容器名
    docker run -d centos:7                       # 后台运行
    docker run -it centos:7                      # 交互式启动(停止容器退出:exit,不停止容器退出:Ctrl+P+Q)
    docker run -P centos:7                       # 端口映射:[随机端口]
    docker run -p 80 centos:7                    # 端口映射:[容器端口] 
    docker run -p 8080:80 centos:7               # 端口映射:[主机端口]:[容器端口]
    docker run -p 192.168.0.1:8080:80 centos:7   # 端口映射:[主机IP]:[端口]:[容器端口]
    docker run -v /www:/www centos:7             # 挂载卷:[主机内目录]:[容器内目录][:ro][:rw]
    docker run --volumes-from centos001 centos:7 # 挂载卷:读取其他容器挂载配置(其他容器我们叫数据卷容器)
    docker run --rm centos:7                     # 停止容器即删除,一般用于临时测试容器
    docker run -e xx=yy mysql:5.7                # 环境配置:[参数名]=[参数值]
    docker run --link centos001 centos:7         # 网络连接link(不推荐)
    docker run --net net001 centos:7             # 自定义网络(默认[--net bridge],注:自定义网络net001需要提前创建好)
    docker run --name centos001 -it -d -p 80:80 -v /root/test:/test centos:7           # 综合案例1
    docker run --name centos002 -it -d -p 8080:80 --volumes-from centos001 centos:7    # 综合案例2
    docker run --name mysql001 -d -p 33060:3306 -e MYSQL_ROOT_PASSWORD=123456 \
    -v /data/mysql/conf:/etc/mysql/conf.d -v /data/mysql/data:/var/lib/mysql mysql:5.7 # 综合案例3
    docker run --name centos001 -it -d -p 80:80 --net net001 centos:7                  # 综合案例4
    
    # 查看运行中的容器
    docker ps
    docker ps -a      # 所有正在运行和运行过的容器
    docker ps -a -n 2 # 所有正在运行和运行过的容器中取最近两个
    docker ps -q      # 只显示编号
    docker ps -l      # 最后一次创建的容器
    
    # 删除容器
    docker rm -f centos001
    docker rm -flv centos001        # l:链接,v:数据卷
    docker rm -flv $(docker ps -aq) # 删除所有容器以及数据卷、链接
    docker ps -aq | xargs docker rm # 删除所有容器
    
    # 容器启动与停止
    docker start centos001
    docker restart centos001
    docker stop centos001
    docker kill centos001
    
    # 查看运行中的容器log
    docker logs centos001
    docker logs -n 100 centos001     # 显示最后100条
    docker logs --tail 100 centos001 # 显示最后100条
    docker logs -t centos001         # 格式化日志输出增加时间
    docker logs -f centos001         # 跟踪日志实时输出
    docker logs -tf -n 100 centos001 # 综合案例
    
    # 宿主机执行容器内部命令
    docker top centos001
    # 查看容器详细信息
    docker inspect centos001
    
    # 执行容器内部命令
    docker exec centos001 /bin/bash     # 进入容器后打开新的终端,exit后不会stop容器
    docker exec -it centos001 /bin/bash # 交互式
    docker exec -it centos001 ls -l /   # 直接执行内部命令
    docker attach centos001             # 进入容器内正在运行的终端,exit后会stop容器
    
    # 从容器拷贝文件到宿主机
    docker cp centos001:/test.txt ./
    
  • 网络命令

    # 网络操作
    docker network --help
    
    # 查看网络列表
    docker network ls
    
    # 添加网络
    docker network create net001                         # 创建网络
    docker network create --driver bridge net001         # 驱动:桥接(默认桥接)
    docker network create --subnet 192.168.0.0/16 net001 # 网段
    docker network create --gateway 192.168.0.1 net001   # 网关
    docker network create --subnet 192.168.0.0/16 --gateway 192.168.0.1 net001 # 综合案例
    
    # 检查网络配置详情
    docker network inspect net001
    
    # 将容器加入到自定义网络中(同一网路中的容器可以通过容器名ping通)
    docker network connect net001 centos01
    
    • subnet说明:
      • 192.168.0.0/8匹配192.*.*.*
      • 192.168.0.0/16匹配192.168.*.*
      • 192.168.0.0/24匹配192.168.0.*
  • 远程仓库命令

    # 提交本地容器为本地镜像
    docker commit -a="作者" -m="描述" centos001 yasin/centos:0.1
    
    # 发布本地镜像到远程仓库(前提:先登录)
    docker push yasin/centos:0.1
    
    # 登陆远程仓库
    docker login -u admin -p 123456
    
    # 登出远程仓库
    docker logout
    
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐