1.启动一个Docker容器
[root@localhost~]#dockerrun-i-tUbuntu/bin/bash
Unabletofindimage‘ubuntu’locally
Pullingrepositoryubuntu
04c5d3b7b065:Downloadcomplete
511136ea3c5a:Downloadcomplete
c7b7c6419568:Downloadcomplete
70c8faa62a44:Downloadcomplete
d735006ad9c1:Downloadcomplete
Status:Downloadednewerimageforubuntu:latest
注:
-i:打开容器中的STDIN
-t:为容器分配一个伪tty终端
从上面我们可以看出,首先Docker会检查本地是否存在ubuntu镜像,如果在本地没有找到该镜像的话,那么Docker就会去官方的DockerHubRegistry查看DockerHub中是否有该镜像。Docker一旦找到该镜像,就会下载该镜像并将其保存到本地的宿主机中。
然后,Docker在文件系统内部用这个镜像创建了一个新的容器。该容器拥有自己的网络、IP地址,以及一个用来可以和宿主机进行通信的桥接网络接口。最后,我们告诉Docker在新容器中要运行什么命令。
当容器创建完毕之后,Docker就会执行容器中的/bin/bash命令。这时间我们就可以看到容器内的shell
root@8c342c0c275c:/#
注:8c342c0c275c代表容器的ID
2.使用容器
查看该容器的主机名
root@8c342c0c275c:/#hostname
8c342c0c275c
可以看到,容器的主机名就是该容器的ID
查看该主机的hosts文件
root@8c342c0c275c:/#cat/etc/hosts
172.17.0.28c342c0c275c
ff00::0ip6-mcastprefix
ff02::1ip6-allnodes
ff02::2ip6-allrouters
127.0.0.1localhost
::1localhostip6-localhostip6-loopback
fe00::0ip6-localnet
可以看到Docker为该容器的IP地址添加了一条主机配置项。
查看该容器的IP地址
root@8c342c0c275c:/#ipa
1:lo:《LOOPBACK,UP,LOWER_UP》mtu65536qdiscnoqueuestateUNKNOWNgroupdefault
link/loopback00:00:00:00:00:00brd00:00:00:00:00:00
inet127.0.0.1/8scopehostlo
valid_lftforeverpreferred_lftforever
inet6::1/128scopehost
valid_lftforeverpreferred_lftforever
4:eth0:《BROADCAST,UP,LOWER_UP》mtu1500qdiscpfifo_faststateUPgroupdefaultqlen1000
link/ether02:42:ac:11:00:02brdff:ff:ff:ff:ff:ff
inet172.17.0.2/16scopeglobaleth0
valid_lftforeverpreferred_lftforever
inet6fe80::42:acff:fe11:2/64scopelink
valid_lftforeverpreferred_lftforever
查看容器中运行的进程
root@8c342c0c275c:/#ps-aux
USERPID%CPU%MEMVSZRSSTTYSTATSTARTTIMECOMMAND
root10.00.1181681976?Ss09:380:00/bin/bash
root190.00.0155681144?R+09:550:00ps-aux
在容器中安装一个软件包
root@8c342c0c275c:/#apt-getupdate&&apt-getinstallvim
Ignhttp://archive.ubuntu.comtrustyInRelease
Ignhttp://archive.ubuntu.comtrusty-updatesInRelease
Ignhttp://archive.ubuntu.comtrusty-securityInRelease
Hithttp://archive.ubuntu.comtrustyRelease.gpg
Get:1http://archive.ubuntu.comtrusty-updatesRelease.gpg[933B]
Get:2http://archive.ubuntu.comtrusty-securityRelease.gpg[933B]
Hithttp://archive.ubuntu.comtrustyRelease
Get:3http://archive.ubuntu.comtrusty-updatesRelease[62.0kB]
Get:4http://archive.ubuntu.comtrusty-securityRelease[62.0kB]
Get:5http://archive.ubuntu.comtrusty/mainSources[1335kB]
Get:6http://archive.ubuntu.comtrusty/restrictedSources[5335B]
Get:7http://archive.ubuntu.comtrusty/universeSources[7926kB]
Get:8http://archive.ubuntu.comtrusty/mainamd64Packages[1743kB]
Get:9http://archive.ubuntu.comtrusty/restrictedamd64Packages[16.0kB]
Get:10http://archive.ubuntu.comtrusty/universeamd64Packages[7589kB]
Get:11http://archive.ubuntu.comtrusty-updates/mainSources[193kB]
Get:12http://archive.ubuntu.comtrusty-updates/restrictedSources[1874B]
Get:13http://archive.ubuntu.comtrusty-updates/universeSources[119kB]
Get:14http://archive.ubuntu.comtrusty-updates/mainamd64Packages[493kB]
Get:15http://archive.ubuntu.comtrusty-updates/restrictedamd64Packages[14.8kB]
Get:16http://archive.ubuntu.comtrusty-updates/universeamd64Packages[298kB]
Get:17http://archive.ubuntu.comtrusty-security/mainSources[70.1kB]
Get:18http://archive.ubuntu.comtrusty-security/restrictedSources[1874B]
Get:19http://archive.ubuntu.comtrusty-security/universeSources[19.1kB]
Get:20http://archive.ubuntu.comtrusty-security/mainamd64Packages[229kB]
Get:21http://archive.ubuntu.comtrusty-security/restrictedamd64Packages[14.8kB]
Get:22http://archive.ubuntu.comtrusty-security/universeamd64Packages[98.1kB]
Fetched20.3MBin41s(490kB/s)
Readingpackagelists.。。Done
Readingpackagelists.。。Done
Buildingdependencytree
Readingstateinformation.。。Done
Thefollowingextrapackageswillbeinstalled:
libgpm2libpython2.7libpython2.7-minimallibpython2.7-stdlibvim-runtime
Suggestedpackages:
gpmctagsvim-docvim-scripts
ThefollowingNEWpackageswillbeinstalled:
libgpm2libpython2.7libpython2.7-minimallibpython2.7-stdlibvim
vim-runtime
0upgraded,6newlyinstalled,0toremoveand2notupgraded.
Needtoget9083kBofarchives.
Afterthisoperation,42.9MBofadditionaldiskspacewillbeused.
Doyouwanttocontinue?[Y/n]y
Get:1http://archive.ubuntu.com/ubuntu/trusty/mainlibgpm2amd641.20.4-6.1[16.5kB]
Get:2http://archive.ubuntu.com/ubuntu/trusty/mainlibpython2.7-minimalamd642.7.6-8[307kB]
Get:3http://archive.ubuntu.com/ubuntu/trusty/mainlibpython2.7-stdlibamd642.7.6-8[1872kB]
Get:4http://archive.ubuntu.com/ubuntu/trusty/mainlibpython2.7amd642.7.6-8[1044kB]
Get:5http://archive.ubuntu.com/ubuntu/trusty/mainvim-runtimeall2:7.4.052-1ubuntu3[4888kB]
Get:6http://archive.ubuntu.com/ubuntu/trusty/mainvimamd642:7.4.052-1ubuntu3[956kB]
Fetched9083kBin51s(175kB/s)
Selectingpreviouslyunselectedpackagelibgpm2:amd64.
(Readingdatabase。。。11527filesanddirectoriescurrentlyinstalled.)
Preparingtounpack。。。/libgpm2_1.20.4-6.1_amd64.deb。。。
Unpackinglibgpm2:amd64(1.20.4-6.1)。。。
Selectingpreviouslyunselectedpackagelibpython2.7-minimal:amd64.
Preparingtounpack。。。/libpython2.7-minimal_2.7.6-8_amd64.deb。。。
Unpackinglibpython2.7-minimal:amd64(2.7.6-8)。。。
Selectingpreviouslyunselectedpackagelibpython2.7-stdlib:amd64.
Preparingtounpack。。。/libpython2.7-stdlib_2.7.6-8_amd64.deb。。。
Unpackinglibpython2.7-stdlib:amd64(2.7.6-8)。。。
Selectingpreviouslyunselectedpackagelibpython2.7:amd64.
Preparingtounpack。。。/libpython2.7_2.7.6-8_amd64.deb。。。
Unpackinglibpython2.7:amd64(2.7.6-8)。。。
Selectingpreviouslyunselectedpackagevim-runtime.
Preparingtounpack。。。/vim-runtime_2%3a7.4.052-1ubuntu3_all.deb。。。
Adding‘diversionof/usr/share/vim/vim74/doc/help.txtto/usr/share/vim/vim74/doc/help.txt.vim-tinybyvim-runtime’
Adding‘diversionof/usr/share/vim/vim74/doc/tagsto/usr/share/vim/vim74/doc/tags.vim-tinybyvim-runtime’
Unpackingvim-runtime(2:7.4.052-1ubuntu3)。。。
Selectingpreviouslyunselectedpackagevim.
Preparingtounpack。。。/vim_2%3a7.4.052-1ubuntu3_amd64.deb。。。
Unpackingvim(2:7.4.052-1ubuntu3)。。。
Settinguplibgpm2:amd64(1.20.4-6.1)。。。
Settinguplibpython2.7-minimal:amd64(2.7.6-8)。。。
Settinguplibpython2.7-stdlib:amd64(2.7.6-8)。。。
Settinguplibpython2.7:amd64(2.7.6-8)。。。
Settingupvim-runtime(2:7.4.052-1ubuntu3)。。。
Processing/usr/share/vim/addons/doc
Settingupvim(2:7.4.052-1ubuntu3)。。。
update- alternatives:using/usr/bin/vim.basictoprovide/usr/bin/vim(vim)inautomode
update- alternatives:using/usr/bin/vim.basictoprovide/usr/bin/vimdiff(vimdiff)inautomode
update- alternatives:using/usr/bin/vim.basictoprovide/usr/bin/rvim(rvim)inautomode
update- alternatives:using/usr/bin/vim.basictoprovide/usr/bin/rview(rview)inautomode
update- alternatives:using/usr/bin/vim.basictoprovide/usr/bin/vi(vi)inautomode
update- alternatives:using/usr/bin/vim.basictoprovide/usr/bin/view(view)inautomode
update- alternatives:using/usr/bin/vim.basictoprovide/usr/bin/ex(ex)inautomode
update- alternatives:using/usr/bin/vim.basictoprovide/usr/bin/editor(editor)inautomode
Processingtriggersforlibc-bin(2.19-0ubuntu6.4)。。。
退出容器
root@8c342c0c275c:/#exit
exit
当我们输入exit后,容器就停止工作了。只有在指定的/bin/bash命令处于运行状态的时间,容器才会相应地处于运行状态。一旦退出容器,/bin/bash命令也就结束了,这时容器也就停止了。
查看系统中容器的列表
[root@localhost~]#dockerps-a
CONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES
8c342c0c275cubuntu:latest/bin/bash26minutesagoExited(0)5minutesagosharp_bohr
8c342c0c275c:代表容器的ID,
ubuntu:latest:创建容器的镜像
/bin/bash :容器最后执行的命令
26minutesago:创建时间
Exited(0)5minutes :容器退出的状态
sharp_bohr :容器的名称
Docker中容器的命名
Docker在创建容器时会自动为容器生成一个随机的名称。那么如果我们想在创建一个容器时指定该容器的名称可以使用如下命令:
[root@localhost~]#dockerrun--nameovcer_the_container-i-tubuntu/bin/bash
root@1ce9f640478d:/#
上面的命令将会创建一个名为ovcer_the_container的容器。对于一个合法的容器的名称来说只可以包括以下字符:小写字母a~z、大写字母A-Z、数字0~9、下划线、圆点、横线。
上面就是Linux使用Docker容器的方法介绍了,在使用命令创建Docker容器后,就可以使用命令对Docker容器进行命名了。