Skip to content

Commit

Permalink
Allow '/' to prefix container names to match Docker
Browse files Browse the repository at this point in the history
Fixes: containers#16663

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
  • Loading branch information
rhatdan committed Dec 12, 2022
1 parent 0037bff commit 853c5c9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
3 changes: 2 additions & 1 deletion libpod/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net"
"os"
"path/filepath"
"strings"
"syscall"

"github.com/containers/buildah/pkg/parse"
Expand Down Expand Up @@ -777,7 +778,7 @@ func WithName(name string) CtrCreateOption {
}

// Check the name against a regex
if !define.NameRegex.MatchString(name) {
if !define.NameRegex.MatchString(strings.TrimPrefix(name, "/")) {
return define.RegexError
}

Expand Down
2 changes: 1 addition & 1 deletion libpod/runtime_ctr.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (r *Runtime) RenameContainer(ctx context.Context, ctr *Container, newName s
return nil, err
}

if newName == "" || !define.NameRegex.MatchString(newName) {
if newName == "" && !define.NameRegex.MatchString(strings.TrimPrefix(newName, "/")) {
return nil, define.RegexError
}

Expand Down
14 changes: 12 additions & 2 deletions test/system/030-run.bats
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,15 @@ echo $rand | 0 | $rand
run_podman 1 image exists $NONLOCAL_IMAGE

# Run a container, without --rm; this should block subsequent --rmi
run_podman run --name keepme $NONLOCAL_IMAGE /bin/true
run_podman run --name /keepme $NONLOCAL_IMAGE /bin/true
run_podman image exists $NONLOCAL_IMAGE

# Now try running with --rmi : it should succeed, but not remove the image
run_podman run --rmi --rm $NONLOCAL_IMAGE /bin/true
run_podman image exists $NONLOCAL_IMAGE

# Remove the stray container, and run one more time with --rmi.
run_podman rm keepme
run_podman rm /keepme
run_podman run --rmi --rm $NONLOCAL_IMAGE /bin/true
run_podman 1 image exists $NONLOCAL_IMAGE
}
Expand Down Expand Up @@ -951,4 +951,14 @@ $IMAGE--c_ok" \
run_podman stop -t 0 $cid
}

@test "podman run bad --name" {
randomname=$(random_string 30)
run_podman 125 create --name "$randomname/bad" $IMAGE
run_podman create --name "/$randomname" $IMAGE
run_podman ps -a --filter name="/$randomname" --format '{{ .Names }}'
is $output "/$randomname" "Should be able to find container by name"
run_podman rm "/$randomname"
run_podman 125 create --name "$randomname/" $IMAGE
}

# vim: filetype=sh

0 comments on commit 853c5c9

Please sign in to comment.