Skip to content

Commit 8defd1c

Browse files
committed
new copy command; scratch image
1 parent 0583c1b commit 8defd1c

File tree

3 files changed

+54
-13
lines changed

3 files changed

+54
-13
lines changed

Dockerfile

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,28 @@
11
# syntax = docker/dockerfile:experimental
22

33
FROM --platform=${BUILDPLATFORM} golang:1.16-alpine as builder
4-
54
# passed by buildkit
65
ARG TARGETOS
76
ARG TARGETARCH
8-
97
# add CA certificates and TZ for local time
108
RUN apk --update add ca-certificates make git
11-
129
# Create and change to the app directory.
1310
RUN mkdir -p /go/src/app
1411
WORKDIR /go/src/app
15-
1612
# Retrieve application dependencies.
1713
# This allows the container build to reuse cached dependencies.
1814
# Expecting to copy go.mod and if present go.sum.
1915
COPY go.mod .
2016
COPY go.sum .
2117
RUN --mount=type=cache,target=/go/mod go mod download
22-
2318
# Copy local code to the container image.
2419
COPY . .
25-
2620
# Build the binary.
2721
RUN --mount=type=cache,target=/root/.cache/go-build TARGETOS=${TARGETOS} TARGETARCH=${TARGETARCH} make
2822

2923
# final image
30-
# keep it FROM alpine - need to copy secrets-init to target container
31-
FROM --platform=${TARGETPLATFORM} alpine:3.13
32-
33-
COPY --from=builder /go/src/app/.bin/secrets-init /usr/local/bin/secrets-init
34-
35-
CMD ["secrets-init", "--version"]
24+
FROM scratch
25+
# copy the binary to the production image from the builder stage.
26+
COPY --from=builder /go/src/app/.bin/secrets-init /secrets-init
27+
ENTRYPOINT ["/secrets-init"]
28+
CMD ["--version"]

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ PLATFORMS = darwin linux
1414
ARCHITECTURES = amd64 arm64
1515
TARGETOS ?= $(GOOS)
1616
TARGETARCH ?= $(GOARCH)
17-
LDFLAGS_VERSION = -X main.Version=$(VERSION) -X main.BuildDate=$(DATE) -X main.GitCommit=$(COMMIT) -X main.GitBranch=$(BRANCH)
17+
LDFLAGS_VERSION = -s -w -X main.Version=$(VERSION) -X main.BuildDate=$(DATE) -X main.GitCommit=$(COMMIT) -X main.GitBranch=$(BRANCH)
1818

1919
DOCKER = docker
2020
GO = go

main.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package main
33
import (
44
"context"
55
"fmt"
6+
"io"
67
"os"
78
"os/exec"
89
"os/signal"
10+
"path/filepath"
911
"runtime"
1012
"secrets-init/pkg/secrets"
1113
"secrets-init/pkg/secrets/aws"
@@ -41,6 +43,15 @@ func main() {
4143
Value: "aws",
4244
},
4345
},
46+
Commands: []*cli.Command{
47+
{
48+
Name: "copy",
49+
Aliases: []string{"cp"},
50+
Usage: "copy itself to a destination folder",
51+
ArgsUsage: "destination",
52+
Action: copyCmd,
53+
},
54+
},
4455
Name: "secrets-init",
4556
Usage: "enrich environment variables with secrets from secret manager",
4657
Action: mainCmd,
@@ -61,6 +72,43 @@ func main() {
6172
}
6273
}
6374

75+
func copyCmd(c *cli.Context) error {
76+
if c.Args().Len() != 1 {
77+
return fmt.Errorf("must specify copy destination")
78+
}
79+
// full path of current executable
80+
src := os.Args[0]
81+
// destination path
82+
dest := filepath.Join(c.Args().First(), filepath.Base(src))
83+
// copy file with current file mode flags
84+
sourceFileStat, err := os.Stat(src)
85+
if err != nil {
86+
return err
87+
}
88+
if !sourceFileStat.Mode().IsRegular() {
89+
return fmt.Errorf("%s is not a regular file", src)
90+
}
91+
source, err := os.Open(src)
92+
if err != nil {
93+
return err
94+
}
95+
srcInfo, err := source.Stat()
96+
if err != nil {
97+
return err
98+
}
99+
defer func() { _ = source.Close() }()
100+
destination, err := os.Create(dest)
101+
if err != nil {
102+
return err
103+
}
104+
defer func() { _ = destination.Close() }()
105+
_, err = io.Copy(destination, source)
106+
if err != nil {
107+
return err
108+
}
109+
return destination.Chmod(srcInfo.Mode())
110+
}
111+
64112
func mainCmd(c *cli.Context) error {
65113
// Routine to reap zombies (it's the job of init)
66114
ctx, cancel := context.WithCancel(context.Background())

0 commit comments

Comments
 (0)