LoginSignup
4
4

More than 5 years have passed since last update.

Sierraの人のために、GoをDocker上でクロスコンパイルさせる方法

Last updated at Posted at 2017-01-24

概要

イメージ作成

①ファイル作成

$ go get -u github.com/sioncojp/test-app
$ mkdir /tmp/work
$ cp -R $GOPATH/src/github.com/sioncojp/test-app /tmp/work/app
$ cp -R $GOPATH/src/github.com/sioncojp/test-app/Makefile /tmp/work/Makefile
$ touch /tmp/work/Dockerfile

②Dockerfile

$ vim /tmp/work/Dockerfile
FROM golang:1.6.4
MAINTAINER Shohei Koyama sion_cojp@yahoo.co.jp

### 今回のケースはないんですが、buildする際に使う "$GOPATH/bin" をあらかじめ作っておいたほうが時間短縮になります。
### なので一度ターゲットのリポジトリをbuildしておきましょう。
RUN mkdir -p /go/src/github.com/sioncojp/test-app
COPY app /go/src/github.com/sioncojp/test-app/
COPY Makefile /root/Makefile

RUN sed -i "1iGOPATH=/go" /root/Makefile
RUN sed -i -e 's/go install/@cd $(GOPATH)\/src\/github.com\/sioncojp\/test-app \&\& GOOS=darwin GOARCH=amd64 go install/g' /root/Makefile

RUN cd /root && make install
RUN rm -rf go/bin/darwin_amd64/test-app
RUN rm -rf /go/src

③Dockerイメージを作る

### 今回はapp-build:latestという名前で作ります
$ cd /tmp/work
$ docker build -t app-build:latest .

イメージができたのでクロスコンパイルする

### ビルドしたものを入れるディレクトリ作成
$ mkdir /tmp/app-build/

### $GOPATH/srcを、コンテナの/go/srcにマウントしてイメージから立ち上げる
$docker run --name hoge -d -v $GOPATH/src/:/go/src -v /tmp/app-build/:/tmp/ -it app-build:latest

### クロスコンパイル
$ docker exec hoge bash -c 'cd /root && make install && mv -f $GOPATH/bin/darwin_amd64/test-app /tmp/test-app'
# 終わったら/tmp/app-build/test-app が出来てると思います

### コンテナ削除
$ docker rm -f hoge

### 実行してみる
$ /tmp/test-app
$ $ curl --unix-socket /tmp/app.sock http://index.html
It works!
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4