在Google Cloud Platform啟用Docker Machine
啟動Google Cloud上的Docker Machine
顧名思義,是在Google Cloud上建立一台雲端主機,並且使用上面的環境來玩Docker...
$ docker-machine create \
--driver google \
--google-project mitac-cp300-taipei101 \
google-machine
ex:
$ export project=your-project-ud
$ export machine_name=google-machine
$ docker-machine create \
> --driver google \
> --google-project $project $macine_name
Opening auth URL in browser.
https://accounts.google.com/o/oauth2/auth?client_id=22738965379-8arp7bah3uln9eoenproamovfjj1ac33.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcompute&state=st1442118741402125441
If the URL doesn't open please open it manually and copy the code here.
# 這邊會跳出認證視窗,請用具備該權限的Google Account進行認證
# 認證後,將瀏覽器索取到的code貼回來指令列
Enter code: 4/ixnKZTv91eLY24C-9l-R_...HS7kOujHc
Got code: 4/ixnKZTv91eLY24C-9l-R_...ZHS7kOujHc
Saving token in /Users/peihsinsu/.docker/machine/machines/google-machine/gce_token
Creating host...
Generating SSH Key
Creating instance.
Creating firewall rule.
Waiting for Instance...
Uploading SSH Key
Waiting for SSH Key
To see how to connect Docker to this machine, run: docker-machine env google-machine
如果有需要詳細指定zone, size等資訊,可以參考:
- --google-zone: The zone to launch the instance.
- --google-machine-type: The type of instance.
- --google-username: The username to use for the instance.
- --google-project: required The name of your project to use when launching the instance.
- --google-auth-token: Your oAuth token for the Google Cloud API.
- --google-scopes: The scopes for OAuth 2.0 to Access Google APIs. See Google Compute Engine Doc.
- --google-disk-size: The disk size of instance.
- --google-disk-type: The disk type of instance.
- --google-address: Instance’s static external IP (name or IP).
- --google-preemptible: Instance preemptibility.
當主機開好之後,也可以直接使用gcloud指令,或到Web Console上確認機器是否已經長出來:
$ gcloud --project=$project compute instances list
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
google-machine asia-east1-c f1-micro 10.240.87.117 104.199.154.208 RUNNING
切換Docker環境到Google Cloud上
$ docker-machine env google-machine
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://130.211.117.197:2376"
export DOCKER_CERT_PATH="/Users/peihsinsu/.docker/machine/machines/google-machine"
export DOCKER_MACHINE_NAME="google-machine"
# Run this command to configure your shell:
# eval "$(docker-machine env google-machine)"
$ eval $(docker-machine env google-machine)
$ env | grep DOCKER
DOCKER_HOST=tcp://130.211.117.197:2376
DOCKER_MACHINE_NAME=google-machine
DOCKER_TLS_VERIFY=1
DOCKER_CERT_PATH=/Users/peihsinsu/.docker/machine/machines/google-machine
啟動一台docker試試看
$ docker run -it peihsinsu/nodejs bash
這時候docker ps可以看到該主機已經run起來,此時連線檢視Compute Engine上的docker machine,也可以同時看到這台主機喔~
simonsu@google-machine:~$ sudo su -
root@google-machine:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
86539a78a4b0 peihsinsu/nodejs "bash" 4 minutes ago Up 4 minutes
cranky_cori
root@google-machine:~#
關閉Docker Machine
當操作結束,不想要這台Docker Machine時,也可以透過docker machine的指令簡單地把Google Cloud上的機器關閉:
$ docker-machine rm google-machine
Successfully removed google-machine