安装好docker后,怎么玩转docker呢,千里之行,始于足下,首先运行我们的第一个容器,我们使用docker run命令创建容器。要创建一个容器我们必须有一个镜像,现在我们从Docker注册服务器的软件仓库(我这里肯定就是DaoCloud了)下载一个Docker的镜像到本地(注意以下所有命令都是以root用户身份执行的)
# docker search ubuntu #搜索镜像
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating s... 5598 [OK]
rastasheep/ubuntu-sshd Dockerized SSH service, built on top of of... 73 [OK]
ubuntu-upstart Upstart is an event-based replacement for ... 70 [OK]
consol/ubuntu-xfce-vnc Ubuntu container with "headless" VNC sessi... 43 [OK]
ubuntu-debootstrap debootstrap --variant=minbase --components... 27 [OK]
torusware/speedus-ubuntu Always updated official Ubuntu docker imag... 27 [OK]
nuagebec/ubuntu Simple always updated Ubuntu docker images... 15 [OK]
nickistre/ubuntu-lamp LAMP server on Ubuntu 15 [OK]
nimmis/ubuntu This is a docker images different LTS vers... 6 [OK]
maxexcloo/ubuntu Base image built on Ubuntu with init, Supe... 2 [OK]
darksheer/ubuntu Base Ubuntu Image -- Updated hourly 2 [OK]
jordi/ubuntu Ubuntu Base Image 1 [OK]
admiringworm/ubuntu Base ubuntu images based on the official u... 1 [OK]
teamrock/ubuntu TeamRock's Ubuntu image configured with AW... 0 [OK]
labengine/ubuntu Images base ubuntu 0 [OK]
webhippie/ubuntu Docker images for ubuntu 0 [OK]
datenbetrieb/ubuntu custom flavor of the official ubuntu base ... 0 [OK]
forumi0721ubuntux64/ubuntu-x64-dev ubuntu-x64-dev 0 [OK]
vcatechnology/ubuntu A Ubuntu image that is updated daily 0 [OK]
lynxtp/ubuntu https://github.com/lynxtp/docker-ubuntu 0 [OK]
forumi0721ubuntuaarch64/ubuntu-aarch64-dev ubuntu-aarch64-dev 0 [OK]
forumi0721ubuntuarmhf/ubuntu-armhf-dev ubuntu-armhf-dev 0 [OK]
konstruktoid/ubuntu Ubuntu base image 0 [OK]
forumi0721ubuntux64/ubuntu-x64-dev-armbian ubuntu-x64-dev-armbian 0 [OK]
smartentry/ubuntu ubuntu with smartentry 0 [OK]
# docker pull ubuntu #下载镜像
13.04: Pulling from library/ubuntu
a3ed95caeb02: Pull complete
727520c5e30b: Pull complete
2e8f7add78f9: Pull complete
4600be257a84: Pull complete
89d0f0874176: Pull complete
Digest: sha256:dfbeee3e3faf84f9c9fa599dadb7eaa39ae21a9342fa1b757ee7f609d77281f1
Status: Downloaded newer image for daocloud.io/library/ubuntu:13.04
# docker images #这个命令用来查看本地镜像库中的镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
daocloud.io/library/ubuntu latest f49eec89601e 5 weeks ago 129 MB
# docker run -t -i daocloud.io/library/ubuntu:latest /bin/bash
root@ac11e88a2e26:/# hostname
ac11e88a2e26
上述docker run命令中使用了 -t -i选项,-t 选项表示为要创建的容器分配一个伪tty终端。-i 选项表示保证容器中stdin是开启的,docker使用我们本地镜像库中daocloud.io/library/ubuntu:latest这个镜像创建了一个容器,该容器拥有自己的网络、IP地址和一个用来和宿主机进行通信的桥接网络接口,最后我们告诉在容器中要运行/bin/bash,这样创建容器后我们就可以看到容器的shell root@ac11e88a2e26:/# 。
我们可以使用docker ps命令(注意这里不要在root@ac11e88a2e26:/#中执行docker ps ,而应该再开一个ssh会话,在我们的虚机中执行)查看当前系统中处于运行状态的容器。
# docker ps -l #显示上次创建的容器,不加-l选项则会显示所有运行着的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ac11e88a2e26 daocloud.io/library/ubuntu:latest "/bin/bash" 48 minutes ago Up 48 minutes pensive_ptolemy
# docker ps --no-trunc # --no-trunc表示不折叠输出,这样就会输出完整的容器id
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ac11e88a2e26fb29cf90930e8f1006159dcc425f7a451d23dee7be9b878b2176 daocloud.io/library/ubuntu:latest "/bin/bash" 48 minutes ago Up 48 minutes pensive_ptolemy
可以看出容器的主机名就是该容器的ID的前面一部分。
我们可以研究下容器的网络配置:
root@ac11e88a2e26:/# cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 ac11e88a2e26
root@ac11e88a2e26:/# ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:ac:11:00:02
inet addr:172.17.0.2 Bcast:0.0.0.0 Mask:255.255.0.0
inet6 addr: fe80::42:acff:fe11:2/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:4611 errors:0 dropped:0 overruns:0 frame:0
TX packets:3951 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:31374248 (31.3 MB) TX bytes:273116 (273.1 KB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
可以看出docker已在hosts文件中为该容器的ip地址添加了一条主机配置项。,容器的网络接口有两个,一个是lo环回接口,还有一个Ip为172.17.0.2的标准eth0网络接口,与普通的Linux主机完全一样。下面我们在这个容器中安装一个软件。
root@ac11e88a2e26:/# vi
bash: vi: command not found
root@ac11e88a2e26:/# vim
bash: vim: command not found
root@ac11e88a2e26:/# apt-get install vim
跟在我们主机上没什么不同。如果这时我们输出exit命令,我们就会返回到ubuntu宿主机的命令行。
root@ac11e88a2e26:/# exit
exit
$ hostname
ubuntu
$ docker ps -a #显示所有的容器包括运行的和停止的
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ac11e88a2e26 daocloud.io/library/ubuntu:latest "/bin/bash" About an hour ago Exited(0)43 seconds ago pensive_ptolemy
可以看到我们的容器仍然存在,但是停止了运行。
在很多命令中我们可以用容器的名字替代CONTAINER ID ,如果我们不指定容器名系统会自己生成一个名字,我们可以指定一个更好记的容器名。要指定容器的别名,只需在docker run 命令中使用–name。
$ docker run --name ubuntu -t -i daocloud.io/library/ubuntu:latest /bin/bash
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2c9b98aa4d76 daocloud.io/library/ubuntu:latest "/bin/bash" 21 seconds ago Up 18 seconds ubuntu
ac11e88a2e26 daocloud.io/library/ubuntu:latest "/bin/bash" About an hour ago Exited (0) 6 minutes ago pensive_ptolemy
注意:
1、容器名必须唯一,不能创建两个名字相同的容器。
2、基本所有使用CONTAINER ID的docker命令中都可以用name代替,这个就是我们改名的原因。
使用docker start/restart命令可以启动已经停止的容器。
还可以使用docker rename old_name new_name修改容器的名字。
# docker rename pensive_ptolemy ubuntu1
#docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2c9b98aa4d76 daocloud.io/library/ubuntu:latest "/bin/bash" 21 seconds ago Up 18 seconds ubuntu
ac11e88a2e26 daocloud.io/library/ubuntu:latest "/bin/bash" About an hour ago Exited (0) 6 minutes ago ubuntu1
docker start 命令可以启动一个停止的容器。
# docker start ubuntu1
ubuntu1
root@ubuntu:/home/jie# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2c9b98aa4d76 daocloud.io/library/ubuntu:latest "/bin/bash" 7 minutes ago Up 7 minutes ubuntu
ac11e88a2e26 daocloud.io/library/ubuntu:latest "/bin/bash" About an hour ago Up 5 seconds ubuntu1
这篇blog主要涉及了一些基本的docker命令,成功的“玩”起来了第一个容器。