Skip to content

Docker 101

changwu edited this page Mar 25, 2016 · 2 revisions

Docker 使用介紹

安裝 Docker Toolbox

官網下載 .dmg 或是用 brew cask 安裝

$ brew cask install dockertoolbox

工具箱包含以下元件:

  • Docker Machine for running the docker-machine binary
  • Docker Engine for running the docker binary
  • Docker Compose for running the docker-compose binary
  • Kitematic, the Docker GUI
  • a shell preconfigured for a Docker command-line environment
  • Oracle VM VirtualBox

因為 Docker daemon 採 Linux-specific kernel, 無法直接跑在 OSX 上, 必須使用 docker-machine 來新增或依附虛擬機來進行操作.

Docker Beta 未來將可在 Mac 上直接運行, 不需要虛擬機 [影片]

boot2docker

Docker 版本

2016/03/25 更新

$ docker --version
Docker version 1.10.3, build 20f81dd

$ docker-machine --version
docker-machine version 0.6.0, build e27fb87

$ pip install docker-compose
$ docker-compose --version
docker-compose version 1.6.2, build 4d72027

MacBook Pro Mid 2009 舊版本硬體有問題

Docker Quickstart Terminal

第一次啟動 Docker Quickstart Terminal

$ bash --login '/Applications/Docker/Docker Quickstart Terminal.app/Contents/Resources/Scripts/start.sh'
Creating CA: /Users/user/.docker/machine/certs/ca.pem
Creating client certificate: /Users/user/.docker/machine/certs/cert.pem
Running pre-create checks...
Creating machine...
(default) Copying /Users/user/.docker/machine/cache/boot2docker.iso to /Users/user/.docker/machine/machines/default/boot2docker.iso...
(default) Creating VirtualBox VM...
(default) Creating SSH key...
(default) Starting the VM...
(default) Check network to re-create if needed...
(default) Waiting for an IP...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: /usr/local/bin/docker-machine env default


                        ##         .
                  ## ## ##        ==
               ## ## ## ## ##    ===
           /"""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
           \______ o           __/
             \    \         __/
              \____\_______/


docker is configured to use the default machine with IP 192.168.99.100
For help getting started, check out the docs at https://docs.docker.com
  1. 開啟新的命令視窗
  2. 新增 VM 為 default 的虛擬機,如果不存在的話
  3. 指向 shell 環境變數給虛擬機
  4. 驗證 docker 有無跑起來

執行 hello-world image 測試 docker 是否正常工作

$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world

03f4658f8b78: Pull complete
a3ed95caeb02: Pull complete
Digest: sha256:8be990ef2aeb16dbcb9271ddfe2610fa6658d13f6dfb8bc72074cc1ca36966a7
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.
 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

Share images, automate workflows, and more with a free Docker Hub account:
 https://hub.docker.com

For more examples and ideas, visit:
 https://docs.docker.com/userguide/

Docker machine

linux_docker mac_docker

同前所述,Docker 無法直接運行在 OS X,需依附 VM 來執行,在 OS X 中當新增 Docker machine 時,Virtual Box driver 的基底 OS 為 boot2docker.iso,此外 Docker machine 依不同系統有不同的 driver。

不透過 Docker Quickstart Terminal

若不透過 Docker Quickstart Terminal 啟動新的虛擬機,需要執行的一般步驟如下:

1. 新增新的虛擬機

$ docker-machine create -d virtualbox default

 Creating VirtualBox VM...
 Creating SSH key...
 Starting VirtualBox VM...
 Starting VM...
 To see how to connect Docker to this machine, run: docker-machine env default

相同於下面的寫法

$ docker-machine create --driver virtualbox default

2. 列出可用的機器

$ docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER    ERRORS
default   -        virtualbox   Running   tcp://192.168.99.100:2376           v1.10.3

3. 取得虛擬機的環境

$ docker-machine env default
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/Users/user/.docker/machine/machines/default"
export DOCKER_MACHINE_NAME="default"
# Run this command to configure your shell:
# eval $(docker-machine env default)

4. 將 shell 連到虛擬機

$ eval $(docker-machine env default)

5. 執行 docker

同樣執行 hello-world 測試環境是否成功

$ docker run hello-world

安裝目錄

新增的機器會放在 ~/.docker/machine/machines/default

新增新的虛擬機

新增的主機叫做 dev

$ docker-machine create -d virtualbox dev
Running pre-create checks...
Creating machine...
(dev) Copying /Users/user/.docker/machine/cache/boot2docker.iso to /Users/user/.docker/machine/machines/dev/boot2docker.iso...
(dev) Creating VirtualBox VM...
(dev) Creating SSH key...
(dev) Starting the VM...
(dev) Check network to re-create if needed...
(dev) Waiting for an IP...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env dev

指定環境變數在當前的 shell

看出已新增兩台主機, 而目前因為環境變數設定的關係, 運行在 default 主機上

$ docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER    ERRORS
default   *        virtualbox   Running   tcp://192.168.99.100:2376           v1.10.3
dev       -        virtualbox   Running   tcp://192.168.99.101:2376           v1.10.3

再次設定環境變數, 只是這次是在主機 dev 上

$ eval $(docker-machine env dev)

dev 主機目前是 active 的狀態了

$ docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER    ERRORS
default   -        virtualbox   Running   tcp://192.168.99.100:2376           v1.10.3
dev       *        virtualbox   Running   tcp://192.168.99.101:2376           v1.10.3

遇到 cert 沒裝好

重新產生憑證

$ docker-machine regenerate-certs dev
Regenerate TLS machine certs?  Warning: this is irreversible. (y/n): y
Regenerating TLS certificates
Waiting for SSH to be available...
Detecting the provisioner...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...

移除虛擬機

docker-machine rm <machine-name>

$ docker-machine rm dev
About to remove dev
Are you sure? (y/n): y
Successfully removed dev
  1. Docker images
  2. Docker registeries
  3. Docker containers

參考資料

Clone this wiki locally