LoginSignup
0
0

More than 3 years have passed since last update.

goose設定ファイルに環境変数を読み込めない(Docker環境)

Posted at

概要

Docker環境でGo製マイグレーションツールのgooseを使用する際、設定ファイルであるdbconf.ymlが環境変数を読んでくれませんでした。

dbconf.yml

development:
  driver: mymysql
  open: $MYSQL_URI

環境変数は、$変数という形式で読み込んでくれるはずなのですが・・・(公式参照)

Dockerfile

FROM golang:1.14.2-alpine

WORKDIR /go/src/server
COPY . .
ENV GO111MODULE=on

RUN apk add --no-cache \
        alpine-sdk=1.0-r0 \
    && go get github.com/pilu/fresh \
              bitbucket.org/liamstask/goose/cmd/goose
CMD ["fresh"]

コンテナ内でgooseコマンドを使うため、コンテナ内にもgooseを導入してあります。

原因

コンテナ内では外部ファイルやコマンドで指定してあげないと、環境変数を読み込むことができないみたいです。
コンテナ内を確認したところ、.envがコピーされていなかったので、Dockerの使用上コピーできないようになっているんですかね?
Docker で環境変数をホストからコンテナに渡す方法(ホスト OS 側からゲスト OS に渡す方法各種)

解決

docker-compose.ymlにオプション追加

version: "3.7"

services:
  server:
    container_name: server
    build:
      context: ./
      dockerfile: Dockerfile.dev
    ports:
      - 8000:8000
    env_file:  # 左記オプション追加
      - .env
    depends_on:
      - db
    tty: true
    stdin_open: true
    volumes:
      - ./:/go/src/server
0
0
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
0
0