Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4427962
  • 博文数量: 252
  • 博客积分: 5347
  • 博客等级: 大校
  • 技术积分: 13838
  • 用 户 组: 普通用户
  • 注册时间: 2009-09-30 10:13
文章分类
文章存档

2022年(12)

2017年(11)

2016年(7)

2015年(14)

2014年(20)

2012年(9)

2011年(20)

2010年(153)

2009年(6)

分类: LINUX

2014-09-14 15:42:08

1.  docker安装

对于Docker的安装在官网上针对不同的操作系统分别进行了描述,,对于ubuntu操作系统来说必须是64位的,因为Docker的官网上只是提供了64位的docker,如果需要32位的docker则需要下载源码进行编译,这里有篇文章介绍了Docker源码如何进行编译成32位,。但是对于我使用的Ubuntu来说正好是32位的。通过下面的两条命令可以看出操作系统的位数和Linux的内核版本。

root@ubuntu:/tmp#uname -a

Linux e529c1b7772a 3.13.0-12-generic #32-Ubuntu SMP Fri Feb 21 17:44:24 UTC 2014 i686 i686 i686 GNU/Linux

root@ubuntu:/tmp#file /sbin/init

/sbin/init: ELF 32-bit LSB  shared object, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=1c47d1e162f6542994e457394ac52078da5cc023, stripped

         如果想在32为的Ubuntu操作系统上运行Docker的话,就必须安装32位的Docker。在Ubuntu中提供docker1.01-32位的deb包,可以直接点击“”跳转到下载页面。下载完成后,直接使用dpkg –i进行安装。

dpkg -i docker.io_1.0.1~dfsg1-0ubuntu1~ubuntu0.14.04.1_i386.deb

Selecting previously unselected package docker.io.

(Reading database ... 150021 files and directories currently installed.)

Preparing to unpack docker.io_1.0.1~dfsg1-0ubuntu1~ubuntu0.14.04.1_i386.deb ...

Unpacking docker.io (1.0.1~dfsg1-0ubuntu1~ubuntu0.14.04.1) ...

Setting up docker.io (1.0.1~dfsg1-0ubuntu1~ubuntu0.14.04.1) ...

docker.io start/running, process 10456

Processing triggers for ureadahead (0.100.0-16) ...

Processing triggers for man-db (2.6.6-1) ...

也可以参考下面的这篇文章《》进行安装。安装完成后,可以使用下面的命令确定安装的版本和基本的信息。由于docker使用go语言进行编写的,所以要依赖于GO的编译工具和库文件。

oot@ubuntu:/tmp# docker version

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

root@ubuntu:/tmp# docker info

Containers: 1

Images: 7

Storage Driver: aufs

 Root Dir: /var/lib/docker/aufs

 Dirs: 10

Execution Driver: native-0.2

Kernel Version: 3.13.0-12-generic

WARNING: No swap limit support

官方安装方式docker pull imagenamedocker的索引中心下载,imagename是镜像名称,例如docker pull ubuntu就是下载base ubuntu并且taglatest

root@ubuntu:/# docker pull ubuntu:14.04

Pulling repository ubuntu

c4ff7513909d: Download complete

511136ea3c5a: Download complete

1c9383292a8f: Download complete

9942dd43ff21: Download complete

d92c3c92fa73: Download complete

0ea0d582fd90: Download complete

cc58e55aa5a5: Download complete

root@ubuntu:~/Downloads# docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE

ubuntu           14.04           c1b5399bd2ee        8 minutes ago       205.3 MB

learn/tutorial      latest           8dbd9e392a96        17 months ago       128 MB

root@ubuntu:/# docker run -i -t ubuntu /bin/bash

2014/09/04 13:04:28 exec format error

docker run -i -t learn/tutorial /bin/bash

2014/09/04 14:45:13 exec format error

         PS:在安装完成Dokcer后,使用Pull命令sudo docker pull ubuntu:14.04从镜像仓库获取ubuntu的镜像后,然后使用run命令docker run -i -t ubuntu /bin/bash直接运行该镜像时,会出现下面的错误信息,后来我又尝试着使用官方演示使用的learn/tutorial镜像,还是出现同样的问题,现在怀疑可能是由于我安装的DOcker32位的,而Image64位的不匹配造成的。前面说过Docker默认提供的都是64位,其中提供的Image自然也都是64位,所以会出现下面的错误信息。在Docker Hub Registry里的大多数镜像都是64位。这里有一个关于该错误的讨论帖“

linux-image-amd64 ”。所以,如果想在32位的Docker基础上运行UbuntuImage那要求Ubuntuimage也必须是32位。这就需要制作32UbuntuImage。这里有一个官方提供的可用的Ubuntu image的列表,

2. 创建一个Image

         由于Docker Hub Registry上面的镜像都是64位的,所以这里需要自己做一个Ubuntu的镜像,这里使用import命令,该命令的官方解释为:Create  a new filesystem image from the contents of a tarball.这里使用openVZ的模板来创建,openVZ模板的下载地址为:,在该页面中有下面的Ubuntu模板可选择,

root@ubuntu:~/Downloads#cat ubuntu-14.04-x86-minimal.tar.gz | docker import - ubuntu:14.04

c1b5399bd2ee1a0e5f3bde58b55a73037249bd2b1ec44457a56f475d9ba60490

可以使用 Docker images命令查看,多了一个ubuntu的镜像。

root@ubuntu:~/Downloads# docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE

ubuntu              14.04        c1b5399bd2ee        8 minutes ago       205.3 MB

                    c4ff7513909d        3 weeks ago         225.4 MB

learn/tutorial      latest            8dbd9e392a96        17 months ago       128 MB

使用下面的命令进入交互的Shell

root@ubuntu:~/Downloads# docker run -i -t ubuntu:14.04 /bin/bash

root@051de31a5db3:/#

docker ps -a -q列出当前运行的容器, -a 会列出所有,包括已停止的, -q 只列出容器 ID docker ps -a -q | xargs docker rm 可以删除所有未运行的容器。

下面使用docker ps –a列出所有的容器,包括已经Exit

使用下面的命令进行删除:

root@ubuntu:/tmp# docker ps -a -q | xargs docker rm

Error response from daemon: Impossible to remove a running container, please stop it first or use -f

2cf226d796cb

4a9808366c8d

2014/09/05 07:48:03 Error: failed to remove one or more containers

         如果是正在运行的容器,必须在rm下使用参数-f

         这时运行的容器会自动退出

root@ubuntu:~/Downloads# docker run -i -t ubuntu:14.04 /bin/bash

root@579b1d296500:/# exit

root@ubuntu:~/Downloads#

3. Dokcer基本命令的使用

3.1  inspect命令

         当我们使用下面的run命令时,就会进入一个交互的shell,其中f78d7854879f数字表示的精简的Container ID,是Container ID的前12位。可以使用下面的两个命令查看完整的信息。如果要了解一个Image或者Container的完整构建信息就可以通过这个命令实现Inspect

root@ubuntu:~/Downloads# docker run -i -t ubuntu:14.04 /bin/bash

root@ f78d7854879f:/#

         可以使用docker ps –l查看,docker ps打印出正在运行的容器,docker ps -a打印出所有运行过的容器

使用 inspect命令:

root@ubuntu:~/Downloads# docker run -i -t ubuntu:14.04 /bin/bash

root@f78d7854879f:/# 

 

root@ubuntu:/tmp# docker inspect f78d7854879f

[{

    "Args": [],

    "Config": {

        "AttachStderr": true,

        "AttachStdin": true,

        "AttachStdout": true,

        "Cmd": [

            "/bin/bash"

        ],

        "CpuShares": 0,

        "Cpuset": "",

        "Domainname": "",

        "Entrypoint": null,

        "Env": [],

        "ExposedPorts": {},

        "Hostname": "f78d7854879f",

        "Image": "ubuntu:14.04",

        "Memory": 0,

        "MemorySwap": 0,

        "NetworkDisabled": false,

        "OnBuild": null,

        "OpenStdin": true,

        "PortSpecs": null,

        "StdinOnce": true,

        "Tty": true,

        "User": "",

        "Volumes": {},

        "WorkingDir": ""

    },

    "Created": "2014-09-09T11:52:32.140889905Z",

    "Driver": "aufs",

    "ExecDriver": "native-0.2",

    "HostConfig": {

        "Binds": null,

        "ContainerIDFile": "",

        "Dns": null,

        "DnsSearch": null,

        "Links": null,

        "LxcConf": [],

        "NetworkMode": "bridge",

        "PortBindings": {},

        "Privileged": false,

        "PublishAllPorts": false,

        "VolumesFrom": null

    },

    "HostnamePath": "/var/lib/docker/containers/f78d7854879f70b56d12f40faf08b8d461746cb15100ad9267ed3dd95b5d9956/hostname",

    "HostsPath": "/var/lib/docker/containers/f78d7854879f70b56d12f40faf08b8d461746cb15100ad9267ed3dd95b5d9956/hosts",

    "Id": "f78d7854879f70b56d12f40faf08b8d461746cb15100ad9267ed3dd95b5d9956",

    "Image": "c1b5399bd2ee1a0e5f3bde58b55a73037249bd2b1ec44457a56f475d9ba60490",

    "MountLabel": "",

    "Name": "/backstabbing_almeida",

    "NetworkSettings": {

        "Bridge": "docker0",

        "Gateway": "172.17.42.1",

        "IPAddress": "172.17.0.9",

        "IPPrefixLen": 16,

        "PortMapping": null,

        "Ports": {}

    },

    "Path": "/bin/bash",

    "ProcessLabel": "",

    "ResolvConfPath": "/var/lib/docker/containers/f78d7854879f70b56d12f40faf08b8d461746cb15100ad9267ed3dd95b5d9956/resolv.conf",

    "State": {

        "ExitCode": 0,

        "FinishedAt": "0001-01-01T00:00:00Z",

        "Paused": false,

        "Pid": 13785,

        "Running": true,

        "StartedAt": "2014-09-09T11:52:32.319172655Z"

    },

    "Volumes": {},

    "VolumesRW": {}

}

也可以通过下面的命令

docker images -notrunc=true

Warning: '-notrunc' is deprecated, it will be replaced by '--no-trunc' soon. See usage.

REPOSITORY          TAG                 IMAGE ID                                                           CREATED             VIRTUAL SIZE

ubuntu              14.04               c1b5399bd2ee1a0e5f3bde58b55a73037249bd2b1ec44457a56f475d9ba60490   4 days ago          205.3 MB

                            0ea0d582fd9027540c1f50c7f0149b237ed483d2b95ac8d107f9db5a912b4240   4 weeks ago         192.7 MB

learn/tutorial      latest              8dbd9e392a964056420e5d58ca5cc376ef18e2de93b5cc90e868a1bbc8318c1c   17 months ago       128 MB

3.2 commit 命令

         在运行run命令进入Container后,如果在该Container种创建了一个文件,如果这时直接退出,当再次执行run命令进入后,发现原先的新创建的文件并不存在。解决这个问题就必须使用commit命令进行提交,也就相当于保存你的容器状态,所以在下次使用时可以重用,当保存容器的时候,仅仅只是保存现在容器和创建容器时候的不同状态。

         例子1:没有使用commit命令提交

root@ubuntu:~/Downloads# docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE

ubuntu              14.04               c1b5399bd2ee        4 days ago          205.3 MB

                            0ea0d582fd90        4 weeks ago         192.7 MB

learn/tutorial      latest              8dbd9e392a96        17 months ago       128 MB

root@ubuntu:~/Downloads# docker run -i -t ubuntu:14.04 /bin/bash

root@ccf59c6f7dbc:/# cd /home/

root@ccf59c6f7dbc:/home# vim test_python.py

root@ccf59c6f7dbc:/home# cat test_python.py

#!/usr/bin/env python

root@ccf59c6f7dbc:/home# exit

exit

root@ubuntu:~/Downloads# docker run -i -t ubuntu:14.04 /bin/bash

root@53e45258eee5:/# cd home/

root@53e45258eee5:/home# ls -al

total 8

drwxr-xr-x  2 root root 4096 Jul 21 15:18 .

drwxr-xr-x 27 root root 4096 Sep  9 08:45 ..

         在执行run命令,进入容器后发现并没有我们创建的文件。

例子2:使用commit进行提交,使用docker images 发现多了一个镜像

root@ubuntu:~/Downloads# docker run -i -t ubuntu:14.04 /bin/bash

root@7aab28da9ee7:/# cd /home

root@7aab28da9ee7:/home# vim test_python.py

root@7aab28da9ee7:/home# cat test_python.py

#!/usr/bin env python

root@7aab28da9ee7:/home# exit

exit

root@ubuntu:~/Downloads# docker ps -l

CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                      PORTS               NAMES

7aab28da9ee7        ubuntu:14.04        /bin/bash           About a minute ago   Exited (0) 19 seconds ago                       romantic_leakey    

root@ubuntu:~/Downloads# docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE

ubuntu              14.04               c1b5399bd2ee        4 days ago          205.3 MB

                            0ea0d582fd90        4 weeks ago         192.7 MB

learn/tutorial      latest              8dbd9e392a96        17 months ago       128 MB

root@ubuntu:~/Downloads# docker commit 7aab28da9ee7 ubuntu:14.04-1

de581840727da1a9f1eb3e8afa3273a69e5630f0625714e6f97ec98537c25074

root@ubuntu:~/Downloads# docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE

ubuntu              14.04-1             de581840727d        4 seconds ago       205.3 MB

ubuntu              14.04               c1b5399bd2ee        4 days ago          205.3 MB

                            0ea0d582fd90        4 weeks ago         192.7 MB

learn/tutorial      latest              8dbd9e392a96        17 months ago       128 MB

root@ubuntu:~/Downloads# docker run -i -t ubuntu:14.04-1 /bin/bash   ---这里运行的是ubuntu:14.04-1

root@76b54055e745:/# cd home/

root@76b54055e745:/home# ls

test_python.py

root@76b54055e745:/home# cat test_python.py

#!/usr/bin env python

root@76b54055e745:/home#

         使用commit命令后会生成一个新的image,也就是我们保存的一个新的image

阅读(78650) | 评论(0) | 转发(3) |
给主人留下些什么吧!~~