title: Can't attach to bash running the Docker container
body: <p>Having troubles with attaching to the bash instance keeping the container running.</p>
  
  <p>To be more detailed. I am running container as here:</p>
  
  <pre><code>$ docker run -dt --name test ubuntu bash
  </code></pre>
  
  <p>Now it should be actually running, not finished.</p>
  
  <pre><code>$ docker ps
  CONTAINER ID        IMAGE               COMMAND             CREATED             
  STATUS              PORTS               NAMES
  f3596c613cfe        ubuntu              "bash"              4 seconds ago       Up 2 seconds                            test
  </code></pre>
  
  <p>After this, I am trying to attach to that instance of bash that keeps the container running. Like this:</p>
  
  <pre><code>$ docker attach test
  </code></pre>
  
  <p>Running this command I am able to write something to stdin, but no result following. I am not sure if bash is getting lines I typed.</p>
  
  <p>Is there some other way to bash that keeps the container running?</p>
  
  <p>I know, that I can run a different instance of bash and use it <code>docker exec -it test bash</code>. But being more general, is there a way to connect to process that's running in Docker container?</p>
  
  <p>Sometimes it can be useful to save the session of a process running inside the container.</p>
  
  <h1>SOLUTION</h1>
  
  <p>Thanks to <code>user2915097</code> for pointing out the missing <code>-i</code> flag.</p>
  
  <p>So now we can have persistent bash session. For example, let's set some <code>alias</code> and reuse after stopping and restarting the container.</p>
  
  <pre><code>$ docker run -itd --name test ubuntu bash
  </code></pre>
  
  <p>To attach to <code>bash</code> instance just run</p>
  
  <pre><code>$ docker attach test
  root@3534cbe1e994:/# alias test="Hello, world!"
  </code></pre>
  
  <p>To detach from container and not to stop the container press <kbd>Ctrl</kbd>+<kbd>p</kbd>, <kbd>Ctrl</kbd>+<kbd>q</kbd></p>
  
  <p>Then we can stop and restart the container</p>
  
  <pre><code>$ docker stop test
  $ docker start test
  </code></pre>
  
  <p>Now we can attach to the same <code>bash</code> instance and check our alias</p>
  
  <pre><code>$ docker attach test
  root@3534cbe1e994:/# test
  Hello, world!
  </code></pre>
  
  <p>Everything is working perfectly!</p>
  
  <p>As I have pointed out in my comment use-case for this can be running some interactive shells as <code>bash</code>, <code>octave</code>, <code>ipython</code> in Docker container persisting all the history, imports, variables and temporary settings just
  by reattaching to the same instance.</p>