From 017ed14462c8d1852c3058805cea6d17a94f0a12 Mon Sep 17 00:00:00 2001 From: simon987 Date: Fri, 3 Jan 2020 16:37:51 -0500 Subject: [PATCH] docker-compose setup --- DOCS.md | 47 +++--------------------------------- Dockerfile | 18 +++++--------- config.yml | 4 +-- docker-compose.yml | 27 +++++++++++++++++++++ storage/maintenance.go | 3 +++ web/Dockerfile | 12 +++++++++ nginx.conf => web/nginx.conf | 6 ++++- 7 files changed, 58 insertions(+), 59 deletions(-) create mode 100644 docker-compose.yml create mode 100644 web/Dockerfile rename nginx.conf => web/nginx.conf (90%) diff --git a/DOCS.md b/DOCS.md index 285d0df..65e71e9 100644 --- a/DOCS.md +++ b/DOCS.md @@ -2,53 +2,12 @@ ## Installation (Docker) -Prerequisites: -* You have a postgres container using the network `tt`, listening -for connections on `172.26.0.2:5432`. -Example: -`docker run -d --name tt_pg --network tt postgres:alpine` - - -1. Initialize the database - - ```bash - psql -h 172.26.0.2 -U postgres - > CREATE USER task_tracker; - > CREATE database task_tracker; - ``` -1. Write configuration file - +1. *(Optional)* Tweak configuration file `vim config.yml` - ```yaml - server: - address: "localhost:3010" - database: - conn_str: "postgres://task_tracker:task_tracker@172.26.0.2/task_tracker?sslmode=disable" - log_levels: ["error", "info", "warn"] - git: - webhook_hash: "sha256" - webhook_sig_header: "X-Gogs-Signature" - log: - level: "trace" - session: - cookie_name: "tt" - expiration: "8h" - monitoring: - snapshot_interval: "120s" - history_length: "400h" - maintenance: - reset_timed_out_tasks_interval: "5m" - ``` -1. Create task_tracker container: +1. + `docker-compose up` - ```bash - docker run --rm\ - -v $PWD/config.yml:/root/config.yml\ - --network tt\ - -p 0.0.0.0:12345:80\ - simon987/task_tracker - ``` ## Installation (Linux) diff --git a/Dockerfile b/Dockerfile index 5f65925..6daae80 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # Build API -FROM golang:1.11.5 as go_build +FROM golang:1.13 as go_build WORKDIR /go/src/github.com/simon987/task_tracker/ COPY .git .git @@ -10,17 +10,11 @@ COPY main main COPY storage storage RUN go get ./main/ && GOOS=linux CGO_ENABLED=0 go build -a -installsuffix cgo -o tt_api ./main/ -# Build Web -FROM node:10-alpine as npm_build -COPY ./web/ ./ -RUN cd ./angular/ && npm install -RUN cd ./angular/ && ./node_modules/@angular/cli/bin/ng build --prod --optimization --output-path "/webroot" +FROM scratch -FROM nginx:alpine -WORKDIR /root +WORKDIR /root/ -COPY nginx.conf schema.sql config.yml ./ -COPY --from=go_build ["/go/src/github.com/simon987/task_tracker/tt_api", "./"] -COPY --from=npm_build ["/webroot", "/webroot"] +COPY --from=go_build ["/go/src/github.com/simon987/task_tracker/tt_api", "/root/"] +COPY ["config.yml", "schema.sql", "/root/"] -CMD ["sh", "-c", "nginx -c /root/nginx.conf && /root/tt_api"] +CMD ["/root/tt_api"] diff --git a/config.yml b/config.yml index 4bb06de..d20fe7c 100644 --- a/config.yml +++ b/config.yml @@ -1,8 +1,8 @@ server: - address: "localhost:3010" + address: "0.0.0.0:3010" database: - conn_str: "user=task_tracker password=task_tracker dbname=task_tracker sslmode=disable" + conn_str: "postgres://task_tracker:task_tracker@tt_db/task_tracker?sslmode=disable" # log_levels: ["debug", "error", "trace", "info", "warn"] log_levels: ["error", "info", "warn"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..ba02d56 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,27 @@ +version: "2.1" +services: + tt: + build: . + depends_on: + tt_db: + condition: service_healthy + tt_web: + build: web/ + ports: + - 8080:80 + depends_on: + tt: + condition: service_started + tt_db: + image: postgres +# volumes: +# - ./tt_db_data:/var/lib/postgresql/data + environment: + - "POSTGRES_USER=task_tracker" + - "POSTGRES_PASSWORD=task_tracker" + healthcheck: + test: ["CMD-SHELL", "pg_isready -U task_tracker"] + interval: 3s + timeout: 2s + retries: 10 + diff --git a/storage/maintenance.go b/storage/maintenance.go index 30f06a5..c867938 100644 --- a/storage/maintenance.go +++ b/storage/maintenance.go @@ -25,6 +25,9 @@ func (database *Database) ResetTimedOutTasks() { WHERE status=1 AND assignee IS NOT NULL AND extract(epoch from now() at time zone 'utc') > (assign_time + max_assign_time);`) handleErr(err) + if err != nil { + return + } rowsAffected, _ := res.RowsAffected() diff --git a/web/Dockerfile b/web/Dockerfile new file mode 100644 index 0000000..1029f5d --- /dev/null +++ b/web/Dockerfile @@ -0,0 +1,12 @@ +FROM node:10-alpine as npm_build +COPY ./ ./ +RUN cd ./angular/ && npm install +RUN cd ./angular/ && ./node_modules/@angular/cli/bin/ng build --prod --optimization --output-path "/webroot" + +FROM nginx:alpine +WORKDIR /root + +COPY nginx.conf /etc/nginx/ +COPY --from=npm_build ["/webroot", "/webroot"] + +EXPOSE 80 diff --git a/nginx.conf b/web/nginx.conf similarity index 90% rename from nginx.conf rename to web/nginx.conf index d28596a..637eeb4 100644 --- a/nginx.conf +++ b/web/nginx.conf @@ -24,6 +24,10 @@ http { keepalive_timeout 65; + upstream api { + server tt:3010; + } + server { listen 80; @@ -39,7 +43,7 @@ http { proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; - proxy_pass http://127.0.0.1:3010$1?$args; + proxy_pass http://api$1?$args; } } } \ No newline at end of file