以编程方式执行到Docker容器中

我想编写一个程序,允许用户选择一个正在运行的docker容器,并为其提供一个交互式shell。我想要的程序流程大致如下:

  1. user runs program from bash - ./my_program
  2. user is given an interactive cli they can use to choose which docker container to exec into
  3. when user chooses container, something like docker exec -it <CONTAINER_ID> bash is run from my_program, my_program exits, and the user is transferred into a shell session of the docker container as if they had manually run docker exec -it <CONTAINER_ID> bash

我正在使用以下代码从golang尝试此操作:

rv, err := exec.Command("docker", "exec", "-it", containerId, "bash").Output()
log.Infof("RV: %v", rv)
if err != nil {
    log.Errorf("Error exec-ing into container: %s", err)
}

并看到以下输出:

RV: []
Error exec-ing into container: exit status 1

I'm trying with err := exec.Command("docker", "exec", "-it", containerId, "bash").Run() as well and see the same error.

我该如何创建这样的程序或调试当前发生的事情?

评论