Docker的稿件

启动Docker引擎

谷歌计算引擎 (GCE)

首先,我们将进行Google Compute Engine的设置,以确保其可用。

启动CoreOS

展示可用的图像。

$ gcloud compute images list
NAME                                PROJECT           ALIAS              DEPRECATED STATUS
centos-6-v20150710                  centos-cloud      centos-6                      READY
centos-7-v20150710                  centos-cloud      centos-7                      READY
coreos-alpha-758-1-0-v20150730      coreos-cloud                                    READY
coreos-beta-723-3-0-v20150710       coreos-cloud                                    READY
coreos-stable-723-3-0-v20150804     coreos-cloud      coreos                        READY
backports-debian-7-wheezy-v20150710 debian-cloud      debian-7-backports            READY
debian-7-wheezy-v20150710           debian-cloud      debian-7                      READY
debian-8-jessie-v20150710           debian-cloud      debian-8                      READY
container-vm-v20150715              google-containers container-vm                  READY
opensuse-13-1-v20150515             opensuse-cloud    opensuse-13                   READY
opensuse-13-2-v20150511             opensuse-cloud    opensuse-13                   READY
rhel-6-v20150710                    rhel-cloud        rhel-6                        READY
rhel-7-v20150710                    rhel-cloud        rhel-7                        READY
sles-11-sp4-v20150714               suse-cloud        sles-11                       READY
sles-12-v20150511                   suse-cloud        sles-12                       READY
ubuntu-1204-precise-v20150625       ubuntu-os-cloud   ubuntu-12-04                  READY
ubuntu-1404-trusty-v20150625        ubuntu-os-cloud   ubuntu-14-04                  READY
ubuntu-1410-utopic-v20150625        ubuntu-os-cloud   ubuntu-14-10                  READY
ubuntu-1504-vivid-v20150616a        ubuntu-os-cloud   ubuntu-15-04                  READY
windows-server-2008-r2-dc-v20150721 windows-cloud     windows-2008-r2               READY
windows-server-2012-r2-dc-v20150721 windows-cloud     windows-2012-r2               READY

显示可用的机器类型。由于显示的数量很多,所以随意使用grep进行筛选。

$ gcloud compute machine-types list|grep f1-micro
NAME           ZONE           CPUS MEMORY_GB DEPRECATED
f1-micro       europe-west1-d 1     0.60
f1-micro       asia-east1-a   1     0.60
f1-micro       asia-east1-b   1     0.60
f1-micro       asia-east1-c   1     0.60
f1-micro       europe-west1-b 1     0.60
f1-micro       europe-west1-c 1     0.60
f1-micro       us-central1-a  1     0.60
f1-micro       us-central1-c  1     0.60
f1-micro       us-central1-b  1     0.60
f1-micro       us-central1-f  1     0.60

根据选择的区域和机器型号,使用费用会有所不同,请查看价格表进行确认。在Google Compute Engine上启动CoreOS。使用以下命令:$ gcloud compute instances create –help。

$ gcloud compute instances create coreos --image coreos --machine-type f1-micro --zone us-central1-a
Created [https://www.googleapis.com/compute/v1/projects/xxx/zones/us-central1-a/instances/coreos].
NAME   ZONE          MACHINE_TYPE PREEMPTIBLE INTERNAL_IP    EXTERNAL_IP     STATUS
coreos us-central1-a f1-micro                 10.xxx.xxx.xxx 130.xxx.xxx.xxx RUNNING

使用gcloud compute instances list命令来确认实例的启动状态。

$ gcloud compute instances list
NAME   ZONE          MACHINE_TYPE PREEMPTIBLE INTERNAL_IP    EXTERNAL_IP     STATUS
coreos us-central1-a f1-micro                 10.xxx.xxx.xxx 130.xxx.xxx.xxx RUNNING

登入CoreOS

使用gcloud compute ssh命令进行登录。
在选项中指定用户名为core。

$ gcloud compute ssh core@coreos --zone us-central1-a
CoreOS stable (723.3.0)
core@coreos ~ $ 

终止CoreOS

使用gcloud compute instances delete命令删除实例。

$ gcloud compute instances delete coreos --zone us-central1-a
The following instances will be deleted. Attached disks configured to 
be auto-deleted will be deleted unless they are attached to any other 
instances. Deleting a disk is irreversible and any data on the disk 
will be lost.
 - [coreos] in [us-central1-a]

Do you want to continue (Y/n)?  

Deleted [https://www.googleapis.com/compute/v1/projects/xxx/zones/us-central1-a/instances/coreos].

确认在gcloud compute instances list命令的列表中没有显示。

$ gcloud compute instances list
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS

苹果操作系统X

在Mac OS X上,需要安装专用工具。

参照 https://docs.docker.com/installation/mac/ 安装Docker。

可以开始使用 boot2docker 命令了。

$ boot2docker status
poweroff

$ boot2docker up
2014/06/27 19:51:15 Waiting for VM to be started
.........................
2014/06/27 19:52:08 exit status 255
2014/06/27 19:52:08 Started.
2014/06/27 19:52:08 Auto detection of the VM's IP address.

$ boot2docker status
running

$ boot2docker down

$ boot2docker status
poweroff

登录到boot2docker

※ 在内部,我们使用VirtualBox上的TinyCore OS来启动Docker主机。

$ boot2docker ssh

将Mac OS X的卷载入到boot2docker中

安装sshfs-fuse。

$ tce-load -wi sshfs-fuse

我将创建一个挂载点。

$ sudo mkdir /mnt/opt

在Mac OS X上通过boot2docker建立sshfs连接。

$ sudo sshfs -o uid=0 -o gid=0 <username>@<your-mac-os-x-ip>:<your-mac-os-x-volume> /mnt/opt/

比如说,下面是一个例子命令。

$ sudo sshfs -o uid=0 -o gid=0 username@192.168.59.x:/opt /mnt/opt/

执行以下命令来卸载。

$ sudo umount /mnt/opt

以下是参考材料:

    • https://gist.github.com/codeinthehole/7ea69f8a21c67cc07293

 

    • https://github.com/boot2docker/boot2docker/issues/188

 

    http://mogproject.blogspot.com/2014/03/docker-how-networking-of-mac.html

Docker的基本操作

使用Docker搜索命令来搜索Docker镜像。

$ docker search michilu
NAME                          DESCRIPTION   STARS     OFFICIAL   TRUSTED
michilu/fedora-zero                         1                    [OK]

使用docker pull命令下载Docker镜像。

$ docker pull michilu/fedora-zero
Pulling repository michilu/fedora-zero
e5ff1cb2cc3b: Download complete 
511136ea3c5a: Download complete 
b645011af928: Download complete 
Status: Downloaded newer image for michilu/fedora-zero:latest

使用docker images命令来检查已经下载的Docker镜像。

$ docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
michilu/fedora-zero   latest              8f101956caef        5 days ago          120.8 MB

使用docker history命令可以查看Docker镜像的内容。

$ docker history michilu/fedora-zero
IMAGE               CREATED             CREATED BY                                      SIZE
e5ff1cb2cc3b        11 months ago        /bin/sh -c #(nop) ADD file:83a81760a7453ec85b   120.8 MB
b645011af928        11 months ago        /bin/sh -c #(nop) MAINTAINER ENDOH takanao <d   0 B
511136ea3c5a        2.164922 years ago                                                   0 B

通过docker run命令启动Docker容器。

$ docker run -i -t michilu/fedora-zero bash
bash-4.3#

返回Docker主机的提示符并退出。

bash-4.3# exit
exit
core@coreos ~ $

使用Docker ps命令来查看Docker容器的状态。

$ docker ps -a
CONTAINER ID        IMAGE                                COMMAND             CREATED             STATUS                      PORTS               NAMES
2bfdb2f48853        michilu/fedora-zero:latest   bash                16 minutes ago      Exited (0) 37 seconds ago                       naughty_mcclintock   

要启动STATUS为Exited的Docker容器,请执行docker start命令。

$ docker start <CONTAINER ID>
<CONTAINER ID>

$ docker ps
CONTAINER ID        IMAGE                        COMMAND             CREATED              STATUS              PORTS               NAMES
bc55f6991792        michilu/fedora-zero:latest   bash                About a minute ago   Up 29 seconds                           lonely_hawking      

要连接正在运行的Docker容器,需要执行docker attach命令。

$ docker attach <CONTAINER ID>
bash-4.3#

使用docker rm命令来删除Docker容器。

$ docker rm <CONTAINER ID>
<CONTAINER ID>

将Docker主机的卷分配给Docker容器。

使用-v选项进行指定。

$ docker run -it -v /mnt/opt:/opt michilu/fedora-zero ls /opt