How do I attach to a running Docker container late

2019-03-31 20:09发布

I want to simply start a docker container that executes some java code which ends up starting JBoss.

This is working fine except I can't figure out how to attach to the container again and get back to the bash prompt.

This is how I start my container:

docker run -i -t -p 80:80 -v /tmp/automatefiles:/automatefromhost jboss bash -c 'cd automatefromhost; chmod 777 *.*; ./runAutomate.sh;'

This is the runAutomate.sh

/usr/bin/java -cp Automate.jar -Djava.net.preferIPv4Stack=true net.Automate > automateresults &
tail -f automateresults

Now I have to do the tail at the end to keep the container running after its finished running my Automate code. The end result of this is that Jboss is running with my app configured correctly.

Now when I try to attach to the container again I just get a blank screen with no prompt...and can't get back to the prompt within the container. So no way to interact with the container after it has started.

Any ideas on how I can start the container, keep it running and then attach to the container later and be back in the prompt to do things like ls, tail etc .

EDIT: I ended up doing this:

I copied this approach : https://stackoverflow.com/a/20932423/1519407 and added to my script

while ( true )
    do
    echo "Detach with Ctrl-p Ctrl-q. Dropping to shell"
    sleep 1
    /bin/bash
done

This still seems kind of hacky but it works...I think its probably better to go down the path of installing ssh onto the container or using something like http://phusion.github.io/baseimage-docker/

标签: docker
5条回答
叼着烟拽天下
2楼-- · 2019-03-31 20:31

The command below:

docker exec -it [container id/name] /bin/bash

can attach a running container.

查看更多
Explosion°爆炸
3楼-- · 2019-03-31 20:34

I have the same problem with docker attach. When running a container as a service (with -d and a front process) docker attach will not give a prompt. More info on that here: docker attach vs lxc-attach

An answer to your question is, look at Run a service automatically in a docker container.

Another option is to install an ssh server and connect via ssh.

Some more advanced info and options are explained in this blog by Jerome Petazzoni: http://jpetazzo.github.io/2014/03/23/lxc-attach-nsinit-nsenter-docker-0-9/

查看更多
唯我独甜
4楼-- · 2019-03-31 20:34

I know this doesn't good practice but works fine.

In first container, I added a volume share in dockerfile. So, I run another container with "--volume-from" CONTAINER_ID to read the log.

查看更多
The star\"
5楼-- · 2019-03-31 20:37

Just putting in code words.

docker attach container_name
ctrl p ctrl q

exit command stops the container, where as ctrlp and ctrl q just detaches that container and keeps it running

Update: For those who don't know already, from docker 1.3 or so, we can use exec command for attaching to a container and exiting it without hassle.

eg: docker exec -it container_name bash

You can just type exit when needed, it will exit the container and still keeps it running.

查看更多
来,给爷笑一个
6楼-- · 2019-03-31 20:41

docker attach [options] [container]

In its simplest form:

docker attach my_container

Once in there, you can leave by using Ctrl-P then Ctrl-Q; if you use Ctrl-C you will kill the container.

https://docs.docker.com/engine/reference/commandline/attach/

查看更多
登录 后发表回答