mirror of
https://github.com/containers/podman.git
synced 2025-08-06 19:44:14 +08:00
podman-remote build should handle -f option properly
podman-remote build has to handle multiple different locations for the Containerfile. Currently this works in local mode but not when using podman-remote. Fixes: https://github.com/containers/podman/issues/9871 Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
This commit is contained in:

committed by
Matthew Heon

parent
6beae86f01
commit
2afb5eeab6
@ -139,6 +139,31 @@ func BuildImage(w http.ResponseWriter, r *http.Request) {
|
||||
addCaps = m
|
||||
}
|
||||
|
||||
// convert addcaps formats
|
||||
containerFiles := []string{}
|
||||
if _, found := r.URL.Query()["dockerfile"]; found {
|
||||
var m = []string{}
|
||||
if err := json.Unmarshal([]byte(query.Dockerfile), &m); err != nil {
|
||||
utils.BadRequest(w, "dockerfile", query.Dockerfile, err)
|
||||
return
|
||||
}
|
||||
containerFiles = m
|
||||
} else {
|
||||
containerFiles = []string{"Dockerfile"}
|
||||
if utils.IsLibpodRequest(r) {
|
||||
containerFiles = []string{"Containerfile"}
|
||||
if _, err = os.Stat(filepath.Join(contextDirectory, "Containerfile")); err != nil {
|
||||
if _, err1 := os.Stat(filepath.Join(contextDirectory, "Dockerfile")); err1 == nil {
|
||||
containerFiles = []string{"Dockerfile"}
|
||||
} else {
|
||||
utils.BadRequest(w, "dockerfile", query.Dockerfile, err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
containerFiles = []string{"Dockerfile"}
|
||||
}
|
||||
}
|
||||
|
||||
addhosts := []string{}
|
||||
if _, found := r.URL.Query()["extrahosts"]; found {
|
||||
if err := json.Unmarshal([]byte(query.AddHosts), &addhosts); err != nil {
|
||||
@ -470,7 +495,7 @@ func BuildImage(w http.ResponseWriter, r *http.Request) {
|
||||
runCtx, cancel := context.WithCancel(context.Background())
|
||||
go func() {
|
||||
defer cancel()
|
||||
imageID, _, err = runtime.Build(r.Context(), buildOptions, query.Dockerfile)
|
||||
imageID, _, err = runtime.Build(r.Context(), buildOptions, containerFiles...)
|
||||
if err == nil {
|
||||
success = true
|
||||
} else {
|
||||
|
@ -282,10 +282,6 @@ func Build(ctx context.Context, containerFiles []string, options entities.BuildO
|
||||
stdout = options.Out
|
||||
}
|
||||
|
||||
entries := make([]string, len(containerFiles))
|
||||
copy(entries, containerFiles)
|
||||
entries = append(entries, options.ContextDirectory)
|
||||
|
||||
excludes := options.Excludes
|
||||
if len(excludes) == 0 {
|
||||
excludes, err = parseDockerignore(options.ContextDirectory)
|
||||
@ -294,9 +290,50 @@ func Build(ctx context.Context, containerFiles []string, options entities.BuildO
|
||||
}
|
||||
}
|
||||
|
||||
tarfile, err := nTar(excludes, entries...)
|
||||
contextDir, err := filepath.Abs(options.ContextDirectory)
|
||||
if err != nil {
|
||||
logrus.Errorf("cannot tar container entries %v error: %v", entries, err)
|
||||
logrus.Errorf("cannot find absolute path of %v: %v", options.ContextDirectory, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tarContent := []string{options.ContextDirectory}
|
||||
newContainerFiles := []string{}
|
||||
for _, c := range containerFiles {
|
||||
containerfile, err := filepath.Abs(c)
|
||||
if err != nil {
|
||||
logrus.Errorf("cannot find absolute path of %v: %v", c, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Check if Containerfile is in the context directory, if so truncate the contextdirectory off path
|
||||
// Do NOT add to tarfile
|
||||
if strings.HasPrefix(containerfile, contextDir+string(filepath.Separator)) {
|
||||
containerfile = strings.TrimPrefix(containerfile, contextDir+string(filepath.Separator))
|
||||
} else {
|
||||
// If Containerfile does not exists assume it is in context directory, do Not add to tarfile
|
||||
if _, err := os.Lstat(containerfile); err != nil {
|
||||
if !os.IsNotExist(err) {
|
||||
return nil, err
|
||||
}
|
||||
containerfile = c
|
||||
} else {
|
||||
// If Containerfile does exists but is not in context directory add it to the tarfile
|
||||
tarContent = append(tarContent, containerfile)
|
||||
}
|
||||
}
|
||||
newContainerFiles = append(newContainerFiles, containerfile)
|
||||
}
|
||||
if len(newContainerFiles) > 0 {
|
||||
cFileJSON, err := json.Marshal(newContainerFiles)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
params.Set("dockerfile", string(cFileJSON))
|
||||
}
|
||||
|
||||
tarfile, err := nTar(excludes, tarContent...)
|
||||
if err != nil {
|
||||
logrus.Errorf("cannot tar container entries %v error: %v", tarContent, err)
|
||||
return nil, err
|
||||
}
|
||||
defer func() {
|
||||
@ -305,23 +342,6 @@ func Build(ctx context.Context, containerFiles []string, options entities.BuildO
|
||||
}
|
||||
}()
|
||||
|
||||
containerFile, err := filepath.Abs(entries[0])
|
||||
if err != nil {
|
||||
logrus.Errorf("cannot find absolute path of %v: %v", entries[0], err)
|
||||
return nil, err
|
||||
}
|
||||
contextDir, err := filepath.Abs(entries[1])
|
||||
if err != nil {
|
||||
logrus.Errorf("cannot find absolute path of %v: %v", entries[1], err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if strings.HasPrefix(containerFile, contextDir+string(filepath.Separator)) {
|
||||
containerFile = strings.TrimPrefix(containerFile, contextDir+string(filepath.Separator))
|
||||
}
|
||||
|
||||
params.Set("dockerfile", containerFile)
|
||||
|
||||
conn, err := bindings.GetClient(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -794,6 +794,32 @@ EOF
|
||||
run_podman rmi -f build_test
|
||||
}
|
||||
|
||||
@test "podman build -f test " {
|
||||
tmpdir=$PODMAN_TMPDIR/build-test
|
||||
subdir=$tmpdir/subdir
|
||||
mkdir -p $subdir
|
||||
|
||||
containerfile1=$tmpdir/Containerfile1
|
||||
cat >$containerfile1 <<EOF
|
||||
FROM scratch
|
||||
copy . /tmp
|
||||
EOF
|
||||
containerfile2=$PODMAN_TMPDIR/Containerfile2
|
||||
cat >$containerfile2 <<EOF
|
||||
FROM $IMAGE
|
||||
EOF
|
||||
run_podman build -t build_test -f Containerfile1 $tmpdir
|
||||
run_podman 125 build -t build_test -f Containerfile2 $tmpdir
|
||||
is "$output" ".*Containerfile2: no such file or directory" "Containerfile2 should not exist"
|
||||
run_podman build -t build_test -f $containerfile1 $tmpdir
|
||||
run_podman build -t build_test -f $containerfile2 $tmpdir
|
||||
run_podman build -t build_test -f $containerfile1
|
||||
run_podman build -t build_test -f $containerfile2
|
||||
run_podman build -t build_test -f $containerfile1 -f $containerfile2 $tmpdir
|
||||
is "$output" ".*$IMAGE" "Containerfile2 is also passed to server"
|
||||
run_podman rmi -f build_test
|
||||
}
|
||||
|
||||
function teardown() {
|
||||
# A timeout or other error in 'build' can leave behind stale images
|
||||
# that podman can't even see and which will cascade into subsequent
|
||||
|
Reference in New Issue
Block a user