Skip to content

Commit 7910404

Browse files
committedDec 28, 2022
⭐ add Dockerfile
1 parent c09f90a commit 7910404

File tree

5 files changed

+253
-1
lines changed

5 files changed

+253
-1
lines changed
 

‎Dockerfile

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Build stage
2+
FROM golang:1.19-alpine3.16 AS builder
3+
WORKDIR /app
4+
COPY . .
5+
RUN go build -o main main.go
6+
RUN apk add curl
7+
RUN curl -L https://github.com/golang-migrate/migrate/releases/download/v4.14.1/migrate.linux-amd64.tar.gz | tar xvz
8+
9+
# Run stage
10+
FROM alpine:3.16
11+
WORKDIR /app
12+
COPY --from=builder /app/main .
13+
COPY --from=builder /app/migrate.linux-amd64 ./migrate
14+
COPY app.env .
15+
COPY start.sh .
16+
COPY wait-for.sh .
17+
COPY db/migrations ./db/migrations
18+
19+
EXPOSE 8080
20+
CMD [ "/app/main" ]
21+
ENTRYPOINT [ "/app/start.sh" ]

‎Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
postgres:
2-
docker run --name postgres14 -p 5555:5432 -e POSTGRES_USER=mars -e POSTGRES_PASSWORD=mars -d postgres:14
2+
docker run --name postgres14 --network bank-network -p 5555:5432 -e POSTGRES_USER=mars -e POSTGRES_PASSWORD=mars -d postgres:14
33

44
createdb:
55
docker exec -it postgres14 createdb --username=mars --owner=mars tinybank

‎docker-compose.yaml

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
version: "3.9"
2+
3+
networks:
4+
bank-network:
5+
driver: bridge
6+
7+
services:
8+
postgres:
9+
image: postgres:14-alpine
10+
environment:
11+
- POSTGRES_USER=root
12+
- POSTGRES_PASSWORD=secret
13+
- POSTGRES_DB=simple_bank
14+
restart: unless-stopped
15+
ports:
16+
- "5432:5432"
17+
networks:
18+
- bank-network
19+
api:
20+
build:
21+
context: .
22+
dockerfile: Dockerfile
23+
ports:
24+
- "8080:8080"
25+
environment:
26+
- DB_SOURCE=postgresql://root:secret@postgres:5432/simple_bank?sslmode=disable
27+
depends_on:
28+
- postgres
29+
networks:
30+
- bank-network
31+
entrypoint:
32+
[
33+
"/app/wait-for.sh",
34+
"postgres:5432",
35+
"--",
36+
"/app/start.sh"
37+
]
38+
command: [ "/app/main" ]

‎start.sh

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
echo "run db migrations"
6+
/app/migrate -path /app/db/migrations -database "$DB_SOURCE" -verbose up
7+
8+
echo "start the app"
9+
exec "$@"

‎wait-for.sh

+184
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
#!/bin/sh
2+
3+
# The MIT License (MIT)
4+
#
5+
# Copyright (c) 2017 Eficode Oy
6+
#
7+
# Permission is hereby granted, free of charge, to any person obtaining a copy
8+
# of this software and associated documentation files (the "Software"), to deal
9+
# in the Software without restriction, including without limitation the rights
10+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
# copies of the Software, and to permit persons to whom the Software is
12+
# furnished to do so, subject to the following conditions:
13+
#
14+
# The above copyright notice and this permission notice shall be included in all
15+
# copies or substantial portions of the Software.
16+
#
17+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
# SOFTWARE.
24+
25+
set -- "$@" -- "$TIMEOUT" "$QUIET" "$PROTOCOL" "$HOST" "$PORT" "$result"
26+
TIMEOUT=15
27+
QUIET=0
28+
# The protocol to make the request with, either "tcp" or "http"
29+
PROTOCOL="tcp"
30+
31+
echoerr() {
32+
if [ "$QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi
33+
}
34+
35+
usage() {
36+
exitcode="$1"
37+
cat << USAGE >&2
38+
Usage:
39+
$0 host:port|url [-t timeout] [-- command args]
40+
-q | --quiet Do not output any status messages
41+
-t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
42+
-- COMMAND ARGS Execute command with args after the test finishes
43+
USAGE
44+
exit "$exitcode"
45+
}
46+
47+
wait_for() {
48+
case "$PROTOCOL" in
49+
tcp)
50+
if ! command -v nc >/dev/null; then
51+
echoerr 'nc command is missing!'
52+
exit 1
53+
fi
54+
;;
55+
wget)
56+
if ! command -v wget >/dev/null; then
57+
echoerr 'nc command is missing!'
58+
exit 1
59+
fi
60+
;;
61+
esac
62+
63+
while :; do
64+
case "$PROTOCOL" in
65+
tcp)
66+
nc -z "$HOST" "$PORT" > /dev/null 2>&1
67+
;;
68+
http)
69+
wget --timeout=1 -q "$HOST" -O /dev/null > /dev/null 2>&1
70+
;;
71+
*)
72+
echoerr "Unknown protocol '$PROTOCOL'"
73+
exit 1
74+
;;
75+
esac
76+
77+
result=$?
78+
79+
if [ $result -eq 0 ] ; then
80+
if [ $# -gt 7 ] ; then
81+
for result in $(seq $(($# - 7))); do
82+
result=$1
83+
shift
84+
set -- "$@" "$result"
85+
done
86+
87+
TIMEOUT=$2 QUIET=$3 PROTOCOL=$4 HOST=$5 PORT=$6 result=$7
88+
shift 7
89+
exec "$@"
90+
fi
91+
exit 0
92+
fi
93+
94+
if [ "$TIMEOUT" -le 0 ]; then
95+
break
96+
fi
97+
TIMEOUT=$((TIMEOUT - 1))
98+
99+
sleep 1
100+
done
101+
echo "Operation timed out" >&2
102+
exit 1
103+
}
104+
105+
while :; do
106+
case "$1" in
107+
http://*|https://*)
108+
HOST="$1"
109+
PROTOCOL="http"
110+
shift 1
111+
;;
112+
*:* )
113+
HOST=$(printf "%s\n" "$1"| cut -d : -f 1)
114+
PORT=$(printf "%s\n" "$1"| cut -d : -f 2)
115+
shift 1
116+
;;
117+
-q | --quiet)
118+
QUIET=1
119+
shift 1
120+
;;
121+
-q-*)
122+
QUIET=0
123+
echoerr "Unknown option: $1"
124+
usage 1
125+
;;
126+
-q*)
127+
QUIET=1
128+
result=$1
129+
shift 1
130+
set -- -"${result#-q}" "$@"
131+
;;
132+
-t | --timeout)
133+
TIMEOUT="$2"
134+
shift 2
135+
;;
136+
-t*)
137+
TIMEOUT="${1#-t}"
138+
shift 1
139+
;;
140+
--timeout=*)
141+
TIMEOUT="${1#*=}"
142+
shift 1
143+
;;
144+
--)
145+
shift
146+
break
147+
;;
148+
--help)
149+
usage 0
150+
;;
151+
-*)
152+
QUIET=0
153+
echoerr "Unknown option: $1"
154+
usage 1
155+
;;
156+
*)
157+
QUIET=0
158+
echoerr "Unknown argument: $1"
159+
usage 1
160+
;;
161+
esac
162+
done
163+
164+
if ! [ "$TIMEOUT" -ge 0 ] 2>/dev/null; then
165+
echoerr "Error: invalid timeout '$TIMEOUT'"
166+
usage 3
167+
fi
168+
169+
case "$PROTOCOL" in
170+
tcp)
171+
if [ "$HOST" = "" ] || [ "$PORT" = "" ]; then
172+
echoerr "Error: you need to provide a host and port to test."
173+
usage 2
174+
fi
175+
;;
176+
http)
177+
if [ "$HOST" = "" ]; then
178+
echoerr "Error: you need to provide a host to test."
179+
usage 2
180+
fi
181+
;;
182+
esac
183+
184+
wait_for "$@"

0 commit comments

Comments
 (0)
Please sign in to comment.