首页 > 代码库 > docker使用笔记

docker使用笔记

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

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

一、基本命令

1、安装ubuntu虚拟机

docker pull ubuntu

2、查看镜像

@~ $ docker imagesREPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZEubuntu              latest              9bd07e480c5b        9 days ago          192.7 MB

3、登录ubuntu

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

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

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

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

二、保存container

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

1、启动container

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

2、attach

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

3、保存镜像,commit命令

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

三、删除container

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

四、恢复

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

1、进入ubuntu,安装curl

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

2、退出之后,保存镜像

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

3、重新进入镜像

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

完活!~

docker使用笔记