diff --git a/service/exec/exec.go b/service/exec/exec.go index a267275..1192b3d 100644 --- a/service/exec/exec.go +++ b/service/exec/exec.go @@ -80,6 +80,9 @@ func NewDockerContainer(memory int64, cpu int64) (*dockerContainer, error) { // CreateAndStartContainer creates a container, sets resource limits, and starts it // The container runs `tail -f /dev/null` to keep running, waiting for new commands func (dc *dockerContainer) CreateAndStartContainer() error { + if dc == nil { + return fmt.Errorf("no container found, please create and start the container first") + } // 如果容器已经存在,则跳过创建 if dc.ContainerID != "" { fmt.Println("Container already exists, skipping creation.") @@ -131,7 +134,7 @@ func (dc *dockerContainer) CreateAndStartContainer() error { // ExecCommandInContainer executes a command in the existing container and returns its output func (dc *dockerContainer) ExecCommandInContainer(command string) (string, error) { - if dc.ContainerID == "" { + if dc == nil { return "", fmt.Errorf("no container found, please create and start the container first") } @@ -173,6 +176,9 @@ func (dc *dockerContainer) ExecCommandInContainer(command string) (string, error // RestartAndCleanContainer stops, removes, and recreates the container func (dc *dockerContainer) RestartAndCleanContainer() error { + if dc == nil { + return nil + } if dc.ContainerID == "" { return fmt.Errorf("no container to restart") } @@ -218,6 +224,9 @@ func (dc *dockerContainer) RestartAndCleanContainer() error { // RemoveContainer stops and removes the container func (dc *dockerContainer) RemoveContainer() error { + if dc == nil { + return nil + } if dc.ContainerID == "" { return fmt.Errorf("no container to remove") }