Add nocreate option for named volumes

Add a per-volume 'nocreate' option that prevents automatic creation of
named volumes when they don't exist. When specified, Podman will fail
if the volume is not found instead of creating it automatically.

Usage: -v myvolume:/data:nocreate
       --mount type=volume,src=myvolume,dst=/data,nocreate

See: #27862
Signed-off-by: Ygal Blum <ygal.blum@gmail.com>
This commit is contained in:
Ygal Blum
2026-01-06 12:01:43 -05:00
parent d3a81e3e17
commit 64ec31ac00
9 changed files with 185 additions and 45 deletions

View File

@@ -1164,4 +1164,94 @@ RUN chmod 755 /test1 /test2 /test3`, ALPINE)
output = session.OutputToString()
Expect(output).ToNot(ContainSubstring("noatime"))
})
It("podman run -v with nocreate option fails when volume doesn't exist", func() {
volName := "testvol-nocreate-nonexistent"
// Ensure volume doesn't exist
session := podmanTest.Podman([]string{"volume", "rm", "-f", volName})
session.WaitWithDefaultTimeout()
// Run with nocreate option should error
session = podmanTest.Podman([]string{"run", "--rm", "-v", fmt.Sprintf("%s:/mnt:nocreate", volName), ALPINE, "true"})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitWithError(125, fmt.Sprintf("volume %s does not exist", volName)))
})
It("podman run -v with nocreate option succeeds when volume exists", func() {
volName := "testvol-nocreate-exists"
// Create volume first
session := podmanTest.Podman([]string{"volume", "create", volName})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
// Run with nocreate option should succeed since volume exists
session = podmanTest.Podman([]string{"run", "--rm", "-v", fmt.Sprintf("%s:/mnt:nocreate", volName), ALPINE, "touch", "/mnt/testfile"})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
// Cleanup
session = podmanTest.Podman([]string{"volume", "rm", volName})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
})
It("podman run --mount type=volume with nocreate option fails when volume doesn't exist", func() {
volName := "testvol-mount-nocreate-nonexistent"
// Ensure volume doesn't exist
session := podmanTest.Podman([]string{"volume", "rm", "-f", volName})
session.WaitWithDefaultTimeout()
// Run with nocreate option should error
session = podmanTest.Podman([]string{"run", "--rm", "--mount", fmt.Sprintf("type=volume,src=%s,dst=/mnt,nocreate", volName), ALPINE, "true"})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitWithError(125, fmt.Sprintf("volume %s does not exist", volName)))
})
It("podman run --mount type=volume with nocreate option succeeds when volume exists", func() {
volName := "testvol-mount-nocreate-exists"
// Create volume first
session := podmanTest.Podman([]string{"volume", "create", volName})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
// Run with nocreate option should succeed since volume exists
session = podmanTest.Podman([]string{"run", "--rm", "--mount", fmt.Sprintf("type=volume,src=%s,dst=/mnt,nocreate", volName), ALPINE, "touch", "/mnt/testfile"})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
// Cleanup
session = podmanTest.Podman([]string{"volume", "rm", volName})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
})
It("podman run -v with nocreate combined with other options", func() {
volName := "testvol-nocreate-combo"
// Create volume first
session := podmanTest.Podman([]string{"volume", "create", volName})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
// Run with nocreate and ro options should succeed
session = podmanTest.Podman([]string{"run", "--rm", "-v", fmt.Sprintf("%s:/mnt:ro,nocreate", volName), ALPINE, "ls", "/mnt"})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
// Cleanup
session = podmanTest.Podman([]string{"volume", "rm", volName})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
})
It("podman create -v with nocreate option fails when volume doesn't exist", func() {
volName := "testvol-create-nocreate"
// Ensure volume doesn't exist
session := podmanTest.Podman([]string{"volume", "rm", "-f", volName})
session.WaitWithDefaultTimeout()
// Create with nocreate option should error
session = podmanTest.Podman([]string{"create", "-v", fmt.Sprintf("%s:/mnt:nocreate", volName), ALPINE, "true"})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitWithError(125, fmt.Sprintf("volume %s does not exist", volName)))
})
})