Skip to content

Instantly share code, notes, and snippets.

@keitaj
Created February 4, 2020 12:31
Show Gist options
  • Save keitaj/10441a7c9587221d86ee003ffd67289d to your computer and use it in GitHub Desktop.
Save keitaj/10441a7c9587221d86ee003ffd67289d to your computer and use it in GitHub Desktop.
Run docker container with arguments
version: "3.7"
services:
ecs-task:
image: ecs-task:latest
container_name: ecs-task
build:
context: ./
dockerfile: Dockerfile
environment:
- GO_ENV=docker
FROM golang:1.13.7-alpine3.10
ADD . /go/src/workspace/ecs-task
WORKDIR /go/src/workspace/ecs-task
RUN go build -o ecs-task && mv ecs-task /bin
CMD /bin/sh ./entrypoint.sh
#!bin/sh
ecs-task -start_time "$(date '+%Y-%m-%d %H:00:00')"

ファイル構成

  • ecs-task/docker-compose.yml
  • ecs-task/Dockerfile
  • ecs-task/entrypoint.sh
  • ecs-task/main.go
$ cd ecs-task
$ docker-compose build
$ docker-compose run --rm ecs-task
start_time:2020-02-04 12:00:00 +0000 UTC

Amazon ECSのタスクスケジュール機能とかで、実行時にtimestamp渡したい場合に使う

package main
import (
"errors"
"flag"
"fmt"
"log"
"os"
"time"
)
const (
timeFormat = "2006-01-02 15:04:05"
)
var (
startTime time.Time
)
func main() {
var startTimeStr string
flag.StringVar(&startTimeStr, "start_time", "", "time format 2006-01-02 15:04:05")
flag.Parse()
if startTimeStr == "" {
log.Fatal(errors.New("start_time is empty"))
}
startTime, err := time.Parse(timeFormat, startTimeStr)
if err != nil {
log.Fatal(err)
}
fmt.Printf("start_time:%v\n", startTime)
os.Exit(0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment