9
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Go1.13とDockerで開発環境構築(ホットリロード有)

9
Posted at

はじめに(背景)

筆者は今までGoでは簡単なツール(Slack用Botとか)しか作ってこなかったが、
Goを使ってWEBアプリを開発してみることにした。 

ところが、開発環境を構築するのに、検索でヒットする情報が微妙だった

  • Goのバージョンが古くてGo Modulesを使っていなかったり、
  • 使っているライブラリの最終更新が3年前だったり

情報を整理しながら開発環境を構築する必要が有ったので、
せっかくだからアウトプットしておこうと思った。

前提

  • Goのversionは、執筆時点で最新の1.13系を使う
  • docker-composeで立ち上げたい
  • せっかくなので、ホットリロードが欲しい

以下には触れません

  • GoでのWEBサーバーの立ち上げ方
  • Go Modulesについて
  • Docker、docker-composeの使い方

本題

Goの設定

Go Modulesを使う

Go Modulesの導入や使い方などは割愛
↓のコマンドでinitialize
go mod init github.com/username/project

ホットリロードの設定

色々調べたら、Realizeが良さそう(適当)
https://github.com/oxequa/realize

設定ファイル(後述)を用意して、
realize startコマンドを実行するだけ

Docker環境用意

Dockerfile


FROM golang:1.13-alpine

WORKDIR /app

COPY . .

RUN apk add --no-cache git \
  && go get gopkg.in/urfave/cli.v2@master \
  && go get github.com/oxequa/realize

 
.realize.yml (ホットリロードの設定)

settings:
  legacy:
    force: false
    interval: 0s
schema:
- name: hoge
  path: path/to/server
  commands:
    install:
      status: true
    run:
      status: true
  watcher:
    extensions:
    - go
    paths:
    - path/from/server
    ignore_paths:
    - .git

 
docker-compose.yml

version: "3"
services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 8080:8080
    volumes:
      - ./:/app
    command: realize start

 

※realizeの設定でエラーが出た。(↑のファイルには反映済み)

パッケージを取得する時
解決方法は以下のissueから
https://github.com/oxequa/realize/issues/253
↓の箇所

RUN apk add --no-cache git \
  && go get gopkg.in/urfave/cli.v2@master \
  && go get github.com/oxequa/realize

 

Go Modules環境起因でもエラー
以下のissueから
https://github.com/oxequa/realize/issues/217
↓の箇所

  commands:
    install:
      status: true

おわりに

以上で、DockerでGoを開発する環境ができました。
あとはdocker-composeにDBなり何なりを追加すれば進められそうです。

 

9
6
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
9
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?