// Shell creates an interactive Docker session to the specified service // starting it if not currently running func (c *DockerCompose) Shell(config parity.ShellConfig) (err error) { log.Stage("Interactive Shell") log.Debug("Compose - starting docker compose services") mergedConfig := *parity.DEFAULT_INTERACTIVE_SHELL_OPTIONS mergo.MergeWithOverwrite(&mergedConfig, &config) // Check if services running // TODO: if running, attach to running container // if NOT running, start services and attach if c.project != nil { log.Step("Starting compose services") injectDisplayEnvironmentVariables(c.project) } container := fmt.Sprintf("parity-%s_%s_1", c.pluginConfig.ProjectNameSafe, mergedConfig.Service) client := utils.DockerClient() createExecOptions := dockerclient.CreateExecOptions{ AttachStdin: true, AttachStdout: true, AttachStderr: true, Tty: true, Cmd: mergedConfig.Command, User: mergedConfig.User, Container: container, } startExecOptions := dockerclient.StartExecOptions{ Detach: false, Tty: true, InputStream: os.Stdin, OutputStream: os.Stdout, ErrorStream: os.Stderr, RawTerminal: true, } log.Step("Attaching to container '%s'", container) if id, err := client.CreateExec(createExecOptions); err == nil { client.StartExec(id.ID, startExecOptions) } else { log.Error("error: %v", err.Error()) } log.Debug("Docker Compose Run() finished") return err }
// Attach attaches to the specified service in the running container func (c *DockerCompose) Attach(config parity.ShellConfig) (err error) { log.Stage("Interactive Shell") log.Debug("Compose - starting docker compose services") mergedConfig := *parity.DEFAULT_INTERACTIVE_SHELL_OPTIONS mergo.MergeWithOverwrite(&mergedConfig, &config) client := utils.DockerClient() container := fmt.Sprintf("parity-%s_%s_1", c.pluginConfig.ProjectNameSafe, mergedConfig.Service) opts := dockerclient.AttachToContainerOptions{ Stdin: true, Stdout: true, Stderr: true, InputStream: os.Stdin, OutputStream: os.Stdout, ErrorStream: os.Stderr, RawTerminal: true, Container: container, Stream: true, Logs: true, } if c.project.Configs[config.Service] == nil { return fmt.Errorf("Service %s does not exist", config.Service) } log.Step("Attaching to container '%s'", container) if err := client.AttachToContainer(opts); err == nil { err = c.project.Up(config.Service) if err != nil { log.Error("error: %s", err.Error()) return err } } else { return err } _, err = client.WaitContainer(container) log.Error("wc error: %s", err.Error()) log.Debug("Docker Compose Run() finished") return err }