@@ -413,17 +413,38 @@ export const checkPortInUse = async (
413413 serverId ?: string ,
414414) : Promise < { isInUse : boolean ; conflictingContainer ?: string } > => {
415415 try {
416- const command = `docker ps -a --format '{{.Names}}' | grep -v '^dokploy-traefik$' | while read name; do docker port "$name" 2>/dev/null | grep -q ':${ port } ' && echo "$name" && break; done || true` ;
417- const { stdout } = serverId
418- ? await execAsyncRemote ( serverId , command )
419- : await execAsync ( command ) ;
416+ // Check if port is in use by a Docker container
417+ const dockerCommand = `docker ps -a --format '{{.Names}}' | grep -v '^dokploy-traefik$' | while read name; do docker port "$name" 2>/dev/null | grep -q ':${ port } ' && echo "$name" && break; done || true` ;
418+ const { stdout : dockerOut } = serverId
419+ ? await execAsyncRemote ( serverId , dockerCommand )
420+ : await execAsync ( dockerCommand ) ;
420421
421- const container = stdout . trim ( ) ;
422+ const container = dockerOut . trim ( ) ;
422423
423- return {
424- isInUse : ! ! container ,
425- conflictingContainer : container || undefined ,
426- } ;
424+ if ( container ) {
425+ return {
426+ isInUse : true ,
427+ conflictingContainer : `container "${ container } "` ,
428+ } ;
429+ }
430+
431+ // Check if port is in use by a host-level service (non-Docker)
432+ // Dokploy runs inside a container, so we spawn an ephemeral container
433+ // with --net=host to share the host's network stack and use nc -z to
434+ // check if something is listening on the port
435+ const hostCommand = `docker run --rm --net=host busybox sh -c 'nc -z 0.0.0.0 ${ port } 2>/dev/null && echo in_use || echo free'` ;
436+ const { stdout : hostOut } = serverId
437+ ? await execAsyncRemote ( serverId , hostCommand )
438+ : await execAsync ( hostCommand ) ;
439+
440+ if ( hostOut . includes ( "in_use" ) ) {
441+ return {
442+ isInUse : true ,
443+ conflictingContainer : "a host-level service" ,
444+ } ;
445+ }
446+
447+ return { isInUse : false } ;
427448 } catch ( error ) {
428449 console . error ( "Error checking port availability:" , error ) ;
429450 return { isInUse : false } ;
0 commit comments