docker简要命令

  1. 查看所有镜像
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
local               v1.0                f9a2256f9bd5        4 weeks ago         1.11GB
hello-world         latest              bf756fb1ae65        4 months ago        13.3kB
ubuntu              14.04               6e4f1fe62ff1        5 months ago        197MB
  1. 查看所有容器
$ docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES
c193b4659c83        ubuntu:14.04        "/bin/bash"              46 minutes ago      Exited (127) 45 minutes ago                       crazy_kapitsa
c029d094cc66        ubuntu:14.04        "/bin/bash"              12 hours ago        Exited (130) 47 minutes ago                       trusting_meninsky
16e0ca0a8278        ubuntu:14.04        "/bin/bash"              12 hours ago        Created                                           jovial_williams
1e75ee3543c4        f9a2256f9bd5        "/bin/bash"              13 days ago         Exited (0) 6 days ago                             gifted_chatelet
89f14131acf6        ubuntu:14.04        "/bin/bash"              13 days ago         Exited (0) 13 days ago                            interesting_grothendieck
f9bd41b13305        f9a2256f9bd5        "/bin/bash"              4 weeks ago         Exited (0) 4 weeks ago                            festive_ardinghelli
cedd097b4b35        f9a2256f9bd5        "/bin/bash"              4 weeks ago         Exited (0) 4 weeks ago                            confident_robinson
31289a9d540d        f9a2256f9bd5        "/bin/bash"              4 weeks ago         Exited (127) 4 weeks ago                          vibrant_jang
38f84c3af7aa        f9a2256f9bd5        "/bin/bash"              4 weeks ago         Exited (130) 4 weeks ago                          romantic_faraday
f261fd053348        f9a2256f9bd5        "/bin/bash"              4 weeks ago         Exited (0) 4 weeks ago                            stoic_goldberg
da6b02b4f62f        f9a2256f9bd5        "/bin/bash"              4 weeks ago         Exited (0) 4 weeks ago                            determined_spence
91c00bb267e4        f9a2256f9bd5        "/bin/bash"              4 weeks ago         Exited (0) 4 weeks ago                            upbeat_lederberg
e1db7d853af6        f9a2256f9bd5        "/bin/bash"              4 weeks ago         Exited (130) 4 weeks ago                          elated_engelbart
a22a152dcacc        f9a2256f9bd5        "/bin/bash"              4 weeks ago         Exited (130) 4 weeks ago                          confident_blackburn
9d2a6fde8e66        f9a2256f9bd5        "/bin/bash --privile…"   4 weeks ago         Exited (2) 4 weeks ago                            recursing_keldysh
72f5f2704b66        f9a2256f9bd5        "/bin/bash"              4 weeks ago         Exited (130) 4 weeks ago                          sweet_matsumoto
5e62d09d0125        f9a2256f9bd5        "/bin/bash"              4 weeks ago         Exited (2) 4 weeks ago                            adoring_gagarin
a4533dca3d88        ubuntu:14.04        "/bin/bash"              4 weeks ago         Exited (130) 4 weeks ago                          affectionate_hopper
998778971bc6        ubuntu:14.04        "/bin/bash"              4 weeks ago         Exited (127) 4 weeks ago                          stupefied_varahamihira
2871f2a84e0d        hello-world         "/hello"                 4 weeks ago         Exited (0) 4 weeks ago                            tender_boyd
  1. 删除容器
$ docker container rm 2871f2a84e0d
2871f2a84e0d
  1. 删除镜像

删除镜像前需要先清除所有使用该镜像的容器,参考删除容器,否则报错Error response from daemon: conflict: unable to remove repository reference "hello-world:latest" (must force) - container 2871f2a84e0d is using its referenced image bf756fb1ae65

$ docker rmi bf756fb1ae65
Untagged: hello-world:latest
Untagged: hello-world@sha256:8e3114318a995a1ee497790535e7b88365222a21771ae7e53687ad76563e8e76
Deleted: sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b
Deleted: sha256:9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63
  1. 命令行方式运行docker
docker run -it ubuntu:14.04 /bin/bash

# 后台方式运行
docker run -itd ubuntu:14.04 /bin/bash
  1. 进入正在后台运行的容器

找到 container 的 CONTAINER ID

docker exec -it c7188be9bd7b /bin/bash
  1. 进入已经是离开状态的容器

找到 container 的 CONTAINER ID(我这是 05bf2d9b89c6),或者是它的 NAMES(自动分配,我这是 jovial_banzai)

docker restart 05bf2d9b89c6
docker exec -it 05bf2d9b89c6 /bin/bash
# 或者
# docker restart jovial_banzai
# docker exec -it jovial_banzai /bin/bash
  1. 容器export导出
$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
05bf2d9b89c6        ubuntu:14.04        "/bin/bash"         24 minutes ago      Up 5 minutes                            jovial_banzai

$ docker export 05bf2d9b89c6 > myContainer.tar
$ ll -h
-rw-r--r-- 1 wons wons 189M 5月  22 10:48 myContainer.tar
  1. 容器import导入
$ docker import myContainer.tar
sha256:2b6a463bfb9ec9fc39fa5637b73fa791be322899873fd7611c7024e33526772d

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              2b6a463bfb9e        5 seconds ago       188MB
local               v1.0                f9a2256f9bd5        4 weeks ago         1.11GB
ubuntu              14.04               6e4f1fe62ff1        5 months ago        197MB

# 给新的镜像命个名
$ docker tag 2b6a463bfb9e b2g/ubuntu14.04:v1.0-beta
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
b2g/ubuntu14.04     v1.0-beta           2b6a463bfb9e        7 minutes ago       188MB
local               v1.0                f9a2256f9bd5        4 weeks ago         1.11GB
ubuntu              14.04               6e4f1fe62ff1        5 months ago        197MB

# 运行它
$ docker run -it b2g/ubuntu14.04:v1.0-beta /bin/sh
$ root@fea8d98a2e34:/# 
  1. commit保存对容器的修改至新镜像

给容器配置编译环境后,可以直接使用 docker commit 工具做出一个新镜像:

$ docker commit busy_bohr b2g/ubuntu14.04:v1.0-beta-2
sha256:5b1bb6106bd6247ea513eea2fe9c482f70784e5b5684faadf781b898545a3b11
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
b2g/ubuntu14.04     v1.0-beta-2         5b1bb6106bd6        3 seconds ago       664MB
b2g/ubuntu14.04     v1.0-beta           2b6a463bfb9e        41 minutes ago      188MB
local               v1.0                f9a2256f9bd5        4 weeks ago         1.11GB
ubuntu              14.04               6e4f1fe62ff1        5 months ago        197MB
  1. 镜像save导出
docker save -o images.tar b2g/ubuntu14.04:v1.0-beta
  1. 镜像load导入
docker load -i images.tar
  1. 挂载宿主目录
docker run -it -v /home/wons/TestMount:/mnt b2g/ubuntu14.04:v1.0-beta-2 /bin/bash
posted @ 2020-05-22 19:43  汪淼焱  阅读(324)  评论(0编辑  收藏  举报