最近听说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 [email protected]:/# 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 [email protected]:/#
下面解释一下上面的命令:docker run -i -t <image> /bin/bash 使用image创建container并进入交互模式, login shell是/bin/bash
4、查看container。退出ubuntu之后,查看
[email protected]:/# 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 [email protected]:/# [email protected]:/# 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 [email protected]:/#
3、保存镜像,commit命令
@~ $ docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE ubuntu latest 9bd07e480c5b 9 days ago 192.7 MB @~ $ docker commit -a "liqiu <[email protected]>" 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 [email protected]:/# sudo apt-install curl
2、退出之后,保存镜像
@~ $ docker commit -a "liqiu <[email protected]>" 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[email protected]:/# curl www.baidu.com <!DOCTYPE html> ...... ...... ......
完活!~
时间: 2024-10-12 21:19:09