Skip to content

Commit

Permalink
chore(docker): use docker-compose to manage two containers
Browse files Browse the repository at this point in the history
  • Loading branch information
eizyc committed Jun 25, 2024
1 parent b278585 commit fd91cbe
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml → .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: ci-test
name: Run unit tests

on:
push:
Expand Down
11 changes: 10 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
CMD ["/app/main"]
ENTRYPOINT [ "/app/start.sh" ]
33 changes: 33 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -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:
14 changes: 13 additions & 1 deletion markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
11 changes: 11 additions & 0 deletions start.sh
Original file line number Diff line number Diff line change
@@ -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 "$@"

0 comments on commit fd91cbe

Please sign in to comment.