From fd91cbec7f70976d19531b89ab3eb890405c8240 Mon Sep 17 00:00:00 2001 From: eizyc Date: Mon, 24 Jun 2024 21:10:25 -0500 Subject: [PATCH] chore(docker): use docker-compose to manage two containers --- .github/workflows/{ci.yml => test.yml} | 2 +- Dockerfile | 11 ++++++++- docker-compose.yaml | 33 ++++++++++++++++++++++++++ markdown.md | 14 ++++++++++- start.sh | 11 +++++++++ 5 files changed, 68 insertions(+), 3 deletions(-) rename .github/workflows/{ci.yml => test.yml} (97%) create mode 100644 docker-compose.yaml create mode 100755 start.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/test.yml similarity index 97% rename from .github/workflows/ci.yml rename to .github/workflows/test.yml index b4cf495..2b0330a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/test.yml @@ -1,4 +1,4 @@ -name: ci-test +name: Run unit tests on: push: diff --git a/Dockerfile b/Dockerfile index 304489a..c558105 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,11 +3,20 @@ FROM golang:1.22.4-alpine3.20 AS builder WORKDIR /app COPY . . RUN go build -o main main.go +RUN apk add curl +RUN curl -L https://github.com/golang-migrate/migrate/releases/download/v4.17.1/migrate.linux-amd64.tar.gz | tar xvz # Run stage FROM alpine:3.20 WORKDIR /app COPY --from=builder /app/main . +COPY --from=builder /app/migrate ./migrate COPY app.env . +COPY start.sh . +COPY db/migration ./migration + + + EXPOSE 8080 -CMD ["/app/main"] \ No newline at end of file +CMD ["/app/main"] +ENTRYPOINT [ "/app/start.sh" ] \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..09b4c32 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,33 @@ +services: + postgres: + image: postgres:16-alpine + environment: + - POSTGRES_USER=root + - POSTGRES_PASSWORD=secret + - POSTGRES_DB=simple_bank + ports: + - "5432:5432" + healthcheck: + test: [ "CMD", "pg_isready", "-U", "root", "-d", "simple_bank" ] + interval: 10s + timeout: 5s + retries: 5 + api: + build: + context: . + dockerfile: Dockerfile + ports: + - "8080:8080" + environment: + - DB_SOURCE=postgresql://root:secret@postgres:5432/simple_bank?sslmode=disable + depends_on: + postgres: + condition: service_healthy + + entrypoint: + [ + "/app/start.sh" + ] + command: [ "/app/main" ] +volumes: + data-volume: \ No newline at end of file diff --git a/markdown.md b/markdown.md index 0b82584..3132af6 100644 --- a/markdown.md +++ b/markdown.md @@ -33,5 +33,17 @@ docker network create bank-network \\ connect network to postgresql container docker network connect bank-network postgres16 \\ container -docker run --name simplebank --network bank-network -p 8080:8080 -e GIN_MODE=release -e DB_SOURCE="postgresql://root:secret@postgres16:5432/simple_bank?sslmode=disable" simplebank:last +docker run --name simplebank --network bank-network -p 8080:8080 -e GIN_MODE=release -e DB_SOURCE="postgresql://root:secret@postgres:5432/simple_bank?sslmode=disable" simplebank:last +``` + +#### How to run docker-compose +``` +docker compose up +``` + +#### Modiy file permissions for start.sh wait-for.sh for Control startup and shutdown order in Compose +To make the api container run after by the postgres container, we have to use [`depends_on`](https://docs.docker.com/compose/startup-order/) and with `condition: service_healthy` +By Legacy versions, besides the depends_on field, we need some script like [wait-for](https://github.com/mrako/wait-for), new version docker compose supported inside. Anyway, If we want to use some scipts, don't miss change scipts permissions. +``` +chomd +x fileName ``` \ No newline at end of file diff --git a/start.sh b/start.sh new file mode 100755 index 0000000..e44f1f7 --- /dev/null +++ b/start.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +set -e + +echo "run db migration" +# ls -l +# source /app/app.env +/app/migrate -path /app/migration -database "$DB_SOURCE" -verbose up + +echo "start the app" +exec "$@" \ No newline at end of file