凌云的博客

行胜于言

Docker 入门

分类:other| 发布时间:2014-06-20 20:55:06


简介

Docker是基于LXC(Linux Container),由Docker Engine(轻量级的运行和打包工具), Docker Hub(用于分享和自动构建的云服务)组成的开放平台。 非常适合开发者和系统管理员用于构建,发布和运行应用。

安装

Docker支持在Linux,Windows和Mac OS X以及Amazon EC2等平台安装 (由于是基于LXC因此Windows和Mac OS X上的Docker其实是运行在一个Ubuntu的虚拟机上的)。

在Ubuntu 14.04下可以通过以下命令安装Docker

由于在Ubuntu下已经有名为docker的安装包,所以Docker的安装包为docker.io

$ 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

如果你希望安装最新版本的Docker可以使用以下方法:

首先,检查你的APT系统是否支持https

[ -e /usr/lib/apt/methods/https ] || {
      apt-get update
            apt-get install apt-transport-https
}

然后,添加Docker的key

$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9

更新你的source list,然后安装lxc-docker

$ sudo sh -c "echo deb https://get.docker.io/ubuntu docker main\
> /etc/apt/sources.list.d/docker.list"
$ sudo apt-get update
$ sudo apt-get install lxc-docker

其他平台的安装可以参考: Docker Installation

快速入门

学习Docker的最佳方法是实践,下面我们来看看Docker最常用的操作。

查看Docker的版本

Docker Engine主要由两部分组成:一个用于运行和管理所有容器的守护进程, 以及一个用于发送请求到守护进程的客户进程。

可以通过docker version查看

$ sudo 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

可以查看Client和Server的版本号,以及对应的Go版本号(Docker使用Go语言开发)

当然你也可以直接键入docker查看Docker有哪些子命令:

$ docker
Usage: docker [OPTIONS] COMMAND [arg...]
 -H=[unix:///var/run/docker.sock]: tcp://host:port to bind/connect to or unix://path/to/socket to use

 A self-sufficient runtime for linux containers.

 Commands:
     attach    Attach to a running container
     build     Build an image from a Dockerfile
     commit    Create a new image from a container's changes
     cp        Copy files/folders from the containers filesystem to the host path
     diff      Inspect changes on a container's filesystem
     events    Get real time events from the server
     export    Stream the contents of a container as a tar archive
     history   Show the history of an image
     images    List images
     import    Create a new filesystem image from the contents of a tarball
     info      Display system-wide information
     inspect   Return low-level information on a container
     kill      Kill a running container
     load      Load an image from a tar archive
     login     Register or Login to the docker registry server
     logs      Fetch the logs of a container
     port      Lookup the public-facing port which is NAT-ed to PRIVATE_PORT
     pause     Pause all processes within a container
     ps        List containers
     pull      Pull an image or a repository from the docker registry server
     push      Push an image or a repository to the docker registry server
     restart   Restart a running container
     rm        Remove one or more containers
     rmi       Remove one or more images
     run       Run a command in a new container
     save      Save an image to a tar archive
     search    Search for an image in the docker index
     start     Start a stopped container
     stop      Stop a running container
     tag       Tag an image into a repository
     top       Lookup the running processes of a container
     unpause   Unpause a paused container
     version   Show the docker version information
     wait      Block until a container stops, then print its exit code

搜索images

可以通过docker search命令查找云端的images

$ sudo docker search

Usage: docker search TERM

Search the docker index for images

  --automated=false    Only show automated builds
  --no-trunc=false     Don't truncate output
  -s, --stars=0        Only displays with at least xxx stars

比如,查找名称为ubuntu的images

$ sudo docker search ubuntu
NAME                         DESCRIPTION                                     STARS  OFFICIAL   AUTOMATED
ubuntu                       Official Ubuntu base image                      322                  
stackbrew/ubuntu             Official Ubuntu base image                      38                   
crashsystems/gitlab-docker   A trusted, regularly updated build of GitL...   19                [OK]
dockerfile/ubuntu            Trusted Ubuntu (http://www.ubuntu.com/) Bu...   15                [OK]
litaio/ruby                  Ubuntu 14.04 with Ruby 2.1.2 compiled from...   5                 [OK]
...

下载容器images

可以通过docker pull(用过git的人应该很熟悉)来下载容器images

$ sudo docker pull   

Usage: docker pull NAME[:TAG]

Pull an image or a repository from the registry

例如,下载名称为ubuntu的images

$ sudo docker pull ubuntu:14.04

Hello World

你可以将容器想象为在盒子中的一个进程。这个盒子包含进程所需要的一切东西,比如:文件系统,系统库以及shell等。 你可以通过运行一个进程来start一个容器。

可以通过docker run来在一个新的容器中运行进程:

$ sudo docker run 

Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Run a command in a new container

比如,我们可以通过下面的命令运行下载的images并且输出Hello World

$ sudo docker run ubuntu:14.04 echo "Hello World"
Hello World

安装东西

现在,让我们来在容器上安装一些工具:vim。我们可以在容器中运行apt-get install -y vim来进行安装。 虽然,容器会在这个命令执行完后停止,但是,这些改变不会消失。

$ sudo docker run ubuntu:14.04 apt-get install -y vim

保存你的修改

在上小节中,我们在容器中安装了vim,你或许想将这个修改保存起来,这样你就可以 从这一点开始运行你的程序。在Docker中,保存修改的过程称为commit。

首先,通过docker ps -l找出最后一个容器的容器ID

$ sudo docker ps -l                
CONTAINER ID  IMAGE         COMMAND               CREATED        STATUS                    PORTS  NAMES
a9e02103ecbd  ubuntu:14.04  apt-get install -y v  6 minutes ago  Exited (0) 5 minutes ago         hungry_wilson

然后,我们可以通过docker commit命令来保存修改

$ sudo docker commit

Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

Create a new image from a container's changes

  -a, --author=""     Author (eg. "John Hannibal Smith <hannibal@a-team.com>"
  -m, --message=""    Commit message

$ sudo docker commit a9e02103ecbd test/vim
f40406b064cc4dcd6b33fe5a0554f984ba4ddb11e8563d55bc22114117bf4ae6

运行你的新images

在上一节中,我们保存了安装vim后的修改。现在,让我们来运行这个新的images

$ sudo docker run test/vim vim --version

检查你的容器

我们可以通过docker ps命令来查看现有的容器

$ sudo docker ps --help

Usage: docker ps [OPTIONS]

List containers

  -a, --all=false       Show all containers. Only running containers are shown by default.
  --before=""           Show only container created before Id or Name, include non-running ones.
  -l, --latest=false    Show only the latest created container, include non-running ones.
  -n=-1                 Show n last created containers, include non-running ones.
  --no-trunc=false      Don't truncate output
  -q, --quiet=false     Only display numeric IDs
  -s, --size=false      Display sizes
  --since=""            Show only containers created since Id or Name, include non-running ones.

通过docker ps -a查看所有的容器

$ sudo docker ps -a        
CONTAINER ID  IMAGE            COMMAND                CREATED           STATUS                        PORTS   NAMES
114876491b4e  test/vim:latest  vim --version          4 minutes ago     Exited (0) 4 minutes ago              tender_lovelace
a9e02103ecbd  ubuntu:14.04     apt-get install -y v   21 minutes ago    Exited (0) 19 minutes ago             hungry_wilson
a90a01068547  ubuntu:14.04     echo 'Hello World'     About an hour ago Exited (0) About an hour ago          hungry_brattain

通过docker insepct可以查看容器的有用信息:

$ sudo docker inspect 114

参考

Docker Try It