1.基础类

  1. # 查看docker版本
  2. docker version
  3. # 显示docker系统信息
  4. docker info
  5. # 日志信息
  6. docker logs
  7. #故障检查
  8. service docker status
  9. # 启动关闭docker
  10. sudo service docker start|stop

2.日志类

查看容器日志

  1. docker logs -f <容器名orID>

3.容器类

docker容器可以理解为在沙盒中运行的进程 这个沙盒包含了该进程运行所必须的资源,包括文件系统、系统类库、shell 环境等等。但这个沙盒默认是不会运行任何程序的。你需要在沙盒中运行一个进程来启动某一个容器。这个进程是该容器的唯一进程,所以当该进程结束的时候,容器也会完全的停止。
查看容器信息

  1. # 查看当前运行的容器
  2. docker ps
  3. # 查看全部容器
  4. docker ps -a
  5. # 查看全部容器的id和信息
  6. docker ps -a -q
  7. # 查看全部容器占用的空间
  8. docker ps -as
  9. # 查看一个正在运行容器进程,支持 ps 命令参数
  10. docker top
  11. # 查看容器的示例id
  12. sudo docker inspect -f '{{.Id}}' [id]
  13. # 检查镜像或者容器的参数,默认返回 JSON 格式
  14. docker inspect

3.1:容器同步命令

  1. # 保存对容器的修改
  2. docker commit
  3. # 保存某个容器成为一个镜像
  4. docker commit -a "user" -m "commit info" [CONTAINER] [imageName]:[imageTag]
  5. # 推送一个容器到中心仓库
  6. docker login --username=[userName] --password=[pwd] [registryURL]
  7. ## 建议登录后查看 docker info
  8. docker tag [imageID] [remoteURL]:[imageTag]
  9. docker push [remoteURL]:[imageTag]
  10. # 拉取提交的容器
  11. docker pull [remoteURL]:[imageTag]
  12. # 对比容器的改动
  13. docker diff
  14. # 附加到一个运行的容器上
  15. docker attach

3.2:创建删除容器

  1. # 创建一个容器命名为 test 使用镜像daocloud.io/library/ubuntu
  2. docker create -it --name test daocloud.io/library/ubuntu
  3. # 创建并启动一个容器 名为 test 使用镜像daocloud.io/library/ubuntu
  4. docker run --name test daocloud.io/library/ubuntu
  5. # 删除一个容器
  6. docker rm [容器id]
  7. # 删除所有容器
  8. docker rm `docker ps -a -q`
  9. # 根据Dockerfile 构建
  10. docker build -t [image_name] [Dockerfile_path]

3.3:docker容器随系统自启

  1. #docker容器随系统自启
  2. docker run --restart=always
  • no – 默认值,如果容器挂掉不自动重启
  • on-failure – 当容器以非 0 码退出时重启容器,同时可接受一个可选的最大重启次数参数 (e.g. on-failure:10).
  • always – 不管退出码是多少都要重启

3.4:容器资源限制参数

  1. # 限制内存最大使用
  2. -m 1024m --memory-swap=1024m
  3. # 限制容器使用CPU
  4. --cpuset-cpus="0,1"

3.5:把一个正在运行的容器保存为镜像

  1. docker commit <CONTAIN-ID> <IMAGE-NAME>

3.6:启动停止容器等操作

  1. docker start|stop|restart [id]
  2. # 暂停|恢复 某一容器的所有进程
  3. docker pause|unpause [id]
  4. # 杀死一个或多个指定容器进程
  5. docker kill -s KILL [id]
  6. # 停止全部运行的容器
  7. docker stop `docker ps -q`
  8. # 杀掉全部运行的容器
  9. docker kill -s KILL `docker ps -q`

3.7:交互式进入容器

  1. sudo docker exec -it {{containerName or containerID}} bash
  2. sudo docker exec -i {{containerName or containerID}} bash
  3. sudo docker exec -t {{containerName or containerID}} bash
  4. sudo docker exec -d {{containerName or containerID}} bash
  • 只用 -i 参数,由于没有分配伪终端,看起来像pipe执行一样。但是执行结果、命令返回值都可以正确获取
  • 只用 -t 参数,则可以看到一个 console 窗口,但是执行命令会发现由于没有获得stdin的输出,无法看到命令执行情况
  • 使用 -it 时,则和我们平常操作 console 界面类似,而且也不会像attach方式因为退出,导致整个容器退出
  • 使用 -d 参数,在后台执行一个进程。如果一个命令需要长时间进程,会很快返回

3.8:导入导出容器

  1. # 支持远程文件 .tar, .tar.gz, .tgz, .bzip, .tar.xz, .txz
  2. docker import
  3. # 导出
  4. docker export [id] >~/Downloads/ubuntu_nexus.tar

4.镜像操作

4.1:远程镜像

  1. docker login
  1. docker search
  2. # 搜索处收藏数不小于 3 ,并且能够自动化构建的 django 镜像,并且完整显示镜像描述
  3. docker search -s 3 --automated --no-trunc django
  4. docker pull
  5. # 拉取ubuntu最新的镜像
  6. docker pull ubuntu:latest
  7. # 服务器拉取个人动态,可选择时间区间
  8. docker events
  9. # 拉取个人从 2015/07/20 到 2015/08/08 的个人动态
  10. docker events --since="20150720" --until="20150808"

4.2:镜像同步操作

  1. # 标记本地镜像,将其归入某一仓库
  2. docker tag
  3. # 将 ID 为 5db5f84x1261 的容器标记为 mine/lnmp:0.2 镜像
  4. docker tag 5db5f84x1261 mine/lnmp:0.2
  5. # 将镜像推送至远程仓库,默认为 Docker Hub
  6. docker push

4.3:本地镜像

  1. # 列出本地所有镜像
  2. docker images
  3. # 本地镜像名为 ubuntu 的所有镜像
  4. docker images ubuntu
  5. # 查看指定镜像的创建历史
  6. docker history [id]
  7. # 本地移除一个或多个指定的镜像
  8. docker rmi
  9. # 移除本地全部镜像
  10. docker rmi `docker images -a -q`
  11. # 指定镜像保存成 tar 归档文件, docker load 的逆操作
  12. docker save
  13. # 将镜像 ubuntu:14.04 保存为 ubuntu14.04.tar 文件
  14. docker save -o ubuntu14.04.tar ubuntu:14.04
  15. # 从 tar 镜像归档中载入镜像, docker save 的逆操作
  16. docker load
  17. # 上面命令的意思是将 ubuntu14.04.tar 文件载入镜像中
  18. docker load -i ubuntu14.04.tar
  19. docker load < /home/save.tar
  20. # 构建自己的镜像
  21. docker build -t <镜像名> <Dockerfile路径>
  22. docker build -t xx/gitlab .

5.Docker run

5.1:后台运行(-d)、并暴露端口

  1. docker run -d -p 127.0.0.1:33301:22 centos6-ssh

5.2:run 命令详解

  1. -a, --attach=[] Attach to STDIN, STDOUT or STDERR 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项
  2. --add-host=[] Add a custom host-to-IP mapping (host:ip)
  3. --blkio-weight=0 Block IO (relative weight), between 10 and 1000
  4. -c, --cpu-shares=0 CPU shares (relative weight)
  5. --cap-add=[] Add Linux capabilities
  6. --cap-drop=[] Drop Linux capabilities
  7. --cgroup-parent= Optional parent cgroup for the container
  8. --cidfile= Write the container ID to the file
  9. --cpu-period=0 Limit CPU CFS (Completely Fair Scheduler) period
  10. --cpu-quota=0 Limit the CPU CFS quota
  11. --cpuset-cpus= CPUs in which to allow execution (0-3, 0,1) 绑定容器到指定CPU运行
  12. --cpuset-mems= MEMs in which to allow execution (0-3, 0,1) 绑定容器到指定MEM运行
  13. -d, --detach=false Run container in background and print container ID 后台运行容器,并返回容器ID
  14. --device=[] Add a host device to the container
  15. --dns=[] Set custom DNS servers 指定容器使用的DNS服务器,默认和宿主一致
  16. --dns-search=[] Set custom DNS search domains 指定容器DNS搜索域名,默认和宿主一致
  17. -e, --env=[] Set environment variables 设置环境变量
  18. --entrypoint= Overwrite the default ENTRYPOINT of the image
  19. --env-file=[] Read in a file of environment variables 从指定文件读入环境变量
  20. --expose=[] Expose a port or a range of ports
  21. -h, --hostname= Container host name 指定容器的hostname
  22. --help=false Print usage
  23. -i, --interactive=false Keep STDIN open even if not attached 以交互模式运行容器,通常与 -t 同时使用
  24. --ipc= IPC namespace to use
  25. -l, --label=[] Set meta data on a container
  26. --label-file=[] Read in a line delimited file of labels
  27. --link=[] Add link to another container
  28. --log-driver= Logging driver for container
  29. --log-opt=[] Log driver options
  30. --lxc-conf=[] Add custom lxc options
  31. -m, --memory= Memory limit
  32. --mac-address= Container MAC address (e.g. 92:d0:c6:0a:29:33)
  33. --memory-swap= Total memory (memory + swap), '-1' to disable swap
  34. --name= Assign a name to the container 为容器指定一个名称
  35. --net=bridge Set the Network mode for the container 指定容器的网络连接类型,支持 bridge/host/none/container:<name|id> 四种类型
  36. --oom-kill-disable=false Disable OOM Killer
  37. -P, --publish-all=false Publish all exposed ports to random ports
  38. -p, --publish=[] Publish a container's port(s) to the host
  39. --pid= PID namespace to use
  40. --privileged=false Give extended privileges to this container
  41. --read-only=false Mount the container's root filesystem as read only
  42. --restart=no Restart policy to apply when a container exits
  43. --rm=false Automatically remove the container when it exits
  44. --security-opt=[] Security Options
  45. --sig-proxy=true Proxy received signals to the process
  46. -t, --tty=false Allocate a pseudo-TTY 为容器重新分配一个伪输入终端,通常与 -i 同时使用
  47. -u, --user= Username or UID (format: <name|uid>[:<group|gid>])
  48. --ulimit=[] Ulimit options
  49. --uts= UTS namespace to use
  50. -v, --volume=[] Bind mount a volume
  51. --volumes-from=[] Mount volumes from the specified container(s)
  52. -w, --workdir= Working directory inside the container