Skip to content

Commit f79fefb

Browse files
committed
logging ehnacements and cleanup; console message updates for the build command
1 parent 5f9b096 commit f79fefb

File tree

11 files changed

+290
-106
lines changed

11 files changed

+290
-106
lines changed

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,11 @@ Commands:
185185

186186
Global options:
187187

188-
* ` --version` - print the version
189-
* ` --debug` - enable debug logs
188+
* `--version` - print the version
189+
* `--debug` - enable debug logs
190+
* `--log-level` - set the format used by logs ('text' (default), or 'json')
191+
* `--log-format` - set the format used by logs ('text' (default), or 'json')
192+
* `--log` - log file to store logs
190193
* `--host` - Docker host address
191194
* `--tls` - use TLS connecting to Docker
192195
* `--tls-verify` - do TLS verification
@@ -198,7 +201,8 @@ Global options:
198201
* `--http-probe` - enables HTTP probing (disabled by default)
199202
* `--http-probe-cmd` - additional HTTP probe command [zero or more]
200203
* `--http-probe-cmd-file` - file with user defined HTTP probe commands
201-
* `--show-clogs` - show container logs (stdout and stderr)
204+
* `--show-clogs` - show container logs (from the container used to perform dynamic inspection)
205+
* `--show-blogs` - show build logs (when the minified container is built)
202206
* `--remove-file-artifacts` - remove file artifacts when command is done (note: you'll loose autogenerated Seccomp and Apparmor profiles)
203207
* `--tag` - use a custom tag for the generated image (instead of the default: `<original_image_name>.slim`)
204208
* `--entrypoint` - override ENTRYPOINT analyzing image
@@ -304,7 +308,7 @@ It's used to minify the `container-transform` tool. You can get the minified ima
304308

305309
## CURRENT STATE
306310

307-
It works pretty well with the sample Node.js, Python (2 and 3), Ruby, Java and Golang images (built from `examples/apps`). More testing needs to be done to see how it works with other images. Rails/unicorn app images are not fully supported yet (WIP).
311+
It works pretty well with the sample Node.js, Python (2 and 3), Ruby, Java and Golang images (built from `examples/apps`). PHP support is WIP. There's already one PHP example, but more needs to be done to support Apache and Nginx based PHP apps. More testing needs to be done to see how it works with other images. Rails/unicorn app images are not fully supported yet (WIP).
308312

309313
Sample images (built with the standard Ubuntu 14.04 base image):
310314

@@ -324,6 +328,10 @@ Sample Golang application images:
324328
* from ubuntu:14.04: 529MB => 1.86MB
325329
* from golang:alpine: 258MB => 1.55MB
326330

331+
Sample PHP application images:
332+
333+
* from php:7.0-cli: 368MB => 26.6MB
334+
327335
You can also run `docker-slim` in the `info` mode and it'll generate useful image information including a "reverse engineered" Dockerfile.
328336

329337
DockerSlim now also generates Seccomp (usable) and AppArmor (WIP) profiles for your container.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
3+
here="$(dirname "$BASH_SOURCE")"
4+
cd $here
5+
docker-slim --debug build --http-probe --show-clogs --show-blogs my/php-web-app
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
3+
here="$(dirname "$BASH_SOURCE")"
4+
cd $here
5+
docker-slim --verbose build --http-probe --show-blogs my/php-web-app

internal/app/master/builder/image_builder.go

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package builder
22

33
import (
4-
"os"
4+
//"os"
5+
"bytes"
56
"path/filepath"
67

78
"github.com/docker-slim/docker-slim/internal/app/master/config"
@@ -14,49 +15,56 @@ import (
1415

1516
// ImageBuilder creates new container images
1617
type ImageBuilder struct {
17-
RepoName string
18-
ID string
19-
Entrypoint []string
20-
Cmd []string
21-
WorkingDir string
22-
Env []string
23-
ExposedPorts map[docker.Port]struct{}
24-
Volumes map[string]struct{}
25-
OnBuild []string
26-
User string
27-
HasData bool
28-
BuildOptions docker.BuildImageOptions
29-
APIClient *docker.Client
18+
ShowBuildLogs bool
19+
RepoName string
20+
ID string
21+
Entrypoint []string
22+
Cmd []string
23+
WorkingDir string
24+
Env []string
25+
ExposedPorts map[docker.Port]struct{}
26+
Volumes map[string]struct{}
27+
OnBuild []string
28+
User string
29+
HasData bool
30+
BuildOptions docker.BuildImageOptions
31+
APIClient *docker.Client
32+
BuildLog bytes.Buffer
3033
}
3134

3235
// NewImageBuilder creates a new ImageBuilder instances
3336
func NewImageBuilder(client *docker.Client,
3437
imageRepoName string,
3538
imageInfo *docker.Image,
3639
artifactLocation string,
40+
showBuildLogs bool,
3741
imageOverrides map[string]bool,
3842
overrides *config.ContainerOverrides) (*ImageBuilder, error) {
3943
builder := &ImageBuilder{
40-
RepoName: imageRepoName,
41-
ID: imageInfo.ID,
42-
Entrypoint: imageInfo.Config.Entrypoint,
43-
Cmd: imageInfo.Config.Cmd,
44-
WorkingDir: imageInfo.Config.WorkingDir,
45-
Env: imageInfo.Config.Env,
46-
ExposedPorts: imageInfo.Config.ExposedPorts,
47-
Volumes: imageInfo.Config.Volumes,
48-
OnBuild: imageInfo.Config.OnBuild,
49-
User: imageInfo.Config.User,
44+
ShowBuildLogs: showBuildLogs,
45+
RepoName: imageRepoName,
46+
ID: imageInfo.ID,
47+
Entrypoint: imageInfo.Config.Entrypoint,
48+
Cmd: imageInfo.Config.Cmd,
49+
WorkingDir: imageInfo.Config.WorkingDir,
50+
Env: imageInfo.Config.Env,
51+
ExposedPorts: imageInfo.Config.ExposedPorts,
52+
Volumes: imageInfo.Config.Volumes,
53+
OnBuild: imageInfo.Config.OnBuild,
54+
User: imageInfo.Config.User,
5055
BuildOptions: docker.BuildImageOptions{
5156
Name: imageRepoName,
5257
RmTmpContainer: true,
5358
ContextDir: artifactLocation,
5459
Dockerfile: "Dockerfile",
55-
OutputStream: os.Stdout,
60+
//SuppressOutput: true,
61+
//OutputStream: os.Stdout,
5662
},
5763
APIClient: client,
5864
}
5965

66+
builder.BuildOptions.OutputStream = &builder.BuildLog
67+
6068
dataDir := filepath.Join(artifactLocation, "files")
6169
builder.HasData = fsutils.IsDir(dataDir)
6270

internal/app/master/cli.go

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ func init() {
3737
Name: "debug",
3838
Usage: "enable debug logs",
3939
},
40+
cli.BoolFlag{
41+
Name: "verbose",
42+
Usage: "enable info logs",
43+
},
44+
cli.StringFlag{
45+
Name: "log-level",
46+
Value: "warn",
47+
Usage: "set the logging level ('debug', 'info', 'warn' (default), 'error', 'fatal', 'panic')",
48+
},
4049
cli.StringFlag{
4150
Name: "log",
4251
Usage: "log file to store logs",
@@ -74,20 +83,49 @@ func init() {
7483
app.Before = func(ctx *cli.Context) error {
7584
if ctx.GlobalBool("debug") {
7685
log.SetLevel(log.DebugLevel)
86+
} else {
87+
if ctx.GlobalBool("verbose") {
88+
log.SetLevel(log.InfoLevel)
89+
} else {
90+
logLevel := log.WarnLevel
91+
logLevelName := ctx.GlobalString("log-level")
92+
switch logLevelName {
93+
case "debug":
94+
logLevel = log.DebugLevel
95+
case "info":
96+
logLevel = log.InfoLevel
97+
case "warn":
98+
logLevel = log.WarnLevel
99+
case "error":
100+
logLevel = log.ErrorLevel
101+
case "fatal":
102+
logLevel = log.FatalLevel
103+
case "panic":
104+
logLevel = log.PanicLevel
105+
default:
106+
log.Fatalf("unknown log-level %q", logLevelName)
107+
}
108+
109+
log.SetLevel(logLevel)
110+
}
77111
}
112+
78113
if path := ctx.GlobalString("log"); path != "" {
79114
f, err := os.Create(path)
80115
if err != nil {
81116
return err
82117
}
83118
log.SetOutput(f)
84119
}
85-
switch ctx.GlobalString("log-format") {
120+
121+
logFormat := ctx.GlobalString("log-format")
122+
switch logFormat {
86123
case "text":
124+
log.SetFormatter(&log.TextFormatter{DisableColors: true})
87125
case "json":
88126
log.SetFormatter(new(log.JSONFormatter))
89127
default:
90-
log.Fatalf("unknown log-format %q", ctx.GlobalString("log-format"))
128+
log.Fatalf("unknown log-format %q", logFormat)
91129
}
92130
return nil
93131
}
@@ -118,6 +156,12 @@ func init() {
118156
EnvVar: "DSLIM_SHOW_CLOGS",
119157
}
120158

159+
doShowBuildLogsFlag := cli.BoolFlag{
160+
Name: "show-blogs",
161+
Usage: "Show build logs",
162+
EnvVar: "DSLIM_SHOW_BLOGS",
163+
}
164+
121165
doUseEntrypointFlag := cli.StringFlag{
122166
Name: "entrypoint",
123167
Value: "",
@@ -227,6 +271,7 @@ func init() {
227271
doHTTPProbeCmdFlag,
228272
doHTTPProbeCmdFileFlag,
229273
doShowContainerLogsFlag,
274+
doShowBuildLogsFlag,
230275
cli.BoolFlag{
231276
Name: "remove-file-artifacts, r",
232277
Usage: "remove file artifacts when command is done",
@@ -281,6 +326,7 @@ func init() {
281326
}
282327

283328
doShowContainerLogs := ctx.Bool("show-clogs")
329+
doShowBuildLogs := ctx.Bool("show-blogs")
284330
doTag := ctx.String("tag")
285331

286332
doImageOverrides := ctx.String("image-overrides")
@@ -322,12 +368,18 @@ func init() {
322368
commands.OnBuild(ctx.GlobalBool("debug"),
323369
statePath,
324370
clientConfig,
325-
imageRef, doTag,
326-
doHTTPProbe, httpProbeCmds,
327-
doRmFileArtifacts, doShowContainerLogs,
371+
imageRef,
372+
doTag,
373+
doHTTPProbe,
374+
httpProbeCmds,
375+
doRmFileArtifacts,
376+
doShowContainerLogs,
377+
doShowBuildLogs,
328378
parseImageOverrides(doImageOverrides),
329379
overrides,
330-
volumeMounts, excludePaths, includePaths,
380+
volumeMounts,
381+
excludePaths,
382+
includePaths,
331383
confinueAfter)
332384

333385
return nil

0 commit comments

Comments
 (0)