使用Docker容器管理Ubuntu系统
时间:2014-11-26 来源:服务器之家 投稿:root
本文将介绍如何安装Docker,并用它高效地管理虚拟机。Docker是一种开源Linux虚拟化平台,以便广大开发人员和系统管理员轻松地开发、部署和执行分布式应用程序。Docker包括这几个部分:Docker引擎(一个轻型运行时和虚拟化层),封装和版本控制虚拟机的工具(类似源代码软件库),以及Docker Hub(一项共享应用程序、实现工作流自动化的云端服务)。Docker让应用程序得以由组件迅速装配而成,消除了开发环境、质量控制和生产环境之间的磨擦。
1 首项附注
以本文为例,我准备把docker安装在Ubuntu 14.04操作系统上。虚拟化软件市场上有几项标准硬件虚拟化技术,比如KVM、Xen或Hyper-V。可是标准虚拟化技术太过笨拙,无法对Linux上的单个应用程序进行虚拟化处理。我们可以克服这种情形,只要使用Linux容器(Linux Container):对操作系统层面的虚拟化而言,这是一种不错的替代方案。Linux容器是非常有用的方式,可以让开发/测试环境出现在安全有保障的一堆容器中。Docker提供了这种用途的Linux容器环境。
2 安装
如前所述,我准备把docker安装在Ubuntu上。在这一章节,我将为大家介绍安装docker的两种方法;在2a这部分中,我使用来自Ubuntu软件库的docker版本,这个版本由Ubuntu维护,提供整整5年的长期支持版(LTS)支持,但它不是最新版本。在2b这部分中,我将使用来自Ubuntu ppa软件库的最新版本。由于docker正在大力开发之中,来自2b)的最新Docker版本极可能适合大多数用户。请使用方法2a或方法2b,但别同时使用这两种方法!
2a 从官方的Ubuntu软件库来安装
想安装docker,请使用下列命令:
sudo apt-get update
sudo apt-get install docker.io
然后创建符号链接,以便在外壳上使用起来更容易。
sudo ln -sf /usr/bin/docker.io /usr/local/bin/docker
sudo sed -i '$acomplete -F _docker docker' /etc/bash_completion.d/docker.io
2b安装最新的docker版本
想从docker.io软件库安装最新的docker版本,运行这个命令:
curl -s https://get.docker.io/ubuntu/ | sudo sh
3 准备外壳环境
现在,我将把我的user=srijan添加到docker组:
sudo usermod -a -G docker srijan
或者使用:
sudo usermod -a -G docker $USER
这里,我会退出当前会话,然后再重新登录。现在,我将添加docker配置文件,以便向系统通知其位置。
sudo vi /etc/default/docker.io
DOCKER="/usr/bin/docker.io"
现在重启服务,如下所示:
sudo service docker.io restart
4 容器管理
我准备随Ubuntu操作系统一同启动容器。我会下载docker映像,如下所示:
docker pull ubuntu
注意:pull命令用于从注册中心(registry)拉取映像或软件库。
现在我将使用下面这个命令,登录进入到Ubuntu容器的bash外壳:
docker run -i -t ubuntu /bin/bash
仅仅为了确认,我将检查容器的IP,如下所示:
root@fd98ee950252:/# ifconfig
eth0 Link encap:Ethernet HWaddr 5a:a6:c6:88:f2:48
inet addr:172.17.0.3 Bcast:0.0.0.0 Mask:255.255.0.0
inet6 addr: fe80::58a6:c6ff:fe88:f248/64 Scope:Link
UP BROADCAST RUNNING MTU:1500 Metric:1
RX packets:7 errors:0 dropped:2 overruns:0 frame:0
TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:558 (558.0 B) TX bytes:648 (648.0 B)
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:1500 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:0
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
root@fd98ee950252:/#
我容器上的IP地址为172.17.0.3。同样,我还可以使用其他窗口。想编辑容器,只要键入:
exit
同样,你可以有其他的操作系统容器,比如说。
我想使用Debian容器,就要使用代码:
docker run -i -t debian /bin/bash
如果你想有某个发行版,那么就要使用这个命令:
docker run -i -t ubuntu:12.04 /bin/bash
它会创建ubuntu12.04容器。我会反复核实,如下所示:
root@44b56100fd1f:/# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=12.04
DISTRIB_CODENAME=precise
DISTRIB_DESCRIPTION="Ubuntu 12.04.4 LTS"
root@44b56100fd1f:/#
4.1 构建我们自己的映像
构建Docker映像有两种方法:
通过docker commit(提交)命令
通过docker build(构建)命令以及Docker文件(Dockerfile)
目前并不推荐docker提交方法,因为借助Docker文件进行构建要灵活得多、强大得多,但为了力求完整起见,我们会向你演示提交方法。之后,我将重点介绍推荐的Docker映像构建方法:编写Docker文件,然后使用docker构建命令。
4.1 使用Docker提交命令来创建映像
我将创建一个容器,并对该容器进行更改DD就像更改代码那样,然后将那些变更内容提交给新的映像。
不妨先通过我们在过去使用的ubuntu映像来创建一个容器。
docker run -i -t ubuntu /bin/bash
root@73527b8b4261:/#
注意:请注意上面的root显示了主机名称73527b8b4261,它是所创建的容器名称;它与你的情况会不一样。
此外,我会将apache安装在容器里面:
apt-get install apache2
我已启动了容器,然后将Apache安装在里面。现在,我准备将该容器用作Web服务器,所以我将它保存在当前状态。
这样一来,我每次创建一个新的容器时,就没必要用Apache来重新构建它。为此,我将退出容器,使用exit命令,然后使用docker提交命令。
exit
docker commit 73527b8b4261 srijan/apache2 8ce0ea7a1528
注意:这里的73527b8b4261是我的容器名称;我为容器使用8ce0ea7a1528标记,你可以赋予任何标记名称,也可以使用同一个名称。
假设你忘了上一个创建的容器的编号,可以使用这个命令:
docker ps -l -q
它会得出73527b8b4261
注意:73527b8b4261是你上一个创建的容器名称,它与你的情况可能不一样。
不妨看一下我们的新映像。它可以这样获得,如下所示:
srijan@vboxtest:~$ docker images srijan/apache2
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
srijan/apache2 8ce0ea7a1528 741155868ac8 6 minutes ago 207.2 MB
srijan@vboxtest:~$
注意:这里的8ce0ea7a1528是我在保存容器时所使用的标记名称;
注意:741155868ac8是该容器的映像编号;
注意:所有这些值与你的情况可能不一样,因为在保存容器时,docker会创建相应的随机名称。
现在,我将保存定制的映像,如下所示:
docker commit -m="A new custom image" --author="Srijan Kishore" 73527b8b4261 srijan/apache2:webserver
它会给出结果:
srijan@vboxtest:~$ docker commit -m="A new custom image" --author="Srijan Kishore" 73527b8b4261 srijan/apache2:webserver
f0367362eb405c513ac002b5cf172a2c0bc6c8212eab91c613f9ee611cf92fec
想从我们的新映像运行容器,我们只要使用docker run(运行)命令。
docker run -t -i srijan/apache2:webserver /bin/bash
我们可以反复核对已提交的映像,如下所示:
srijan@vboxtest:~$ docker inspect srijan/apache2:webserver
[{
"Architecture": "amd64",
"Author": "Srijan Kishore",
"Comment": "A new custom image",
"Config": {
"AttachStderr": false,
"AttachStdin": false,
"AttachStdout": false,
"Cmd": [
"/bin/bash"
],
"CpuShares": 0,
"Cpuset": "",
"Domainname": "",
"Entrypoint": null,
"Env": [
"HOME=/",
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"ExposedPorts": null,
"Hostname": "",
"Image": "",
"Memory": 0,
"MemorySwap": 0,
"NetworkDisabled": false,
"OnBuild": null,
"OpenStdin": false,
"PortSpecs": null,
"StdinOnce": false,
"Tty": false,
"User": "",
"Volumes": null,
"WorkingDir": ""
},
"Container": "73527b8b42614f6ecd83fb5f9822d6086988d3b68fd5e32b4afbc7cd415402fd",
"ContainerConfig": {
"AttachStderr": true,
"AttachStdin": true,
"AttachStdout": true,
"Cmd": [
"/bin/bash"
],
"CpuShares": 0,
"Cpuset": "",
"Domainname": "",
"Entrypoint": null,
"Env": [
"HOME=/",
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"ExposedPorts": null,
"Hostname": "73527b8b4261",
"Image": "ubuntu",
"Memory": 0,
"MemorySwap": 0,
"NetworkDisabled": false,
"OnBuild": null,
"OpenStdin": true,
"PortSpecs": null,
"StdinOnce": true,
"Tty": true,
"User": "",
"Volumes": null,
"WorkingDir": ""
},
"Created": "2014-06-30T12:58:04.973349049Z",
"DockerVersion": "1.0.1",
"Id": "f0367362eb405c513ac002b5cf172a2c0bc6c8212eab91c613f9ee611cf92fec",
"Os": "linux",
"Parent": "ef83896b7fb99b00b9e0e6ac943826386e7edcef11a3a2f58b42011ab4a4e683",
"Size": 14463026
}
]srijan@vboxtest:~$
想从我们的新映像运行容器,我们可以使用docker运行命令。
docker run -t -i srijan/apache2:webserver /bin/bash
4.2 用Docker文件构建映像
Docker文件使用基本的特定领域语言(DSL),以及用于构建Docker映像的指令。然后,我们使用docker构建命令,通过Docker文件里面的指令来构建新的映像。Docker公司的开发团队还在此发布了Docker文件教程(http://www.docker.io/learn/dockerfile/),可以帮助大家学会如何构建Docker文件。
现在,我将创建一个简单的docker文件,它可以按照用户的需求,进一步改进和完善。首先,让一个目录假设为dir=kishore:
mkdir kishore
cd kishore
现在创建一个Docker文件,如下所示:
vi Dockerfile
并输入内容,如下所示:
FROM ubuntu:12.04
MAINTAINER Srijan Kishore mailto:s.kishore@ispconfig.org
RUN apt-get -qq update
RUN apt-get -qqy install apache2
现在打开定制的容器,请使用这个命令:
sudo docker build -t="srijan/custom1" .
它会得出如下结果:
srijan@vboxtest:~/kishore$ sudo docker build -t="srijan/custom1" .
[sudo] password for srijan:
Sending build context to Docker daemon 2.56 kB
Sending build context to Docker daemon
Step 0 : FROM ubuntu:12.04
Pulling repository ubuntu
ea7d6801c538: Download complete
511136ea3c5a: Download complete
65b7e9ccb809: Download complete
f8dd6bd14f58: Download complete
a343823119db: Download complete
---> ea7d6801c538
Step 1 : MAINTAINER Srijan Kishore mailto:s.kishore@ispconfig.org
---> Running in a6135f405eb4
---> be97c22efa82
Removing intermediate container a6135f405eb4
Step 2 : RUN apt-get -qq update
---> Running in b5681cd85ba8
---> cbc3a95de894
Removing intermediate container b5681cd85ba8
Step 3 : RUN apt-get -qqy install apache2
---> Running in 5765c09b530f
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package libsqlite3-0.
(Reading database ... 7551 files and directories currently installed.)
Unpacking libsqlite3-0 (from .../libsqlite3-0_3.7.9-2ubuntu1.1_amd64.deb) ...
Selecting previously unselected package libroken18-heimdal.
Unpacking libroken18-heimdal (from .../libroken18-heimdal_1.6~git20120311.dfsg.1-2ubuntu0.1_amd64.deb) ...
Selecting previously unselected package libasn1-8-heimdal.
Unpacking libasn1-8-heimdal (from .../libasn1-8-heimdal_1.6~git20120311.dfsg.1-2ubuntu0.1_amd64.deb) ...
Selecting previously unselected package libgpg-error0.
Unpacking libgpg-error0 (from .../libgpg-error0_1.10-2ubuntu1_amd64.deb) ...
Selecting previously unselected package libgcrypt11.
Unpacking libgcrypt11 (from .../libgcrypt11_1.5.0-3ubuntu0.2_amd64.deb) ...
Selecting previously unselected package libgdbm3.
Unpacking libgdbm3 (from .../libgdbm3_1.8.3-10_amd64.deb) ...
Selecting previously unselected package libp11-kit0.
Unpacking libp11-kit0 (from .../libp11-kit0_0.12-2ubuntu1_amd64.deb) ...
Selecting previously unselected package libtasn1-3.
Unpacking libtasn1-3 (from .../libtasn1-3_2.10-1ubuntu1.1_amd64.deb) ...
Selecting previously unselected package libgnutls26.
Unpacking libgnutls26 (from .../libgnutls26_2.12.14-5ubuntu3.8_amd64.deb) ...
Selecting previously unselected package libhcrypto4-heimdal.
Unpacking libhcrypto4-heimdal (from .../libhcrypto4-heimdal_1.6~git20120311.dfsg.1-2ubuntu0.1_amd64.deb) ...
Selecting previously unselected package libheimbase1-heimdal.
Unpacking libheimbase1-heimdal (from .../libheimbase1-heimdal_1.6~git20120311.dfsg.1-2ubuntu0.1_amd64.deb) ...
Selecting previously unselected package libwind0-heimdal.
Unpacking libwind0-heimdal (from .../libwind0-heimdal_1.6~git20120311.dfsg.1-2ubuntu0.1_amd64.deb) ...
Selecting previously unselected package libhx509-5-heimdal.
Unpacking libhx509-5-heimdal (from .../libhx509-5-heimdal_1.6~git20120311.dfsg.1-2ubuntu0.1_amd64.deb) ...
Selecting previously unselected package libkrb5-26-heimdal.
Unpacking libkrb5-26-heimdal (from .../libkrb5-26-heimdal_1.6~git20120311.dfsg.1-2ubuntu0.1_amd64.deb) ...
Selecting previously unselected package libheimntlm0-heimdal.
Unpacking libheimntlm0-heimdal (from .../libheimntlm0-heimdal_1.6~git20120311.dfsg.1-2ubuntu0.1_amd64.deb) ...
Selecting previously unselected package libgssapi3-heimdal.
Unpacking libgssapi3-heimdal (from .../libgssapi3-heimdal_1.6~git20120311.dfsg.1-2ubuntu0.1_amd64.deb) ...
Selecting previously unselected package libsasl2-2.
Unpacking libsasl2-2 (from .../libsasl2-2_2.1.25.dfsg1-3ubuntu0.1_amd64.deb) ...
Selecting previously unselected package libldap-2.4-2.
Unpacking libldap-2.4-2 (from .../libldap-2.4-2_2.4.28-1.1ubuntu4.4_amd64.deb) ...
Selecting previously unselected package libcap2.
Unpacking libcap2 (from .../libcap2_1%3a2.22-1ubuntu3_amd64.deb) ...
Selecting previously unselected package libexpat1.
Unpacking libexpat1 (from .../libexpat1_2.0.1-7.2ubuntu1.1_amd64.deb) ...
Selecting previously unselected package libmagic1.
Unpacking libmagic1 (from .../libmagic1_5.09-2ubuntu0.3_amd64.deb) ...
Selecting previously unselected package file.
Unpacking file (from .../file_5.09-2ubuntu0.3_amd64.deb) ...
Selecting previously unselected package mime-support.
Unpacking mime-support (from .../mime-support_3.51-1ubuntu1_all.deb) ...
Selecting previously unselected package netbase.
Unpacking netbase (from .../netbase_4.47ubuntu1_all.deb) ...
Selecting previously unselected package libsasl2-modules.
Unpacking libsasl2-modules (from .../libsasl2-modules_2.1.25.dfsg1-3ubuntu0.1_amd64.deb) ...
Selecting previously unselected package openssl.
Unpacking openssl (from .../openssl_1.0.1-4ubuntu5.16_amd64.deb) ...
Selecting previously unselected package libapr1.
Unpacking libapr1 (from .../libapr1_1.4.6-1_amd64.deb) ...
Selecting previously unselected package libaprutil1.
Unpacking libaprutil1 (from .../libaprutil1_1.3.12+dfsg-3_amd64.deb) ...
Selecting previously unselected package libaprutil1-dbd-sqlite3.
Unpacking libaprutil1-dbd-sqlite3 (from .../libaprutil1-dbd-sqlite3_1.3.12+dfsg-3_amd64.deb) ...
Selecting previously unselected package libaprutil1-ldap.
Unpacking libaprutil1-ldap (from .../libaprutil1-ldap_1.3.12+dfsg-3_amd64.deb) ...
Selecting previously unselected package apache2.2-bin.
Unpacking apache2.2-bin (from .../apache2.2-bin_2.2.22-1ubuntu1.6_amd64.deb) ...
Selecting previously unselected package apache2-utils.
Unpacking apache2-utils (from .../apache2-utils_2.2.22-1ubuntu1.6_amd64.deb) ...
Selecting previously unselected package libswitch-perl.
Unpacking libswitch-perl (from .../libswitch-perl_2.16-2_all.deb) ...
Selecting previously unselected package libclass-isa-perl.
Unpacking libclass-isa-perl (from .../libclass-isa-perl_0.36-3_all.deb) ...
Selecting previously unselected package perl-modules.
Unpacking perl-modules (from .../perl-modules_5.14.2-6ubuntu2.4_all.deb) ...
Selecting previously unselected package perl.
Unpacking perl (from .../perl_5.14.2-6ubuntu2.4_amd64.deb) ...
Selecting previously unselected package apache2.2-common.
Unpacking apache2.2-common (from .../apache2.2-common_2.2.22-1ubuntu1.6_amd64.deb) ...
Selecting previously unselected package apache2-mpm-worker.
Unpacking apache2-mpm-worker (from .../apache2-mpm-worker_2.2.22-1ubuntu1.6_amd64.deb) ...
Selecting previously unselected package apache2.
Unpacking apache2 (from .../apache2_2.2.22-1ubuntu1.6_amd64.deb) ...
Selecting previously unselected package ssl-cert.
Unpacking ssl-cert (from .../ssl-cert_1.0.28ubuntu0.1_all.deb) ...
Setting up libsqlite3-0 (3.7.9-2ubuntu1.1) ...
Setting up libroken18-heimdal (1.6~git20120311.dfsg.1-2ubuntu0.1) ...
Setting up libasn1-8-heimdal (1.6~git20120311.dfsg.1-2ubuntu0.1) ...
Setting up libgpg-error0 (1.10-2ubuntu1) ...
Setting up libgcrypt11 (1.5.0-3ubuntu0.2) ...
Setting up libgdbm3 (1.8.3-10) ...
Setting up libp11-kit0 (0.12-2ubuntu1) ...
Setting up libtasn1-3 (2.10-1ubuntu1.1) ...
Setting up libgnutls26 (2.12.14-5ubuntu3.8) ...
Setting up libhcrypto4-heimdal (1.6~git20120311.dfsg.1-2ubuntu0.1) ...
Setting up libheimbase1-heimdal (1.6~git20120311.dfsg.1-2ubuntu0.1) ...
Setting up libwind0-heimdal (1.6~git20120311.dfsg.1-2ubuntu0.1) ...
Setting up libhx509-5-heimdal (1.6~git20120311.dfsg.1-2ubuntu0.1) ...
Setting up libkrb5-26-heimdal (1.6~git20120311.dfsg.1-2ubuntu0.1) ...
Setting up libheimntlm0-heimdal (1.6~git20120311.dfsg.1-2ubuntu0.1) ...
Setting up libgssapi3-heimdal (1.6~git20120311.dfsg.1-2ubuntu0.1) ...
Setting up libsasl2-2 (2.1.25.dfsg1-3ubuntu0.1) ...
Setting up libldap-2.4-2 (2.4.28-1.1ubuntu4.4) ...
Setting up libcap2 (1:2.22-1ubuntu3) ...
Setting up libexpat1 (2.0.1-7.2ubuntu1.1) ...
Setting up libmagic1 (5.09-2ubuntu0.3) ...
Setting up file (5.09-2ubuntu0.3) ...
Setting up mime-support (3.51-1ubuntu1) ...
update-alternatives: using /usr/bin/see to provide /usr/bin/view (view) in auto mode.
Setting up netbase (4.47ubuntu1) ...
Setting up libsasl2-modules (2.1.25.dfsg1-3ubuntu0.1) ...
Setting up openssl (1.0.1-4ubuntu5.16) ...
Setting up libapr1 (1.4.6-1) ...
Setting up libaprutil1 (1.3.12+dfsg-3) ...
Setting up libaprutil1-dbd-sqlite3 (1.3.12+dfsg-3) ...
Setting up libaprutil1-ldap (1.3.12+dfsg-3) ...
Setting up apache2.2-bin (2.2.22-1ubuntu1.6) ...
Setting up apache2-utils (2.2.22-1ubuntu1.6) ...
Setting up libclass-isa-perl (0.36-3) ...
Setting up ssl-cert (1.0.28ubuntu0.1) ...
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (This frontend requires a controlling tty.)
debconf: falling back to frontend: Teletype
Setting up libswitch-perl (2.16-2) ...
Setting up perl-modules (5.14.2-6ubuntu2.4) ...
Setting up perl (5.14.2-6ubuntu2.4) ...
update-alternatives: using /usr/bin/prename to provide /usr/bin/rename (rename) in auto mode.
Setting up apache2.2-common (2.2.22-1ubuntu1.6) ...
Enabling site default.
Enabling module alias.
Enabling module autoindex.
Enabling module dir.
Enabling module env.
Enabling module mime.
Enabling module negotiation.
Enabling module setenvif.
Enabling module status.
Enabling module auth_basic.
Enabling module deflate.
Enabling module authz_default.
Enabling module authz_user.
Enabling module authz_groupfile.
Enabling module authn_file.
Enabling module authz_host.
Enabling module reqtimeout.
Setting up apache2-mpm-worker (2.2.22-1ubuntu1.6) ...
invoke-rc.d: policy-rc.d denied execution of start.
Setting up apache2 (2.2.22-1ubuntu1.6) ...
Processing triggers for libc-bin ...
ldconfig deferred processing now taking place
---> 634855a43331
Removing intermediate container 5765c09b530f
Successfully built 634855a43331
srijan@vboxtest:~/kishore$
现在不妨看一下我们的新映像。为此,我们可以使用docker映像命令来实现。
docker images srijan/custom1
srijan@vboxtest:~/kishore$ docker images srijan/custom1
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
srijan/custom1 latest 634855a43331 About a minute ago 277.4 MB
srijan@vboxtest:~/kishore$
这里,你会发现映像在默认情况下会安装apache2。不妨反复核对一下:
docker run -t -i srijan/apache2:webserver /bin/bash
现在,你可以进入新容器,而apache2和12.04ubuntu已预装。
5 主机名称的更改
假设你想更改主机名称,或者你想有一个定制的主机称,比如我这里的server1.example.com。
我会使用:
docker run -h 'server1.example.com' -t -i srijan/custom1 /bin/bash
它会得出映像容器,如下所示:
srijan@vboxtest:~/kishore$ docker run -h 'server1.example.com' -t -i srijan/custom1 /bin/bash
root@server1:/# hostname -f
server1.example.com
root@server1:/#
6 docker的实用命令集锦
pull(pull用于从注册中心拉取映像或软件库)
比如:
docker run -i -t ubuntu /bin/bash
commit(commit用于保存容器)
比如:
docker commit 73527b8b4261 srijan/apache2 8ce0ea7a1528
cp(将文件/文件夹从容器的文件系统拷贝到主机路径。路径相对于文件系统的根目录。)
比如:
docker cp CONTAINER:PATH HOSTPATH
其中的CONTAINER是容器,将文件/文件夹从PATH拷贝到HOSTPATH
start和stop容器:
docker start 4386fb97867d
docker stop 4386fb97867d
其中的4386fb97867d是你的容器编号
export(将文件系统的内容作为tar存档文件导出到STDOUT)
docker export 4386fb97867d > latest.tar
import(创建一个空的文件系统映像,将打包文件[.tar、.tar.gz、.tgz、.bzip、.tar.xz或.txz]的内容导入到里面,然后以可选方式标记它。)
docker import http://example.com/exampleimage.tgz
从本地文件导入:
通过pipe和stdin导入到docker。
cat exampleimage.tgz | sudo docker import - exampleimagelocal:new
从本地目录导入:
sudo tar -c . | sudo docker import - exampleimagedir
history(显示映像的历史记录)
docker history [OPTIONS] IMAGE
sudo docker history ea7d6801c538
images(它会显示映像)
docker images [OPTIONS] [NAME]
它后面跟一些选项,如下所示:
-a, --all=false 显示所有映像(默认情况下,过滤掉中间映像层)
-f, --filter=[]: 提供过滤器值(即“dangling=true”)
--no-trunc=false 不截短输出
-q, --quiet=false 只显示数字编号
info(显示整个系统的信息)
srijan@VE130214:~$ sudo docker info
srijan@vboxtest:~$ sudo docker info
Containers: 20
Images: 65
Storage Driver: aufs
Root Dir: /var/lib/docker/aufs
Dirs: 105
Execution Driver: native-0.2
Kernel Version: 3.13.0-30-generic
WARNING: No swap limit support
inspect(返回关于容器/映像的低级信息)
docker inspect CONTAINER|IMAGE [CONTAINER|IMAGE...]
kill(终止运行中的容器/发送SIGKILL,即指定信号)
docker kill [OPTIONS] CONTAINER [CONTAINER...]
login(注册或登录docker注册中心服务器,如果未指定任何服务器,https://index.docker.io/v1/为默认值。)
docker login localhost:8080
它会登录到自托管注册中心。
logs(读取容器的日志)
docker logs CONTAINER
ps(列出容器)
docker ps [OPTIONS]
它有下列选项:
-a, --all=false 显示所有容器。默认情况下只显示运行中的容器。
--before="" 只显示编号或名称之前创建的容器,包括非运行中的容器。
-l, --latest=false 只显示最近创建的容器,包括非运行中的容器。
-n=-1 显示n个最近创建的容器,包括非运行中的容器。
--no-trunc=false 不截短输出。
-q, --quiet=false 只显示数字编号。
-s, --size=false 显示大小。
--since="" 只显示自编号或名称以来创建的容器,包括非运行中的容器。
push(将映像或软件库推送到注册中心)
docker push NAME[:TAG]
restart(它将重启运行中的容器)
docker restart [OPTIONS] CONTAINER [CONTAINER...]
rm(它将删除一个或多个容器)
docker rm [OPTIONS] CONTAINER [CONTAINER...]
rmi(它将删除一个或多个映像)
docker rmi IMAGE [IMAGE...]
run(在新容器中运行命令)
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
它有下列选项:
-a, --attach=[] 连接到stdin、stdout或stderr
-c, --cpu-shares=0 处理器共享(相对权重)
--cidfile="" 将容器编号写入到文件
-d, --detach=false 分离模式:在后台运行容器,输出新的容器编号
--dns=[] 设置自定义DNS服务器
--dns-search=[] 设置自定义DNS搜索域
-e, --env=[] 设置环境变量
--entrypoint="" 覆盖映像的默认入口点
--env-file=[] 在行分隔的文件中读取ENV变量
--expose=[] 暴露来自容器的端口,又不将端口发布到你的主机
-h, --hostname="" 容器主机名称
-i, --interactive=false 让stdin保持开放,即便没有连接
--link=[] 将链接添加到另一个容器(名称:别名)
--lxc-conf=[] (lxc exec-driver only)添加自定义lxc选项--lxc-conf="lxc.cgroup.cpuset.cpus = 0,1"
-m, --memory="" 内存限制(格式:,其中unit = b, k, m or g)
--name="" 为容器赋予名称
--net="bridge" 为容器设置网络模式
'bridge':为docker网桥上的容器创建新的网络堆栈
'none':不为该容器创建任何网络机制
'container:<name|id>':重复使用另一个容器的网络堆栈
'host':使用容器里面的主机网络堆栈
-p, --publish=[] 将容器的端口发布到主机
格式:ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort
(使用“docker port”即可看到实际映射)
-P, --publish-all=false 将所有暴露的端口发布到主机接口
--privileged=false 为该容器赋予扩展后的权限
--rm=false 容器退出后,自动删除容器(与-d不兼容)
--sig-proxy=true 将所有收到的信号代理输出到进程(即便处于非-tty模式下)
-t, --tty=false 分配伪终端
-u, --user="" 用户名称或UID
-v, --volume=[] 绑定挂载卷(比如来自host:-v /host:/container,来自docker: -v /container)
--volumes-from=[] 从指定的一个或多个容器挂载卷
-w, --workdir="" 容器里面的工作目录
save(将映像保存到tar存档文件,默认情况下流式传输到stdout)
docker save IMAGE
search(搜索docker索引,寻找映像)
docker search TERM
tag(将映像标记到软件库)
docker tag [OPTIONS] IMAGE REGISTRYHOST/NAME[:TAG]
top(查询容器的运行中进程)
docker top CONTAINER [ps OPTIONS]
version(显示docker版本信息)
srijan@vboxtest:~$ sudo docker version
[sudo] password for srijan:
Client version: 1.0.1
Client API version: 1.12
Go version (client): go1.2.1
Git commit (client): 990021a
Server version: 1.0.1
Server API version: 1.12
Go version (server): go1.2.1
Git commit (server): 990021a
6 结束语
我试着让大家更加熟悉docker,但愿本文会帮助各位进一步了解docker,并在自己的测试/生产环境中充分利用docker。
想了解更多信息,请参阅http://www.docker.com/。
转载请注明原文地址:http://www.server110.com/docker/201411/11191.html