Skip to content

Commit 190efbb

Browse files
committed
docker api release
1 parent 8fe484f commit 190efbb

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed

.env

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
PROJECT_PATH=github.com/vanetraj/go-api-quickstart
1+
GO_VERSION=latest
2+
PROJECT_PATH=github.com/vanetraj/go-api-quickstart
3+
PROJECT_NAME=go-api-quickstart

Dockerfile

+46
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
ARG GO_VERSION=latest
22

3+
#
4+
# dev image
5+
#
36
FROM golang:${GO_VERSION} AS dev
47

58
# Go dependency management tool
@@ -8,6 +11,49 @@ RUN go get -u github.com/golang/dep/cmd/dep
811
# Golang live reload and task runner
912
RUN go get -u github.com/oxequa/realize
1013

14+
15+
#
16+
# builder image
17+
#
18+
FROM golang:${GO_VERSION} as builder
19+
20+
WORKDIR /go/src/project
21+
# Copy the current code into our workdir
22+
COPY . .
23+
24+
# godep - dependency manager tool,
25+
RUN go get -u github.com/golang/dep/cmd/dep
26+
# Create a dep project, and run `ensure`, which will pull in all
27+
# of the dependencies within this directory.
28+
RUN dep ensure -vendor-only
29+
# Build the binary, with a few flags which will allow
30+
# us to run this binary in Alpine.
31+
RUN CGO_ENABLED=0 GOOS=linux go build -o app -a -installsuffix cgo .
32+
33+
#
34+
# release image
35+
#
36+
FROM alpine:latest AS release
37+
38+
# Security related package, good to have.
39+
RUN apk --no-cache add ca-certificates
40+
41+
# Same as before, create a directory for our app.
42+
RUN mkdir /app
43+
WORKDIR /app
44+
45+
# Here, instead of copying the binary from our host machine,
46+
# we pull the binary from the container named `builder`, within
47+
# this build context.
48+
COPY --from=builder /go/src/project/app .
49+
50+
# Run the binary as per usual! This time with a binary build in a
51+
# separate container, with all of the correct dependencies and
52+
# run time libraries.
53+
CMD ["./app"]
54+
55+
56+
1157
LABEL version="1.0" \
1258
maintainer="[email protected]"
1359

Makefile

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
include .env
2+
3+
build:
4+
# docker build -t $(PROJECT_NAME) --target release .
5+
docker-compose build --force-rm api_release
6+
7+
run:
8+
# docker run $(PROJECT_NAME)
9+
docker-compose run api_release

docker-compose.yml

+12
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,23 @@ services:
44
build:
55
context: .
66
target: dev
7+
args:
8+
- GO_VERSION:${GO_VERSION}
79
container_name: api
810
volumes:
911
- .:/go/src/${PROJECT_PATH}
1012
working_dir: /go/src/${PROJECT_PATH}
1113
command: realize start
1214
entrypoint: ./entrypoint.sh
15+
ports:
16+
- 8080:8080
17+
18+
api_release:
19+
build:
20+
context: .
21+
target: release
22+
args:
23+
- GO_VERSION:${GO_VERSION}
24+
image: ${PROJECT_NAME}
1325
ports:
1426
- 8080:8080

0 commit comments

Comments
 (0)