takaya030の備忘録

PHP、Laravel、Docker などの話がメインです

docker-compose を使って Google App Engine for Go の開発環境を作る

検証環境

Windows10 Home Edition
VirtualBox 5.1.22
Docker version 17.05.0-ce, build 89658be
docker-compose version 1.6.2, build 4d72027

ディレクトリ構成

+---gaego
|   |   docker-compose.yml
|   |   
|   +---config
|   |   
|   +---data
|   |       Dockerfile
|   |   
|   +---glide
|   |       
|   +---sdk
|           Dockerfile
|            
+---logs
|    
+---www
    |       
    +---app
            app.yaml
            hello.go

各種設定ファイル

gaego/docker-compose.yml

version: "2"
services:
  data:
    build: ./data
    volumes:
      - ../:/data
  sdk:
    build: ./sdk
    volumes:
      - ./config/:/home/docker/.config
      - ./glide/:/home/docker/.glide
    volumes_from:
      - data
    ports:
      - "8080:8080"
      - "8000:8000"

gaego/data/Dockerfile

FROM google/cloud-sdk:alpine
LABEL maintainer "takaya030"

# install go1.6
RUN curl -Lso go1.6.4.linux-amd64.tar.gz https://storage.googleapis.com/golang/go1.6.4.linux-amd64.tar.gz \
	&& tar -C /usr/local -xzf go1.6.4.linux-amd64.tar.gz \
	&& rm go1.6.4.linux-amd64.tar.gz

# install GAE for Go SDK
RUN gcloud components install app-engine-go

# install git
RUN apk add --update --no-cache \
		sudo \
		git

# create docker user
RUN adduser -S -u 1000 -g 50 docker \
	&& echo 'docker:tcuser' | chpasswd

# for saving gcloud config
RUN sudo -u docker gcloud config set core/disable_usage_reporting true && \
    sudo -u docker gcloud config set component_manager/disable_update_check true && \
    sudo -u docker gcloud config set metrics/environment github_docker_image

RUN mkdir -p /data/www \
    && mkdir -p /home/docker/.glide
VOLUME ["/data","/home/docker/.config","/home/docker/.glide"]
WORKDIR /data/www
ENV GOPATH /data/www
ENV PATH $PATH:/usr/local/go/bin:/data/www/bin

USER 1000

CMD ["dev_appserver.py","--host","0.0.0.0","--admin_host","0.0.0.0","./app"]

gaego/www/app/app.yaml

runtime: go
api_version: go1

handlers:
- url: /.*
  script: _go_app

gaego/www/app/hello.go

package hello

import (
    "fmt"
    "net/http"
)

func init() {
    http.HandleFunc("/", handler)
}

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Hello, world!")
}

イメージのビルド

gaego ディレクトリに移動後、以下のコマンドでイメージをビルドします。

$ docker-compose build

gcloud の初期設定

以下のコマンドで gcloud の初期設定を行います。この操作は一回実行するだけで OK です

$ docker-compose run --rm sdk gcloud config set core/disable_usage_reporting true
$ docker-compose run --rm sdk gcloud config set component_manager/disable_update_check tru
e
$ docker-compose run --rm sdk gcloud config set metrics/environment github_docker_image

gcloud info で設定内容が確認できます

$ docker-compose run --rm sdk gcloud info
Starting gaego_data_1
Google Cloud SDK [162.0.0]

Platform: [Linux, x86_64] ('Linux', '67fd8b0f0e41', '4.4.66-boot2docker', '#1 SMP Fri May 5 20:44:25 UTC 2017', 'x86_64', '')
Python Version: [2.7.13 (default, Dec 22 2016, 09:22:15)  [GCC 6.2.1 20160822]]
Python Location: [/usr/bin/python2]
Site Packages: [Disabled]

Installation Root: [/google-cloud-sdk]
Installed Components:
  core: [2017.07.07]
  app-engine-python: [1.9.57]
  app-engine-go: []
  gcloud: []
  gsutil: [4.27]
  bq: [2.0.24]
System PATH: [/google-cloud-sdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin:/data/www/bin]
Python PATH: [/google-cloud-sdk/lib/third_party:/google-cloud-sdk/lib:/usr/lib/python27.zip:/usr/lib/python2.7:/usr/lib/python2.7/plat-linux2:/usr/lib/python2.7/lib-tk:/usr/lib/python2.7/lib-old:/usr/lib/python2.7/lib-dynload]
Cloud SDK on PATH: [True]
Kubectl on PATH: [False]

Installation Properties: [/google-cloud-sdk/properties]
User Config Directory: [/home/docker/.config/gcloud]
Active Configuration Name: [default]
Active Configuration Path: [/home/docker/.config/gcloud/configurations/config_default]

Account: [None]
Project: [None]

Current Properties:
  [metrics]
    environment: [github_docker_image]
  [core]
    disable_usage_reporting: [true]
  [component_manager]
    disable_update_check: [true]

Logs Directory: [/home/docker/.config/gcloud/logs]
Last Log File: [/home/docker/.config/gcloud/logs/2017.07.22/02.04.22.201586.log]

git: [git version 2.11.2]
ssh: [OpenSSH_7.4p1, LibreSSL 2.4.4]

動作確認

以下のコマンドで開発用サーバーが起動します。

$ docker-compose up -d

web ブラウザで http://192.168.99.100:8080 にアクセスして "Hello, world!" の文字が表示されれば成功です。

http://192.168.99.100:8000 にアクセスすると管理ページが表示されます。
f:id:takaya030:20170708180943p:plain

更新履歴

  • (2017/08/04) glide のキャッシュフォルダを追加
  • (2017/07/22) gclod config のフォルダを volume 指定するように変更。gcloud の初期設定を追加