Skip to content

Commit

Permalink
Always create working directory when using compat API
Browse files Browse the repository at this point in the history
Docker/Moby always create the working directory, and some tools
rely on that behavior (example, woodpecker/drone).

Fixes #11842

Signed-off-by: Michael Scherer <[email protected]>
  • Loading branch information
mscherer committed Nov 12, 2021
1 parent 92da868 commit b1c0023
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/api/handlers/compat/containers_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ func CreateContainer(w http.ResponseWriter, r *http.Request) {
utils.Error(w, "Something went wrong.", http.StatusInternalServerError, errors.Wrap(err, "fill out specgen"))
return
}
// moby always create the working directory
sg.CreateWorkingDir = true

ic := abi.ContainerEngine{Libpod: runtime}
report, err := ic.ContainerCreate(r.Context(), sg)
Expand Down
3 changes: 3 additions & 0 deletions pkg/specgen/generate/container_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,9 @@ func createContainerOptions(ctx context.Context, rt *libpod.Runtime, s *specgen.
if s.WorkDir == "" {
s.WorkDir = "/"
}
if s.CreateWorkingDir {
options = append(options, libpod.WithCreateWorkingDir())
}
if s.StopSignal != nil {
options = append(options, libpod.WithStopSignal(*s.StopSignal))
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/specgen/specgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@ type ContainerStorageConfig struct {
// If unset, the default, /, will be used.
// Optional.
WorkDir string `json:"work_dir,omitempty"`
// Create the working directory if it doesn't exist.
// If unset, it doesn't create it.
// Optional.
CreateWorkingDir bool `json:"create_working_dir,omitempty"`
// StorageOpts is the container's storage options
// Optional.
StorageOpts map[string]string `json:"storage_opts,omitempty"`
Expand Down
14 changes: 14 additions & 0 deletions test/python/docker/compat/test_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,17 @@ def test_mount_preexisting_dir(self):
ctr.start()
ret, out = ctr.exec_run(["stat", "-c", "%u:%g", "/workspace"])
self.assertEqual(out.rstrip(), b'1042:1043', "UID/GID set in dockerfile")


def test_non_existant_workdir(self):
dockerfile = (B'FROM quay.io/libpod/alpine:latest\n'
B'USER root\n'
B'WORKDIR /workspace/scratch\n'
B'RUN touch test')
img: Image
img, out = self.client.images.build(fileobj=io.BytesIO(dockerfile))
ctr: Container = self.client.containers.create(image=img.id, detach=True, command="top",
volumes=["test_non_existant_workdir:/workspace"])
ctr.start()
ret, out = ctr.exec_run(["stat", "/workspace/scratch/test"])
self.assertEqual(ret, 0, "Working directory created if it doesn't exist")

0 comments on commit b1c0023

Please sign in to comment.