Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions internal/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,20 @@ func Copy(containerName, containerPath, localPath string) error {
}
return nil
}

// CheckDaemonRunning checks if the Docker daemon is running.
// Returns nil if the daemon is running, or an error if it's not.
func CheckDaemonRunning() error {
cmd := exec.Command("docker", "info")
Copy link
Contributor

@mrodm mrodm Jul 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commands has a large output. It should not be an issue, but maybe it could be format the output to just shown a specific value (e.g. ServerVersion).

Suggested change
cmd := exec.Command("docker", "info")
cmd := exec.Command("docker", "info", "--format", "{{ .ServerVersion }}")

errOutput := new(bytes.Buffer)
cmd.Stderr = errOutput

logger.Debugf("running command to check Docker daemon: %s", cmd)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd use Trace level for this message

Suggested change
logger.Debugf("running command to check Docker daemon: %s", cmd)
logger.Tracef("running command to check Docker daemon: %s", cmd)

if err := cmd.Run(); err != nil {
daemonError := fmt.Errorf("docker daemon is not running (stderr=%q): %w", errOutput.String(), err)
logger.Error(": Docker daemon is not running or not accessible")
logger.Error(": Please make sure Docker is installed and running before executing commands that require Docker")
Comment on lines +204 to +205
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an internal method. I would prefer to log closer to the user.

Suggested change
logger.Error(": Docker daemon is not running or not accessible")
logger.Error(": Please make sure Docker is installed and running before executing commands that require Docker")

return daemonError
Comment on lines +203 to +206
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit. You can directly return the error.

Suggested change
daemonError := fmt.Errorf("docker daemon is not running (stderr=%q): %w", errOutput.String(), err)
logger.Error(": Docker daemon is not running or not accessible")
logger.Error(": Please make sure Docker is installed and running before executing commands that require Docker")
return daemonError
logger.Error(": Docker daemon is not running or not accessible")
logger.Error(": Please make sure Docker is installed and running before executing commands that require Docker")
return fmt.Errorf("docker daemon is not running (stderr=%q): %w", errOutput.String(), err)

}
return nil
}
27 changes: 27 additions & 0 deletions internal/stack/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/elastic/elastic-package/internal/compose"
"github.com/elastic/elastic-package/internal/docker"
"github.com/elastic/elastic-package/internal/install"
"github.com/elastic/elastic-package/internal/logger"
)

type ServiceStatus struct {
Expand Down Expand Up @@ -50,7 +51,18 @@ func (eb *envBuilder) build() []string {
return eb.vars
}

// CheckDockerRunning verifies that the Docker daemon is running before executing Docker operations
func CheckDockerRunning() error {
logger.Debug("Checking if Docker daemon is running...")
return docker.CheckDaemonRunning()
}

func dockerComposeBuild(ctx context.Context, options Options) error {
// Check if Docker daemon is running
if err := CheckDockerRunning(); err != nil {
return err
}

c, err := compose.NewProject(DockerComposeProjectName(options.Profile), options.Profile.Path(ProfileStackPath, ComposeFile))
if err != nil {
return fmt.Errorf("could not create docker compose project: %w", err)
Expand All @@ -77,6 +89,11 @@ func dockerComposeBuild(ctx context.Context, options Options) error {
}

func dockerComposePull(ctx context.Context, options Options) error {
// Check if Docker daemon is running
if err := CheckDockerRunning(); err != nil {
return err
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having to check it on every operation, I think we could create a new newComposeProvider() (*composeProvider, error) constructor that could run the check, when initializing it in the factory, here:

return &composeProvider{}, nil

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the other providers (Environment and Serverless) could take advantage of this check too. IIRC these two providers run also docker (docker-compose) commands.

Just thinking that doing this check in the providers makes that elastic-package test or install commands do not run this check.
Maybe it's not needed, since all these commands will require to be the Elastic stack up... WDYT?


c, err := compose.NewProject(DockerComposeProjectName(options.Profile), options.Profile.Path(ProfileStackPath, ComposeFile))
if err != nil {
return fmt.Errorf("could not create docker compose project: %w", err)
Expand All @@ -103,6 +120,11 @@ func dockerComposePull(ctx context.Context, options Options) error {
}

func dockerComposeUp(ctx context.Context, options Options) error {
// Check if Docker daemon is running
if err := CheckDockerRunning(); err != nil {
return err
}

c, err := compose.NewProject(DockerComposeProjectName(options.Profile), options.Profile.Path(ProfileStackPath, ComposeFile))
if err != nil {
return fmt.Errorf("could not create docker compose project: %w", err)
Expand Down Expand Up @@ -135,6 +157,11 @@ func dockerComposeUp(ctx context.Context, options Options) error {
}

func dockerComposeDown(ctx context.Context, options Options) error {
// Check if Docker daemon is running
if err := CheckDockerRunning(); err != nil {
return err
}

c, err := compose.NewProject(DockerComposeProjectName(options.Profile), options.Profile.Path(ProfileStackPath, ComposeFile))
if err != nil {
return fmt.Errorf("could not create docker compose project: %w", err)
Expand Down