4

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/

Community
  • 1
  • 1
tinytelly
  • 279
  • 2
  • 4
  • 14

5 Answers5

9

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.

phoenix
  • 607
  • 6
  • 13
  • Should the container be stopped or started in order to do this? I am interested in knowing the original mode you used to to create and operate the container. – Web User Apr 10 '17 at 06:53
  • @WebUser Container should be running, before we run the attach command `docker exec` makes more sense because we can terminal in to a stopped container as well. I created the containers using `docker run` from the image! – phoenix Apr 11 '17 at 15:31
  • So you created the container with `docker run` and did you add any command in interactive mode using `--it`? – Web User Apr 11 '17 at 15:56
3

The command below:

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

can attach a running container.

zcmyworld
  • 393
  • 1
  • 2
  • 9
2

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/

Community
  • 1
  • 1
qkrijger
  • 26,686
  • 6
  • 36
  • 37
1

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.

Fernando Ike
  • 101
  • 4
0

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/

Philippe
  • 3,945
  • 3
  • 38
  • 56