docker根据容器创建镜像

 

创建一个镜像,执行里面的mengka.sh

 

 

1. 根据已经存在的镜像,创建容器

 

sh-3.2# docker run -ti ubuntu:mengka
root@9b56cabcf039:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

 

2. 在容器中,新建脚本

 

vi mengka.sh

#!/bin/bash
while true;do echo `date +%F%T`;sleep 1;done

每秒打印一次当前时间;

root@9b56cabcf039:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mengka.sh  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

 

3. 根据刚刚退出的容器,创建一个镜像

 

sh-3.2# docker ps -a
CONTAINER ID        IMAGE                                     COMMAND                CREATED             STATUS                       PORTS               NAMES
9b56cabcf039        ubuntu:14.04                              "/bin/bash"            5 minutes ago       Exited (130) 5 seconds ago                       pensive_yonath     
9c2b0d270c80        ubuntu:14.04                              "/bin/bash"            5 minutes ago       Exited (130) 5 minutes ago                       naughty_torvalds   
24869d1d79d3        hyy044101331/mengka-new-images:20150113   "/bin/bash"            7 minutes ago       Exited (0) 7 minutes ago                         fervent_hoover     
1b8b2033ff11        hyy044101331/mengka-new-images:20150113   "/bin/sh -c 'while t   43 minutes ago      Up 37 minutes                                    dreamy_hypatia     
5ef903292447        hyy044101331/mengka-new-images:20150113   "/bin/bash"            53 minutes ago      Up 45 minutes                                    evil_lumiere       


sh-3.2# docker commit -m "mengka test" -a "mengka.hyy" 9b56cabcf039 mengka-image-time01
058463d4646ce3ce827665614b0c917f0b8d8a0795fb4f722977744092858978
sh-3.2#
sh-3.2#


sh-3.2# docker images
REPOSITORY                       TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
mengka-image-time01              latest              058463d4646c        4 seconds ago       188.3 MB
mengka-new-images                20150113            dc27e692f33a        3 days ago          188.3 MB
mengka-new-images                latest              dc27e692f33a        3 days ago          188.3 MB
mengka_aa/mengka-new-images      20150113            dc27e692f33a        3 days ago          188.3 MB
hyy044101331/mengka-new-images   20150113            dc27e692f33a        3 days ago          188.3 MB
ubuntu                           14.04.1             8eaa4ff06b53        2 weeks ago         188.3 MB
ubuntu                           latest              8eaa4ff06b53        2 weeks ago         188.3 MB
ubuntu                           mengka              8eaa4ff06b53        2 weeks ago         188.3 MB
ubuntu                           trusty              8eaa4ff06b53        2 weeks ago         188.3 MB
ubuntu                           14.04               8eaa4ff06b53        2 weeks ago         188.3 MB
fedora                           latest              834629358fe2        2 weeks ago         241.3 MB

 

4. 利用刚刚创建的镜像,创建一个容器

docker run -d mengka-image-time01 /bin/sh mengka.sh

 

sh-3.2#  docker run -d mengka-image-time01 /bin/sh mengka.sh
18cab084f4f6e5103adedb6c91e05a191cea82193a804882e48deec20e5b66fc
sh-3.2#
sh-3.2#
sh-3.2# docker ps -a
CONTAINER ID        IMAGE                                     COMMAND                CREATED             STATUS                       PORTS               NAMES
18cab084f4f6        mengka-image-time01:latest                "/bin/sh mengka.sh"    5 seconds ago       Up 5 seconds                                     fervent_morse      
9b56cabcf039        ubuntu:14.04                              "/bin/bash"            8 minutes ago       Exited (130) 3 minutes ago                       pensive_yonath     
9c2b0d270c80        ubuntu:14.04                              "/bin/bash"            8 minutes ago       Exited (130) 8 minutes ago                       naughty_torvalds   
24869d1d79d3        hyy044101331/mengka-new-images:20150113   "/bin/bash"            10 minutes ago      Exited (0) 10 minutes ago                        fervent_hoover     
1b8b2033ff11        hyy044101331/mengka-new-images:20150113   "/bin/sh -c 'while t   47 minutes ago      Up 40 minutes                                    dreamy_hypatia     
5ef903292447        hyy044101331/mengka-new-images:20150113   "/bin/bash"            57 minutes ago      Up 49 minutes                                    evil_lumiere       
sh-3.2#

 

5.查看运行的容器的log

sh-3.2# docker logs 18cab084f4f6
2015-01-1613:01:38
2015-01-1613:01:40
2015-01-1613:01:41
2015-01-1613:01:42
2015-01-1613:01:43
2015-01-1613:01:44
2015-01-1613:01:45
2015-01-1613:01:46
2015-01-1613:01:47
2015-01-1613:01:48

   

6. 进入容器

 

docker attach fervent_morse

sh-3.2# docker attach fervent_morse
2015-01-1613:12:06
2015-01-1613:12:07
2015-01-1613:12:08
2015-01-1613:12:09
2015-01-1613:12:10
2015-01-1613:12:11

 

 docker exec -ti 18cab084f4f6 /bin/bash

sh-3.2# docker exec -ti 18cab084f4f6 /bin/bash
root@18cab084f4f6:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mengka.sh  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

 

7.  停止容器

sh-3.2# docker stop 18cab084f4f6
18cab084f4f6

 

 8. 重新启动

sh-3.2# docker restart 18cab084f4f6
18cab084f4f6
sh-3.2# docker ps -a
CONTAINER ID        IMAGE                                     COMMAND                CREATED             STATUS                        PORTS               NAMES
18cab084f4f6        mengka-image-time01:latest                "/bin/sh mengka.sh"    11 minutes ago      Up 4 seconds                                      fervent_morse      
9b56cabcf039        ubuntu:14.04                              "/bin/bash"            20 minutes ago      Exited (130) 15 minutes ago                       pensive_yonath     
9c2b0d270c80        ubuntu:14.04                              "/bin/bash"            20 minutes ago      Exited (130) 20 minutes ago                       naughty_torvalds   
24869d1d79d3        hyy044101331/mengka-new-images:20150113   "/bin/bash"            22 minutes ago      Exited (0) 22 minutes ago                         fervent_hoover     
1b8b2033ff11        hyy044101331/mengka-new-images:20150113   "/bin/sh -c 'while t   58 minutes ago      Up 52 minutes                                     dreamy_hypatia     
5ef903292447        hyy044101331/mengka-new-images:20150113   "/bin/bash"            About an hour ago   Up About an hour                                  evil_lumiere       

 

9. 查看运行的容器的log

 

sh-3.2# docker logs 18cab084f4f6
2015-01-1613:07:48
2015-01-1613:07:49
2015-01-1613:07:50
2015-01-1613:07:51
2015-01-1613:07:52
2015-01-1613:07:53
2015-01-1613:07:54
2015-01-1613:07:55
2015-01-1613:07:56
2015-01-1613:07:57

 

 

你可能感兴趣的:(docker)