Skip to content

Commit 102ac5a

Browse files
committed
Add air-go docker live reload and local mongo
Mongo creates collections automatically if not found User-service has been set to env:dev in compose.yaml Air allows live reloads in Go while in docker by binding the local src code to the container For the node apps, use `docker compose watch`
1 parent 18841d5 commit 102ac5a

File tree

6 files changed

+174
-25
lines changed

6 files changed

+174
-25
lines changed

backend/.air.toml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
root = "."
2+
testdata_dir = "testdata"
3+
tmp_dir = "tmp"
4+
5+
[build]
6+
args_bin = []
7+
bin = "tmp/main.exe"
8+
cmd = "go build -o ./tmp/main.exe ."
9+
delay = 1000
10+
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
11+
exclude_file = []
12+
exclude_regex = ["_test.go"]
13+
exclude_unchanged = false
14+
follow_symlink = false
15+
full_bin = ""
16+
include_dir = []
17+
include_ext = ["go", "tpl", "tmpl", "html"]
18+
include_file = []
19+
kill_delay = "0s"
20+
log = "build-errors.log"
21+
poll = true # NOTE: set to true to work inside docker
22+
poll_interval = 0
23+
post_cmd = []
24+
pre_cmd = []
25+
rerun = false
26+
rerun_delay = 500
27+
send_interrupt = false
28+
stop_on_error = false
29+
30+
[color]
31+
app = ""
32+
build = "yellow"
33+
main = "magenta"
34+
runner = "green"
35+
watcher = "cyan"
36+
37+
[log]
38+
main_only = false
39+
time = false
40+
41+
[misc]
42+
clean_on_exit = false
43+
44+
[proxy]
45+
app_port = 0
46+
enabled = false
47+
proxy_port = 0
48+
49+
[screen]
50+
clear_on_rebuild = false
51+
keep_scroll = true

backend/.dockerignore

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Include any files or directories that you don't want to be copied to your
2+
# container here (e.g., local build artifacts, temporary files, etc.).
3+
#
4+
# For more help, visit the .dockerignore file reference guide at
5+
# https://docs.docker.com/go/build-context-dockerignore/
6+
7+
**/.DS_Store
8+
**/.classpath
9+
**/.dockerignore
10+
**/.env
11+
**/.git
12+
**/.gitignore
13+
**/.project
14+
**/.settings
15+
**/.toolstarget
16+
**/.vs
17+
**/.vscode
18+
**/*.*proj.user
19+
**/*.dbmdl
20+
**/*.jfm
21+
**/bin
22+
**/charts
23+
**/docker-compose*
24+
**/compose.y*ml
25+
**/Dockerfile*
26+
**/node_modules
27+
**/npm-debug.log
28+
**/obj
29+
**/secrets.dev.yaml
30+
**/values.dev.yaml
31+
LICENSE
32+
README.md

backend/Dockerfile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ COPY --from=build /bin/server /bin/server
4141

4242
EXPOSE 9090
4343

44-
# Run
45-
CMD ["/bin/server"]
44+
RUN go install github.com/air-verse/air@latest
45+
#COPY go.mod go.sum ./
46+
#RUN #go mod download
47+
48+
ENTRYPOINT ["air", "-c", ".air.toml"]
4649

4750

4851
################################################################################
@@ -56,7 +59,7 @@ CMD ["/bin/server"]
5659
# most recent version of that image when you build your Dockerfile. If
5760
# reproducability is important, consider using a versioned tag
5861
# (e.g., alpine:3.17.2) or SHA (e.g., alpine@sha256:c41ab5c992deb4fe7e5da09f67a8804a46bd0592bfdf0b1847dde0e0889d2bff).
59-
FROM alpine:latest AS final
62+
FROM alpine:latest AS prod
6063

6164
# Install any runtime dependencies that are needed to run your application.
6265
# Leverage a cache mount to /var/cache/apk/ to speed up subsequent builds.

compose.yaml

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ x-common-environment:
22
environment:
33
- ENV=${ENV:-dev}
44

5+
# NOTE: Ports do not need to be exposed if not needed to expose outside the network
6+
57
services:
68
peerprep-dev:
7-
# profiles:
8-
# - [ dev ]
99
depends_on:
1010
- user-service-dev
1111
- backend-dev
@@ -20,8 +20,6 @@ services:
2020
- ./peerprep:/frontend
2121
ports:
2222
- "${PEERPREP_PORT:-3000}:3000"
23-
# extra_hosts:
24-
# - "host.docker.internal:host-gateway"
2523
networks:
2624
- backend
2725
- user-service
@@ -42,18 +40,20 @@ services:
4240
build:
4341
context: user-service
4442
target: dev
43+
depends_on:
44+
- mongo-dev
4545
env_file:
4646
- user-service/.env
4747
environment:
48-
- ENV=${ENV:-PROD}
48+
- ENV=${ENV:-DEV}
4949
volumes:
5050
- ./user-service:/user-service
5151
ports:
5252
- "${USER_SERVICE_PORT:-3001}:3001"
5353
networks:
5454
- user-service
55-
# extra_hosts:
56-
# - "host.docker.internal:host-gateway"
55+
- user-mongo
56+
##### No curl on distroless node
5757
# healthcheck:
5858
# test: [ "CMD", "curl", "-f", "http://localhost:3001/" ]
5959
restart: on-failure
@@ -83,7 +83,6 @@ services:
8383
- backend/.env
8484
environment:
8585
- ENV=${ENV:-dev}
86-
8786
volumes:
8887
- logs:/log
8988
- ./backend:/src
@@ -101,26 +100,36 @@ services:
101100
target: backend/app
102101

103102

104-
# backend-prod:
105-
# extends:
106-
# service: backend-prod
107-
# profiles:
108-
# - [ prod ]
109-
# environment:
110-
# - ENV=prod
111-
# build:
112-
# target: final
103+
# backend-prod:
104+
# extends:
105+
# service: backend-prod
106+
# profiles:
107+
# - [ prod ]
108+
# environment:
109+
# - ENV=prod
110+
# build:
111+
# target: final
112+
113+
mongo-dev:
114+
image: "mongo:latest"
115+
restart: on-failure
116+
volumes:
117+
- mongo-data:/data/db
118+
ports:
119+
- "27017:27017"
120+
- "${usermongoport:-27017}:27017"
121+
networks:
122+
- user-mongo
113123

114-
# mongo:
115-
# image: "mongo:latest"
116-
# ports:
117-
# - "27017:27017"
118124

119125
networks:
120126
backend:
121127
driver: bridge
122128
user-service:
123129
driver: bridge
130+
user-mongo:
131+
driver: bridge
124132

125133
volumes:
126-
logs:
134+
logs:
135+
mongo-data:

peerprep/.dockerignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Include any files or directories that you don't want to be copied to your
2+
# container here (e.g., local build artifacts, temporary files, etc.).
3+
#
4+
# For more help, visit the .dockerignore file reference guide at
5+
# https://docs.docker.com/go/build-context-dockerignore/
6+
7+
**/.classpath
8+
**/.dockerignore
9+
**/.env
10+
**/.git
11+
**/.gitignore
12+
**/.project
13+
**/.settings
14+
**/.toolstarget
15+
**/.vs
16+
**/.vscode
17+
**/.next
18+
**/.cache
19+
**/*.*proj.user
20+
**/*.dbmdl
21+
**/*.jfm
22+
**/charts
23+
**/docker-compose*
24+
**/compose.y*ml
25+
**/Dockerfile*
26+
**/node_modules
27+
**/npm-debug.log
28+
**/obj
29+
**/secrets.dev.yaml
30+
**/values.dev.yaml
31+
**/build
32+
**/dist
33+
LICENSE
34+
README.md

production.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# https://docs.docker.com/reference/compose-file/merge/#replace-value
2+
# run using `docker compose up -f compose.yaml -f production.yml`
3+
4+
x-common-environment:
5+
environment:
6+
- ENV=${ENV:-PROD}
7+
8+
services:
9+
peerprep-dev:
10+
build:
11+
target: prod
12+
13+
user-service-dev:
14+
depends_on: !reset [ ]
15+
build:
16+
target: prod
17+
18+
backend-dev:
19+
build:
20+
target: prod

0 commit comments

Comments
 (0)