简体   繁体   中英

Can't attach to bash running the Docker container

Having troubles with attaching to the bash instance keeping the container running.

To be more detailed. I am running container as here:

$ docker run -dt --name test ubuntu bash

Now it should be actually running, not finished.

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             
STATUS              PORTS               NAMES
f3596c613cfe        ubuntu              "bash"              4 seconds ago       Up 2 seconds                            test

After this, I am trying to attach to that instance of bash that keeps the container running. Like this:

$ docker attach test

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.

Is there some other way to bash that keeps the container running?

I know, that I can run a different instance of bash and use it docker exec -it test bash . But being more general, is there a way to connect to process that's running in Docker container?

Sometimes it can be useful to save the session of a process running inside the container.

SOLUTION

Thanks to user2915097 for pointing out the missing -i flag.

So now we can have persistent bash session. For example, let's set some alias and reuse after stopping and restarting the container.

$ docker run -itd --name test ubuntu bash

To attach to bash instance just run

$ docker attach test
root@3534cbe1e994:/# alias test="Hello, world!"

To detach from container and not to stop the container press Ctrl + p , Ctrl + q

Then we can stop and restart the container

$ docker stop test
$ docker start test

Now we can attach to the same bash instance and check our alias

$ docker attach test
root@3534cbe1e994:/# test
Hello, world!

Everything is working perfectly!

As I have pointed out in my comment use-case for this can be running some interactive shells as bash , octave , ipython in Docker container persisting all the history, imports, variables and temporary settings just by reattaching to the same instance.

Your container is running, it is not finished, as you can see

  • it appears in docker ps , so it is a running container
  • it show up n seconds

you launch it with -dt so you want it

detached (for d) allocate a tty (for t)

but not interactive, as you do not add -i

Usually, you nearly always provide -it together, it may be -idt

See this thread

When would I use `--interactive` without `--tty` in a Docker container?

as you want bash, I think you should add -i

I am not sure why you use -d

Usually it is

docker run -it --rm --name=mytest ubuntu bash

and you can test

A container's running lifecycle is determined by its root process, which is bash in your example. When your start your ubuntu container with bash as the process, bash is immediately exiting because it has nothing to keep it running. That's why the container immediately exits and there's nothing to attach to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM