title: Backup PGSQL in a container using Golang docker SDK
body: <p>On host machine I have docker-compose stack that have a service called
  transactions-db
  its a PostgreSQL container</p>
  <p>I want to create a backup service using Golang to be able to create .sql to be used for restores later on</p>
  <p>docker-compose.yml for backup service</p>
  <pre><code>version: &quot;3.8&quot;
  
  services:
    backup-service:
      build:
        context: .
        dockerfile: Dockerfile
      image: backup-service
      container_name: backup-service
      volumes:
        - .:/app
        - /var/run/docker.sock:/var/run/docker.sock
  </code></pre>
  <p>main.go</p>
  <pre><code>package main
  
  import (
      &quot;context&quot;
      &quot;fmt&quot;
      &quot;os&quot;
  )
  
  func main () {
      result, err := ExecuteCommandOnContainer(context.Background(), ConnectToDocker(), &quot;transactions-db&quot;)
      if err != nil {
          fmt.Println(err)
          os.Exit(1)
      }
      fmt.Println(result.ExitCode)
  }
  </code></pre>
  <p>docker.go</p>
  <pre><code>package main
  
  import (
      &quot;context&quot;
      &quot;fmt&quot;
      &quot;github.com/docker/docker/api/types&quot;
      &quot;github.com/docker/docker/client&quot;
  )
  
  func ConnectToDocker() *client.Client {
      cli, err := client.NewClientWithOpts()
      if err != nil {
          fmt .Println(err)
      }
      return cli
  }
  func ExecuteCommandOnContainer(ctx context.Context, cli *client.Client, containerID string) (types.ContainerExecInspect, error) {
      command := []string{&quot;pg_dumpall&quot;, &quot;-c&quot;, &quot;-U&quot;, &quot;user&quot;, &quot;|&quot;, &quot;gzip&quot;, &quot;&gt;&quot;, &quot;backup/tr.sql&quot;}
      execConfig := types.ExecConfig{
          Tty:          true,
          AttachStdin:  true,
          AttachStderr: true,
          AttachStdout: true,
          Cmd:          command,
      }
      create, err := cli.ContainerExecCreate(ctx, containerID, execConfig)
      if err != nil {
          fmt .Println(err)
      }
  
      inspect, err := cli.ContainerExecInspect(ctx, create.ID)
      if err != nil {
          fmt .Println(err)
      }
      return inspect, nil
  }
  </code></pre>
  <p>basically nothing happens when I run this code. I only get the following:</p>
  <p>backup-service  | 0
  backup-service exited with code 0</p>
  <p>I tried to normally run: go run .
  I've also tried using my docker-compose up
  same result</p>
  <p>note: I've tested listing container it works but when trying to execute a command nothing</p>