fix: 添加对 nil 容器的检查,确保在执行容器操作前容器已创建
This commit is contained in:
parent
a374db2560
commit
b49b780da2
@ -80,6 +80,9 @@ func NewDockerContainer(memory int64, cpu int64) (*dockerContainer, error) {
|
|||||||
// CreateAndStartContainer creates a container, sets resource limits, and starts it
|
// CreateAndStartContainer creates a container, sets resource limits, and starts it
|
||||||
// The container runs `tail -f /dev/null` to keep running, waiting for new commands
|
// The container runs `tail -f /dev/null` to keep running, waiting for new commands
|
||||||
func (dc *dockerContainer) CreateAndStartContainer() error {
|
func (dc *dockerContainer) CreateAndStartContainer() error {
|
||||||
|
if dc == nil {
|
||||||
|
return fmt.Errorf("no container found, please create and start the container first")
|
||||||
|
}
|
||||||
// 如果容器已经存在,则跳过创建
|
// 如果容器已经存在,则跳过创建
|
||||||
if dc.ContainerID != "" {
|
if dc.ContainerID != "" {
|
||||||
fmt.Println("Container already exists, skipping creation.")
|
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
|
// ExecCommandInContainer executes a command in the existing container and returns its output
|
||||||
func (dc *dockerContainer) ExecCommandInContainer(command string) (string, error) {
|
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")
|
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
|
// RestartAndCleanContainer stops, removes, and recreates the container
|
||||||
func (dc *dockerContainer) RestartAndCleanContainer() error {
|
func (dc *dockerContainer) RestartAndCleanContainer() error {
|
||||||
|
if dc == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
if dc.ContainerID == "" {
|
if dc.ContainerID == "" {
|
||||||
return fmt.Errorf("no container to restart")
|
return fmt.Errorf("no container to restart")
|
||||||
}
|
}
|
||||||
@ -218,6 +224,9 @@ func (dc *dockerContainer) RestartAndCleanContainer() error {
|
|||||||
|
|
||||||
// RemoveContainer stops and removes the container
|
// RemoveContainer stops and removes the container
|
||||||
func (dc *dockerContainer) RemoveContainer() error {
|
func (dc *dockerContainer) RemoveContainer() error {
|
||||||
|
if dc == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
if dc.ContainerID == "" {
|
if dc.ContainerID == "" {
|
||||||
return fmt.Errorf("no container to remove")
|
return fmt.Errorf("no container to remove")
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user