Skip to content

Commit 6801b69

Browse files
committed
Add container information to .containerenv
We have been asked to leak some container information and image information into the container to be used by certain tools. (Toolbox and others) Signed-off-by: Daniel J Walsh <[email protected]>
1 parent af10f8c commit 6801b69

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

run_linux.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,16 +216,23 @@ func (b *Builder) Run(command []string, options RunOptions) error {
216216
}
217217
// Empty file, so no need to recreate if it exists
218218
if _, ok := bindFiles["/run/.containerenv"]; !ok {
219-
// Empty string for now, but we may consider populating this later
220219
containerenvPath := filepath.Join(path, "/run/.containerenv")
221220
if err = os.MkdirAll(filepath.Dir(containerenvPath), 0755); err != nil {
222221
return err
223222
}
224-
emptyFile, err := os.Create(containerenvPath)
225-
if err != nil {
223+
224+
// Populate the .containerenv with container information
225+
containerenv := fmt.Sprintf(`\
226+
engine="buildah-%s"
227+
name=%q
228+
id=%q
229+
image=%q
230+
imageid=%q
231+
`, Version, b.Container, b.ContainerID, b.FromImage, b.FromImageID)
232+
233+
if err = ioutils.AtomicWriteFile(containerenvPath, []byte(containerenv), 0755); err != nil {
226234
return err
227235
}
228-
emptyFile.Close()
229236
if err := label.Relabel(containerenvPath, b.MountLabel, false); err != nil {
230237
return err
231238
}

tests/run.bats

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,15 @@ function configure_and_check_user() {
391391
# test a standard mount to /run/.containerenv
392392
run_buildah run $cid ls -1 /run/.containerenv
393393
expect_output --substring "/run/.containerenv"
394+
395+
run_buildah run $cid sh -c '. /run/.containerenv; echo $engine'
396+
expect_output --substring "buildah"
397+
398+
run_buildah run $cid sh -c '. /run/.containerenv; echo $name'
399+
expect_output "alpine-working-container"
400+
401+
run_buildah run $cid sh -c '. /run/.containerenv; echo $image'
402+
expect_output --substring "alpine:latest"
394403
}
395404

396405
@test "run-device" {

0 commit comments

Comments
 (0)