Mac OS X Yosemite 10.10.1安装Docker

时间:2021-07-26 15:13:41

2014-12-06 wcdj

摘要:本文记录Docker在OS X Yosemite的安装方法,也可参考Docker的这篇文章Installing Docker on Mac OS X


0 介绍

Docker is a platform for developers and sysadmins to develop, ship, and run applications. Docker lets you quickly assemble applications from components and eliminates the friction that can come when shipping code. Docker lets you get your code tested and deployed into productionas fast as possible.

Docker consists of:

  • The Docker Engine - our lightweight and powerful open source container virtualization technology combined with a work flow for building and containerizing your applications.
  • Docker Hub - our SaaS service for sharing and managing your application stacks.

Mac OS X Yosemite 10.10.1安装Docker

Mac OS X Yosemite 10.10.1安装Docker


1 安装

下载最新的安装包:Boot2Docker-1.3.2.pkg,安装完成后如下图所示:

Mac OS X Yosemite 10.10.1安装Docker


docker的默认安装目录:

Mac OS X Yosemite 10.10.1安装Docker


docker的相关用法:

gerryyang@mba:docker$docker
Usage: docker [OPTIONS] COMMAND [arg...]

A self-sufficient runtime for linux containers.

Options:
  --api-enable-cors=false                          Enable CORS headers in the remote API
  -D, --debug=false                                Enable debug mode
  -d, --daemon=false                               Enable daemon mode
  -G, --group="docker"                             Group to assign the unix socket specified by -H when running in daemon mode
                                                     use '' (the empty string) to disable setting of a group
  -H, --host=[]                                    The socket(s) to bind to in daemon mode or connect to in client mode, specified using one or more tcp://host:port, unix:///path/to/socket, fd://* or fd://socketfd.
  --tls=false                                      Use TLS; implied by tls-verify flags
  --tlscacert="/Users/gerryyang/.docker/ca.pem"    Trust only remotes providing a certificate signed by the CA given here
  --tlscert="/Users/gerryyang/.docker/cert.pem"    Path to TLS certificate file
  --tlskey="/Users/gerryyang/.docker/key.pem"      Path to TLS key file
  --tlsverify=false                                Use TLS and verify the remote (daemon: verify client, client: verify daemon)
  -v, --version=false                              Print version information and quit

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 a container's filesystem to the host path
    create    Create a new container
    diff      Inspect changes on a container's filesystem
    events    Get real time events from the server
    exec      Run a command in an existing container
    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 log in to a Docker registry server
    logout    Log out from a Docker registry server
    logs      Fetch the logs of a container
    port      Lookup the public-facing port that is NAT-ed to PRIVATE_PORT
    pause     Pause all processes within a container
    ps        List containers
    pull      Pull an image or a repository from a Docker registry server
    push      Push an image or a repository to a 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 on the Docker Hub
    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

Run 'docker COMMAND --help' for more information on a command.
gerryyang@mba:~$boot2docker 
Usage: boot2docker [<options>] {help|init|up|ssh|save|down|poweroff|reset|restart|config|status|info|ip|shellinit|delete|download|upgrade|version} [<args>]
gerryyang@mba:~$boot2docker init
Latest release for boot2docker/boot2docker is v1.3.2
Downloading boot2docker ISO image...
Success: downloaded https://github.com/boot2docker/boot2docker/releases/download/v1.3.2/boot2docker.iso
    to /Users/gerryyang/.boot2docker/boot2docker.iso
Generating public/private rsa key pair.
Your identification has been saved in /Users/gerryyang/.ssh/id_boot2docker.
Your public key has been saved in /Users/gerryyang/.ssh/id_boot2docker.pub.
The key fingerprint is:
3f:1d:a1:10:0e:db:68:1a:d1:8d:07:bd:c1:30:9d:f1 gerryyang@mba.local
The key's randomart image is:
+--[ RSA 2048]----+
|    ..=Ooo       |
|     .oBOo       |
|    . +.+oE .    |
|     +  .. . .   |
|    .   S . .    |
|         . . .   |
|          o .    |
|           .     |
|                 |
+-----------------+
gerryyang@mba:~$boot2docker start
Waiting for VM and Docker daemon to start...
.........................oooooooooooooooooooooooo
Started.
Writing /Users/gerryyang/.boot2docker/certs/boot2docker-vm/ca.pem
Writing /Users/gerryyang/.boot2docker/certs/boot2docker-vm/cert.pem
Writing /Users/gerryyang/.boot2docker/certs/boot2docker-vm/key.pem

To connect the Docker client to the Docker daemon, please set:
    export DOCKER_HOST=tcp://192.168.59.103:2376
    export DOCKER_CERT_PATH=/Users/gerryyang/.boot2docker/certs/boot2docker-vm
    export DOCKER_TLS_VERIFY=1
gerryyang@mba:~$docker run hello-world
Unable to find image 'hello-world' locally
hello-world:latest: The image you are pulling has been verified
511136ea3c5a: Pull complete 
7fa0dcdc88de: Pull complete 
ef872312fe1b: Pull complete 
Status: Downloaded newer image for hello-world:latest
Hello from Docker.
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (Assuming it was not already locally available.)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

For more examples and ideas, visit:
 http://docs.docker.com/userguide/
gerryyang@mba:~$docker version
Client version: 1.3.2
Client API version: 1.15
Go version (client): go1.3.3
Git commit (client): 39fa2fa
OS/Arch (client): darwin/amd64
Server version: 1.3.2
Server API version: 1.15
Go version (server): go1.3.3
Git commit (server): 39fa2fa
gerryyang@mba:~$
gerryyang@mba:~$docker search tutorial
NAME                                       DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
learn/tutorial                                                                             5                    
clusterhq/elasticsearch                    Elasticsearch 1.3.x image for ClusterHQ's ...   3                    [OK]
clusterhq/kibana                           Kibana 3.1.x image for ClusterHQ's flocker...   2                    [OK]
caterpillar/python-tutorial                                                                2                    
clusterhq/logstash                         Logstash 1.4.x image for ClusterHQ's flock...   2                    [OK]
namin/io.livecode.ch                       interactive programming tutorials, powered...   1                    [OK]
florentbenoit/docker-angularjs-tutorials                                                   1                    [OK]
caterpillar/java-tutorial                                                                  1                    
hyzhou/tutorial                                                                            0                    
bhim3003/tutorial                                                                          0                    
icesai/java-tutorial                                                                       0                    
pololin/java-tutorial                                                                      0                    
ray0410/java-tutorial                                                                      0                    
mzdaniel/buildbot-tutorial                                                                 0                    
ipush/java-tutorial                                                                        0                    
odewahn/jem-tutorial                                                                       0                    
vitojeng/java-tutorial                                                                     0                    
ryanking/kings-tutorial                                                                    0                    
sp013719/java-tutorial                                                                     0                    
odewahn/jem-tutorial-pyxie                                                                 0                    
johnstontrav/apache-do-tutorial                                                            0                    
reproducible/scipy-tutorial-2014                                                           0                    
barnybug/coreos-tutorial-nginx                                                             0                    
akamatsu/tutorial2                                                                         0                    
akamatsu/tutorial1                                                                         0                    
jbarbier/tutorial1                                                                         0                    
rdhyee/commoncrawltutorials                                                                0                    [OK]
yyao/scidbtutorialdocker                                                                   0                    [OK]
wush978/twdsc2014tutorial                                                                  0                    [OK]
sylvainlasnier/echo                        This is a docker tutorial.  This image run...   0                    [OK]
c3h3/dsc2014tutorial                                                                       0                    [OK]
johnsand/java-tutorial01                                                                   0                    
skrassiev/golang-docker-test-webbench      tutorial sandbox                                0                    [OK]
padraigobrien/hanna                        Cause I am following a tutorial on automat...   0                    [OK]
adamvduke/railstutorial                    First docker image.  Contains a sample rai...   0                    
cazcade/weave-multicast-tutorial                                                           0                    [OK]
sfilipov/rails-tutorial                                                                    0                    [OK]
neilellis/weave-multicast-tutorial                                                         0                    [OK]
kedarvaidya/rails-tutorial-v3-dev                                                          0                    [OK]
intrig/tutorial                                                                            0                    
richardqa/tutorial                                                                         0                    
zqhxuyuan/tutorial                                                                         0                    
asarcar/tutorial                                                                           0                 




其实在Mac上使用brew来安装docker更加方便。

brew update

brew install docker

brew link docker

brew link boot2docker

当用brew安装某个应用后,但没有成功创建brew link(比如,link的目录已经存在相同的老版本文件导致link创建失败),此时可以将老版本删除,然后再次执行brew link xxx就可以创建link了,后面就可以使用brew安装的新版本了。

例如:

gerryyang@mba:~$brew link boot2docker
Linking /usr/local/Cellar/boot2docker/1.4.1... 1 symlinks created
gerryyang@mba:~$ls -lh /usr/local/bin/boot2docker
lrwxr-xr-x 1 gerryyang admin 43 12 22 20:50 /usr/local/bin/boot2docker -> ../Cellar/boot2docker/1.4.1/bin/boot2docker*
gerryyang@mba:~$brew link boot2docker
Warning: Already linked: /usr/local/Cellar/boot2docker/1.4.1
To relink: brew unlink boot2docker && brew link boot2docker
gerryyang@mba:homebrew$boot2docker version
Boot2Docker-cli version: v1.4.1
Git commit: 43241cb

2 常见问题

(1) 新版本会解决。

gerryyang@mba:~$docker pull learn/tutorial
Pulling repository learn/tutorial
8dbd9e392a96: Error pulling image (latest) from learn/tutorial, endpoint: https://registry-1.docker.io/v1/, Untar exit status 1 [debug] archive.go:103 [tar autodetect] n: [253 55 122 88 90 0 0 4 230 28dbd9e392a96: Error pulling image (latest) from learn/tutorial, Untar exit status 1 [debug] archive.go:103 [tar autodetect] n: [253 55 122 88 90 0 0 4 230 214]
2014/12/07 22:50:52 exec: "xz": executable file not found in $PATH

https://github.com/docker/docker/issues/9327


(2) docker Error: client and server don't have same version

t looks like you need to upgrade the VM after installing boot2docker:

if you are upgrading from boot2docker 0.12 or later, you can update your existing virtual machine (after upgrading using the installer) using boot2docker stop && boot2docker download && boot2docker up - and you will not lose your existing data.

http://*.com/questions/24586573/docker-error-client-and-server-dont-have-same-version

http://kungpaocode.com/2014/11/docker-client-and-server-dont-have-same-version/


(3) 保存对容器的修改

gerryyang@mba:docker$docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
35445695c475        learn/ping:latest   "ping google.com"   14 minutes ago      Exited (0) 13 minutes ago                       serene_jang         
gerryyang@mba:docker$docker commit 354 gerryyang/ping
ffd0f8dae3ae658ed69ec65d1d15ee95cda9d483ea51491a785497a7843a0396
gerryyang@mba:docker$docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
gerryyang@mba:docker$docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
35445695c475        learn/ping:latest   "ping google.com"   14 minutes ago      Exited (0) 14 minutes ago                       serene_jang         
gerryyang@mba:docker$docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
gerryyang/ping      latest              ffd0f8dae3ae        18 seconds ago      139.5 MB
ubuntu              latest              9bd07e480c5b        2 weeks ago         192.7 MB
hello-world         latest              ef872312fe1b        11 weeks ago        910 B
learn/ping          latest              a1dbb48ce764        17 months ago       139.5 MB
learn/tutorial      latest              8dbd9e392a96        20 months ago       128 MB


(4) 提交image

gerryyang@mba:docker$docker push learn/ping
The push refers to a repository [learn/ping] (len: 1)
Sending image list

Please login prior to push:
Username: gerryyang
Password:
Email: diggwcdj@gmail.com
Account created. Please use the confirmation link we sent to your e-mail to activate it.
The push refers to a repository [learn/ping] (len: 1)
Sending image list
FATA[0045] Error: Status 403 trying to push repository learn/ping: Account is not Active 

gerryyang@mba:docker$docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
gerryyang/ping      latest              ffd0f8dae3ae        18 seconds ago      139.5 MB
ubuntu              latest              9bd07e480c5b        2 weeks ago         192.7 MB
hello-world         latest              ef872312fe1b        11 weeks ago        910 B
learn/ping          latest              a1dbb48ce764        17 months ago       139.5 MB
learn/tutorial      latest              8dbd9e392a96        20 months ago       128 MB
gerryyang@mba:docker$docker push gerryyang/ping
The push refers to a repository [gerryyang/ping] (len: 1)
Sending image list
Pushing repository gerryyang/ping (1 tags)
8dbd9e392a96: Image already pushed, skipping 
a1dbb48ce764: Image already pushed, skipping 
ffd0f8dae3ae: Image successfully pushed 
Pushing tag for rev [ffd0f8dae3ae] on {https://cdn-registry-1.docker.io/v1/repositories/gerryyang/ping/tags/latest}
gerryyang@mba:docker$

需要注册自己的账户,若在push时账户不存在会提示你对新的账户进行激活。

Mac OS X Yosemite 10.10.1安装Docker

查看repositories的信息:

Mac OS X Yosemite 10.10.1安装Docker



参考

[1] http://docs.docker.com/

[2] http://docs.docker.com/installation/#installation

[3] https://www.docker.com/tryit/#