docker使用笔记

最近听说docker很火,这两天学习了一下。本人实用的是mac os操作系统。

有关什么是docker、docker的好处以及安装,请参考:http://tech.uc.cn/?p=2726

一、基本命令

1、安装ubuntu虚拟机

docker pull ubuntu

2、查看镜像

@~ $ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubuntu              latest              9bd07e480c5b        9 days ago          192.7 MB

3、登录ubuntu

@~ $ docker run -a stdin -a stdout -a stderr -i -t ubuntu /bin/bash
root@ccaffb725895:/# uname -a
Linux ccaffb725895 3.16.4-tinycore64 #1 SMP Thu Oct 23 16:14:24 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
root@ccaffb725895:/# 

下面解释一下上面的命令:docker run -i -t <image> /bin/bash 使用image创建container并进入交互模式, login shell是/bin/bash

4、查看container。退出ubuntu之后,查看

root@ccaffb725895:/# exit
exit
@~ $ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
ccaffb725895        ubuntu:latest       "/bin/bash"         3 minutes ago       Exited (0) 15 seconds ago                       trusting_bohr       

二、保存container

如果我以后登录ubuntu都输入上面这些命令太繁琐了,需要锁定container

1、启动container

@~ $ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
ccaffb725895        ubuntu:latest       "/bin/bash"         3 minutes ago       Exited (0) 15 seconds ago                       trusting_bohr       
@~ $ docker start ccaffb725895
ccaffb725895

2、attach

@~ $ docker start ccaffb725895
ccaffb725895
@~ $ docker attach ccaffb725895
root@ccaffb725895:/# 
root@ccaffb725895:/# uname -a
Linux ccaffb725895 3.16.4-tinycore64 #1 SMP Thu Oct 23 16:14:24 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
root@ccaffb725895:/# 

3、保存镜像,commit命令

@~ $ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubuntu              latest              9bd07e480c5b        9 days ago          192.7 MB
@~ $ docker commit -a "liqiu <qiu.li@qunar.com>" ccaffb725895 ubuntu:ssh-ubuntu
5d2941e8c0bf7fe988604f11d043a03331ec83536ce09f841e760d51156b6865
@~ $ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              VIRTUAL SIZE
ubuntu              ssh-ubuntu          5d2941e8c0bf        About a minute ago   192.7 MB
ubuntu              latest              9bd07e480c5b        9 days ago           192.7 MB

三、删除container

@~ $ docker stop ccaffb725895
ccaffb725895
@~ $ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                       PORTS               NAMES
ccaffb725895        ubuntu:latest       "/bin/bash"         16 minutes ago      Exited (127) 5 minutes ago                       trusting_bohr       
@~ $ docker rm ccaffb725895
ccaffb725895
@~ $ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
@

四、恢复

一切恢复平静之后,思考一个问题,我在虚拟机安装了一个curl,过几天我再启动它curl还存在吗?

1、进入ubuntu,安装curl

@~ $ docker run -a stdin -a stdout -a stderr -i -t ubuntu /bin/bash
root@0efb23b52565:/# sudo apt-install curl

2、退出之后,保存镜像

@~ $ docker commit -a "liqiu <qiu.li@qunar.com>" 0efb23b52565 ubuntu:curl-ubuntu
aac468ce18c883bc71e849028a1718e57d80d3c6231840e6b1fdb4d66354bfe6
@~ $ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              VIRTUAL SIZE
ubuntu              curl-ubuntu         aac468ce18c8        About a minute ago   204.1 MB
ubuntu              latest              9bd07e480c5b        9 days ago           192.7 MB

3、重新进入镜像

@~ $ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              VIRTUAL SIZE
ubuntu              curl-ubuntu         aac468ce18c8        About a minute ago   204.1 MB
ubuntu              latest              9bd07e480c5b        9 days ago           192.7 MB
@~ $ docker run -a stdin -a stdout -a stderr -i -t aac468ce18c8 /bin/bash
root@00ddbbab1ccf:/# curl www.baidu.com
<!DOCTYPE html>
......
......
......

五、其他命令介绍:

ctrl-p ctrl-q 退出虚拟机,如果重新进入,可以使用docker attach *** 

root@2c1282c522bf:/# (此时ctrl+p 然后 ctrl+q)
@~/git/github/docker-hive-on-tez-master $ docker attach 2c1282c522bf root@2c1282c522bf:/# (重新进入了)

 

posted @ 2014-12-14 16:56  李秋  阅读(3484)  评论(0编辑  收藏  举报