docker_相关操作

来源:互联网 发布:linux 机器重启时间 编辑:程序博客网 时间:2024/06/16 06:14

docker 操作

容器(container )就是 镜像(image)run 之后生产的东西。

参考资料:

  • https://www.keakon.net/2016/03/07/Docker%E5%AD%A6%E4%B9%A0%E5%90%8E%E8%AE%B0

  • 信息查询,列出 container 和 image 的数量,宿主机器的操作系统,还有 CPU 核数与内存大小等

    $ docker info$ docker version
  • 列出所有镜像(image)

    $ docker imagesREPOSITORY          TAG                 IMAGE ID            CREATED             SIZEhello-world         latest              1815c82652c0        2 weeks ago         1.84kB
  • 列出所有运行中的容器(container)

    $ docker ps -aCONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                          PORTS               NAMES4f580deaaa45        hello-world         "/hello"            About a minute ago   Exited (0) About a minute ago                       festive_haibt
  • 停止容器(container)

    $ docker stop 25e170c7730d # 25e170c7730d 为 容器id25e170c7730d
  • 删除容器

    $ docker rm 25e170c7730d # 移除 容器25e170c7730d
  • 删除镜像 (image)

    • 删除镜像前必须先停掉引用到镜像的容器(container)

    • 参考资料:http://yaxin-cn.github.io/Docker/how-to-delete-a-docker-image.html

    • 比如删除一个 hello-world 镜像

    wilker@ubuntu:/etc/default$ docker images # 列出已有的镜像REPOSITORY          TAG                 IMAGE ID            CREATED             SIZEhello-world         latest              1815c82652c0        2 weeks ago         1.84kBwilker@ubuntu:/etc/default$ docker rmi 1815c82652c0 # 尝试删除 1815c82652c0 镜像,但是报了一个已停掉的 container 25e170c7730d 引用着Error response from daemon: conflict: unable to delete 1815c82652c0 (must be forced) - image is being used by stopped container 25e170c7730dwilker@ubuntu:/etc/default$ docker stop 25e170c7730d # 假设 容器没有停掉 这里停掉一下25e170c7730dwilker@ubuntu:/etc/default$ docker rm 25e170c7730d # 移除 容器25e170c7730dwilker@ubuntu:/etc/default$ docker rmi 1815c82652c0 # 最后再删除镜像Untagged: hello-world:latestUntagged: hello-world@sha256:f3b3b28a45160805bb16542c9531888519430e9e6d6ffc09d72261b0d26ff74fDeleted: sha256:1815c82652c03bfd8644afda26fb184f2ed891d921b20a0703b46768f9755c57Deleted: sha256:45761469c965421a92a69cc50e92c01e0cfa94fe026cdd1233445ea00e96289awilker@ubuntu:/etc/default$ docker images # ok,doneREPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
  • 启动已停止的容器,并进入容器

    wilker@ubuntu:~/Desktop$ docker ps -a # 先查看一下已停止的容器 STATUS=ExitedCONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS               NAMES62a1b3fcae28        my-python-base      "bash"                   About an hour ago   Exited (0) 2 seconds ago wilker@ubuntu:~$ docker start 62a1b3fcae28 # 启动容器62a1b3fcae28# 进入容器有两种方式# 方式一wilker@ubuntu:~$ docker exec -it 62a1b3fcae28 /bin/bashroot@62a1b3fcae28:/app## 方式二,attach后随便按个方向键wilker@ubuntu:~$ docker attach 62a1b3fcae28root@62a1b3fcae28:/app#
  • 批量清理相关

    • 杀死所有正在运行的容器
    $ docker kill $(docker ps -a -q)
    • 杀死所有正在运行的容器
    docker kill $(docker ps -a -q)
    • 删除所有已经停止的容器
    docker rm $(docker ps -a -q)
    • 删除所有未打 dangling 标签的镜像
    docker rmi $(docker images -q -f dangling=true)
    • 删除所有镜像
    docker rmi $(docker images -q)
    • 为这些命令创建别名
    # ~/.bash_aliases# 杀死所有正在运行的容器.alias dockerkill='docker kill $(docker ps -a -q)'# 删除所有已经停止的容器.alias dockercleanc='docker rm $(docker ps -a -q)'# 删除所有未打标签的镜像.alias dockercleani='docker rmi $(docker images -q -f dangling=true)'# 删除所有已经停止的容器和未打标签的镜像.alias dockerclean='dockercleanc || true && dockercleani'
  • 查找、拉取、推送 镜像

    wilker@ubuntu:~$ docker search registry # 查找NAME                                      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATEDregistry                                  The Docker Registry 2.0 implementation for...   1547      [OK]       konradkleine/docker-registry-frontend     Browse and modify your Docker registry in ...   151                  [OK]...wilker@ubuntu:~$ docker pull registry # 拉取Using default tag: latestlatest: Pulling from library/registry90f4dba627d6: Pull complete 3a754cdc94a5: Pull complete 0756a217635f: Pull complete f82b9495c796: Pull complete 154ef19ddee6: Pull complete Digest: sha256:5eaafa2318aa0c4c52f95077c2a68bed0b13f6d2b464835723d4de1484052299Status: Downloaded newer image for registry:latestwilker@ubuntu:~$ docker push yourId/myubuntu:16.04  # 将 本地镜像 yourId/myubuntu:16.04 推送到官方的docker hub,要求 yourId 为 你的账户id,The push refers to a repository [docker.io/yourId/myubuntu]c6eaa74cb40b: Pushed ac6cf829fa81: Pushed 
    • 推送到自己的私人仓库:docker_自建私有仓库.md
    wilker@ubuntu:~$ docker push 11.22.33.44:5000/python_img:16.04 # 推送到 ip 为 11.22.33.44,端口为 5000 的服务器上
  • 修改镜像名: $ docker tag 源镜像名 目的镜像名

    wilker@ubuntu:~$ docker tag myubuntu:16.04 wilker/myubuntu:16.04 #wilker@ubuntu:~$ docker imagesREPOSITORY          TAG                 IMAGE ID            CREATED             SIZEmyubuntu            16.04               37973fe59483        28 minutes ago      119MB # 可以看到 image id 相同wilker/myubuntu     16.04               37973fe59483        28 minutes ago      119MBwilker@ubuntu:~$ docker rmi myubuntu:16.04 # 移除原来的 镜像Untagged: myubuntu:16.04wilker@ubuntu:~$ docker imagesREPOSITORY          TAG                 IMAGE ID            CREATED             SIZEwilker/myubuntu     16.04               37973fe59483        29 minutes ago      119MB


参考资料

  • http://www.dongcoder.com/detail-535767.html
原创粉丝点击