Skip to content

Commit b34858e

Browse files
committed
fix: clean up port-reserver.pid and the parent dir when the container is removed
In the current implementation, when a container is started with the -p option, a `sleep infinity` process is launched to reserve host ports. The PID of this process is stored in `/run/nerdctl/<namespace>/<container ID>/port-reserver.pid`. When the container is removed, the following file and directory are expected to be cleaned up. - /run/nerdctl/<namespace>/<container ID>/port-reserver.pid - /run/nerdctl/<namespace>/<container ID>/ However, currently they are not removed as shown below: ``` $ sudo nerdctl run -d --name nginx -p 81:80 nginx ca1552d394cd8efcc5adcff9434deb4f3ead569d168f6e31bea1ae965db8b919 $ ls /run/nerdctl/default/ca1552d394cd8efcc5adcff9434deb4f3ead569d168f6e31bea1ae965db8b919 port-reserver.pid $ sudo nerdctl rm -f nginx nginx $ ls /run/nerdctl/default/ca1552d394cd8efcc5adcff9434deb4f3ead569d168f6e31bea1ae965db8b919 port-reserver.pid ``` Therefore, this commit ensures that they are removed when the container is removed. Signed-off-by: Hayato Kiwata <dev@haytok.jp>
1 parent d5bc0f0 commit b34858e

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

pkg/ocihook/ocihook.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,9 @@ func onPostStop(opts *handlerOpts) error {
748748
if err = killProcessByPidFile(portReserverPidFile); err != nil {
749749
log.L.WithError(err).Errorf("failed to kill the port-reserver process")
750750
}
751+
if err := os.RemoveAll(filepath.Dir(portReserverPidFile)); err != nil {
752+
log.L.WithError(err).Errorf("failed to remove the port-reserver directory %s", filepath.Dir(portReserverPidFile))
753+
}
751754
return nil
752755
}
753756

0 commit comments

Comments
 (0)