LoginSignup
2
3

More than 5 years have passed since last update.

Docker上でGo言語のcronを設定する。

Last updated at Posted at 2017-01-06

はまったのでメモ。

Dockerfile

FROM golang:latest

RUN apt-get update && apt-get install -y cron

# timezone
RUN cp /usr/share/zoneinfo/Asia/Tokyo /etc/localtime

#workspase
RUN mkdir -p /go/src/app
WORKDIR /go/src/app

# this will ideally be built by the ONBUILD below ;)
CMD ["go-wrapper", "run"]

ONBUILD COPY . /go/src/app
ONBUILD RUN go-wrapper download
ONBUILD RUN go-wrapper install

#localfileのenvから環境変数を取得
RUN env > /env

#cron setting
#分 時 日 月 曜日 <実行コマンド>
CMD echo '*/1 * * * * cd /go/src/app; env - `cat /env` go run /go/src/app/batch.go >> /var/log/cron.log 2>&1' | crontab - && cron -f

cronの時間の設定はこちらを参考にしてください。

はまった点

1つ目

cronの処理の際は環境変数がリセットされるみたいなので、envコマンドを使って環境変数を設定してあげる必要があった。

2つ目

env - `cat /env`

のシングルコーテーションをダブルコーテーションで実行しているとうまく動作しなかった。

デバッグ

〜〜 >> /var/log/cron.log 2>&1 〜〜

で標準logとエラーlogを記憶している。

参考

2
3
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
2
3