こんにちは。KOUKIです。
とあるWeb系企業でGoエンジニアをしています。
今回は、GolangとMongoDBの開発環境をDocker上で構築する方法を記事にしました。

事前準備
1 2 3 4 5 6 7 8 9 10 11 |
# フォルダ/ファイル mkdir go-app cd go-app touch main.go touch .air.toml touch Dockerfile touch docker-compose.yml # モジュール go mod init go-app go get go.mongodb.org/mongo-driver |
Docker
Dockerは、開発環境構築に便利なコンテナ技術です。
Dockerfile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# Dockerfile-golang FROM golang:1.16 WORKDIR /app COPY go.* . # airの設定ファイルをコピー COPY *air.toml . RUN go mod download COPY . . RUN curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s -- -b $(go env GOPATH)/bin CMD ["air"] |
docker-compose.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
version: "3.1" services: backend: build: context: . dockerfile: ./Dockerfile ports: - 4000:4000 volumes: - .:/app environment: - MONGO_URI=mongodb://admin:password@mongo:27017/test?authSource=admin depends_on: - mongo mongo: image: mongo:4.4.3 environment: MONGO_INITDB_ROOT_USERNAME: admin MONGO_INITDB_ROOT_PASSWORD: password ports: - 27017:27017 volumes: - ./db:/data/db - ./configdb:/data/configdb |
main.go
GoからMongoDBに接続するための簡単なサンプルコードを記載します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package main import ( "context" "log" "os" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/readpref" ) func init() { ctx := context.Background() log.Println(os.Getenv("MONGO_URI")) client, err := mongo.Connect(ctx, options.Client().ApplyURI(os.Getenv("MONGO_URI"))) if err = client.Ping(context.TODO(), readpref.Primary()); err != nil { log.Fatal(err) } log.Println("Connection to MongoDB") } func main() { log.Println("Go Run") } |
ホットリロード
Go製のairを使うとソースコードの変更を検知してビルドを自動で行ってくれるので、導入しておきましょう。
.air.tomlに設定情報を記述します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# .air.toml # Config file for [Air](https://github.com/cosmtrek/air) in TOML format # Working directory # . or absolute path, please note that the directories following must be under root. root = "." tmp_dir = "tmp" [build] # Just plain old shell command. You could use `make` as well. cmd = "go build -o ./tmp/main ." # Binary file yields from `cmd`. bin = "tmp/main" # Customize binary. full_bin = "APP_ENV=dev APP_USER=air ./tmp/main" # Watch these filename extensions. include_ext = ["go", "tpl", "tmpl", "html"] # Ignore these filename extensions or directories. exclude_dir = ["assets", "tmp", "vendor"] # Watch these directories if you specified. include_dir = [] # Exclude files. exclude_file = [] # This log file places in your tmp_dir. log = "air.log" # It's not necessary to trigger build each time file changes if it's too frequent. delay = 1000 # ms # Stop running old binary when build errors occur. stop_on_error = true # Send Interrupt signal before killing process (windows does not support this feature) send_interrupt = false # Delay after sending Interrupt signal kill_delay = 500 # ms [log] # Show log time time = false [color] # Customize each part's color. If no color found, use the raw app log. main = "magenta" watcher = "cyan" build = "yellow" runner = "green" [misc] # Delete tmp directory on exit clean_on_exit = true |
起動確認
準備が整ったので、下記のコマンドで起動を確認します。
1 2 3 |
docker-compose up backend_1 | 2021/08/29 01:11:03 Connection to MongoDB backend_1 | 2021/08/29 01:11:03 Go Run |
Connection to MongoDBがコンソール上に表示されたら接続成功です。
コメントを残す
コメントを投稿するにはログインしてください。