Docker [Docker 学习笔记] 容器的启动、运行,终止与导入导出

大海 · 2018年09月23日 · 1922 次阅读
[sks@bogon ~]$ docker pull ubuntu:18.04
[sks@bogon ~]$ docker pull centos:7
[sks@bogon ~]$ docker run ubuntu:18.04 /bin/sh -c "while true; do echo hello world; sleep 1; done"
hello world
hello world
hello world
hello world
[sks@bogon ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              4ab4c602aa5e        2 weeks ago         1.84kB
ubuntu              18.04               cd6d8154f1e1        2 weeks ago         84.1MB
centos              7                   5182e96772bf        6 weeks ago         200MB
[sks@bogon ~]$ docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
1ae6efb8e977        ubuntu:18.04        "/bin/sh -c 'while t…"   8 minutes ago       Up 8 minutes                            pensive_sammet
[sks@bogon ~]$ docker run ubuntu:18.04 /bin/echo "hello world"
hello world
[sks@bogon ~]$ docker run -t -i ubuntu:18.04 /bin/bash
root@e83b0fe98af4:/# pwd
/
root@e83b0fe98af4:/# ls
bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr
root@e83b0fe98af4:/# ps
   PID TTY          TIME CMD
     1 pts/0    00:00:00 bash
    10 pts/0    00:00:00 ps
root@e83b0fe98af4:/# exit                  
exit
[sks@bogon ~]$ docker container logs 1ae6efb8e977
hello world
hello world
hello world
hello world

[sks@bogon ~]$ docker container stop 1ae6efb8e977
1ae6efb8e977
[sks@bogon ~]$ docker container restart 1ae6efb8e977
1ae6efb8e977
[sks@bogon ~]$ docker container logs 1ae6efb8e977
hello world
hello world
hello world
hello world
hello world
[sks@bogon ~]$ docker container stop 1ae6efb8e977
1ae6efb8e977
[sks@bogon ~]$ docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES
e83b0fe98af4        ubuntu:18.04        "/bin/bash"              4 minutes ago       Exited (0) 3 minutes ago                         sharp_visvesvaraya
fd517728bf21        ubuntu:18.04        "/bin/echo 'hello wo…"   4 minutes ago       Exited (0) 4 minutes ago                         awesome_thompson
1ae6efb8e977        ubuntu:18.04        "/bin/sh -c 'while t…"   14 minutes ago      Exited (137) 7 seconds ago                       pensive_sammet
2a81ce65766d        ubuntu:18.04        "/bin/sh -c 'while t…"   14 minutes ago      Exited (0) 14 minutes ago                        hopeful_chandrasekhar
dc785f93714f        ubuntu:18.04        "/bin/sh -c 'while t…"   14 minutes ago      Exited (2) 14 minutes ago                        sleepy_heyrovsky
ca45b3cb3de8        ubuntu:18.04        "/bin/bash"              17 minutes ago      Exited (0) 15 minutes ago                        naughty_perlman
78d2f5e39206        ubuntu:18.04        "/bin/echo 'hello wo…"   18 minutes ago      Exited (0) 18 minutes ago                        sleepy_neumann
57ccb6d51ec8        hello-world         "/hello"                 14 hours ago        Exited (0) 14 hours ago                          stupefied_mcnulty
6c1599e062c4        hello-world         "/hello"                 14 hours ago        Exited (0) 14 hours ago                          agitated_beaver
31757d8a0b21        hello-world         "/hello"                 14 hours ago        Exited (0) 14 hours ago                          focused_carson
575ed6223341        hello-world         "/hello"                 14 hours ago        Exited (0) 14 hours ago                          lucid_chatterjee
[sks@bogon ~]$ docker container stop 1ae6efb8e977
1ae6efb8e977
[sks@bogon ~]$ docker container stop 2a81ce65766d
2a81ce65766d
[sks@bogon ~]$ docker container stop dc785f93714f
dc785f93714f
[sks@bogon ~]$ docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                            PORTS               NAMES
e83b0fe98af4        ubuntu:18.04        "/bin/bash"              5 minutes ago       Exited (0) 4 minutes ago                              sharp_visvesvaraya
fd517728bf21        ubuntu:18.04        "/bin/echo 'hello wo…"   5 minutes ago       Exited (0) 5 minutes ago                              awesome_thompson
1ae6efb8e977        ubuntu:18.04        "/bin/sh -c 'while t…"   15 minutes ago      Exited (137) About a minute ago                       pensive_sammet
2a81ce65766d        ubuntu:18.04        "/bin/sh -c 'while t…"   15 minutes ago      Exited (0) 15 minutes ago                             hopeful_chandrasekhar
dc785f93714f        ubuntu:18.04        "/bin/sh -c 'while t…"   16 minutes ago      Exited (2) 16 minutes ago                             sleepy_heyrovsky
ca45b3cb3de8        ubuntu:18.04        "/bin/bash"              19 minutes ago      Exited (0) 17 minutes ago                             naughty_perlman
78d2f5e39206        ubuntu:18.04        "/bin/echo 'hello wo…"   19 minutes ago      Exited (0) 19 minutes ago                             sleepy_neumann
57ccb6d51ec8        hello-world         "/hello"                 14 hours ago        Exited (0) 14 hours ago                               stupefied_mcnulty
6c1599e062c4        hello-world         "/hello"                 14 hours ago        Exited (0) 14 hours ago                               agitated_beaver
31757d8a0b21        hello-world         "/hello"                 14 hours ago        Exited (0) 14 hours ago                               focused_carson
575ed6223341        hello-world         "/hello"                 14 hours ago        Exited (0) 14 hours ago                               lucid_chatterjee

[sks@bogon ~]$ docker run -dit ubuntu
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
Digest: sha256:de774a3145f7ca4f0bd144c7d4ffb2931e06634f11529653b23eba85aef8e378
Status: Downloaded newer image for ubuntu:latest
262d621f34e61e365d308ecefc9936500bcfbc1e8897a1b15f29b96f22d606ea
[sks@bogon ~]$ docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
262d621f34e6        ubuntu              "/bin/bash"         22 seconds ago      Up 21 seconds                           amazing_allen
[sks@bogon ~]$ docker attach 262d
root@262d621f34e6:/# ls
bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr
root@262d621f34e6:/# exit
exit
[sks@bogon ~]$ docker run -dit ubuntu
64a8d78ff1c0c0c048ef4908e937043431e258097429caa34115b96f6434f830
[sks@bogon ~]$ docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
64a8d78ff1c0        ubuntu              "/bin/bash"         8 seconds ago       Up 7 seconds                            naughty_montalcini
[sks@bogon ~]$ docker run -dit ubuntu
459cc685745c4ac506fd7e2c453491ff7cf0e9f546833d4b1873c981dfd65422
[sks@bogon ~]$ docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
459cc685745c        ubuntu              "/bin/bash"         7 seconds ago       Up 6 seconds                            determined_visvesvaraya
64a8d78ff1c0        ubuntu              "/bin/bash"         2 minutes ago       Up 2 minutes                            naughty_montalcini
[sks@bogon ~]$ docker run -dit ubuntu
31ea529dd65855123bda6a49fefaa8823a008dcdf401f9087a3db742e249db02
[sks@bogon ~]$ docker exec -i -t 31ea bash
root@31ea529dd658:/# exit
exit
[sks@bogon ~]$ 
[sks@bogon ~]$ docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES
31ea529dd658        ubuntu              "/bin/bash"              8 minutes ago       Up 8 minutes                                      distracted_elbakyan
459cc685745c        ubuntu              "/bin/bash"              9 minutes ago       Up 9 minutes                                      determined_visvesvaraya
64a8d78ff1c0        ubuntu              "/bin/bash"              12 minutes ago      Up 12 minutes                                     naughty_montalcini
262d621f34e6        ubuntu              "/bin/bash"              13 minutes ago      Exited (0) 12 minutes ago                         amazing_allen
e83b0fe98af4        ubuntu:18.04        "/bin/bash"              20 minutes ago      Exited (0) 19 minutes ago                         sharp_visvesvaraya
fd517728bf21        ubuntu:18.04        "/bin/echo 'hello wo…"   20 minutes ago      Exited (0) 20 minutes ago                         awesome_thompson
1ae6efb8e977        ubuntu:18.04        "/bin/sh -c 'while t…"   30 minutes ago      Exited (137) 16 minutes ago                       pensive_sammet
2a81ce65766d        ubuntu:18.04        "/bin/sh -c 'while t…"   31 minutes ago      Exited (0) 30 minutes ago                         hopeful_chandrasekhar
dc785f93714f        ubuntu:18.04        "/bin/sh -c 'while t…"   31 minutes ago      Exited (2) 31 minutes ago                         sleepy_heyrovsky
ca45b3cb3de8        ubuntu:18.04        "/bin/bash"              34 minutes ago      Exited (0) 32 minutes ago                         naughty_perlman
78d2f5e39206        ubuntu:18.04        "/bin/echo 'hello wo…"   34 minutes ago      Exited (0) 34 minutes ago                         sleepy_neumann
57ccb6d51ec8        hello-world         "/hello"                 15 hours ago        Exited (0) 15 hours ago                           stupefied_mcnulty
6c1599e062c4        hello-world         "/hello"                 15 hours ago        Exited (0) 15 hours ago                           agitated_beaver
31757d8a0b21        hello-world         "/hello"                 15 hours ago        Exited (0) 15 hours ago                           focused_carson
575ed6223341        hello-world         "/hello"                 15 hours ago        Exited (0) 15 hours ago                           lucid_chatterjee
[sks@bogon ~]$ docker export e83b0fe98af4 > ubuntu.tar
[sks@bogon ~]$ cat ubuntu.tar | docker import - test/ubuntu:v1.0
sha256:2c47cf17950f55d5ac256ff190d9b0d8520f9136a1bf79f25166ffb37fc7807d
[sks@bogon ~]$ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test/ubuntu         v1.0                2c47cf17950f        7 seconds ago       69.8MB
hello-world         latest              4ab4c602aa5e        2 weeks ago         1.84kB
ubuntu              18.04               cd6d8154f1e1        2 weeks ago         84.1MB
ubuntu              latest              cd6d8154f1e1        2 weeks ago         84.1MB
centos              7                   5182e96772bf        6 weeks ago         200MB

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册