How to enter in a Docker container already running with a new TTY

DockerTty

Docker Problem Overview


I have a container that is running the Apache service in the foreground. I would like to be able to access the container from another shell in order to "poke around" inside it and examine the files. At the moment, if I attach to the container, I am left looking at the Apache daemon and cannot run any commands.

Is it possible to attach another tty to a running container? Possibly, I can take advantage of the fact that Docker is actually just wrapping around LXC containers? I have tried sudo lxc-console -n [container-id] -t [1-4] but it appears that only one tty is made available and that is the one running the apache daemon. Perhaps there is a way to enable multiple lxc consoles during the build?

I would rather not configure and build the container with an openssh service if possible.

Docker Solutions


Solution 1 - Docker

With docker 1.3, there is a new command docker exec. This allows you to enter a running container:

docker exec -it [container-id] bash

Note: this assumes bash is installed on your container. You may run sh or whatever interactive shell is installed on the container.

Solution 2 - Docker

You should use Jérôme Petazzoni's tool called 'nsenter' to enter a container without using SSH. See: https://github.com/jpetazzo/nsenter

Install with simply running: docker run -v /usr/local/bin:/target jpetazzo/nsenter

Then use the command docker-enter <container-id> to enter the container.

Solution 3 - Docker

Update

As of docker 0.9, for the steps below to now work, one now has to update the /etc/default/docker file with the '-e lxc' to the docker daemon startup option before restarting the daemon (I did this by rebooting the host).

update to the /etc/default/docker file

This is all because...

> ...it [docker 0.9] contains a new "engine driver" abstraction to make possible the use > of other API than LXC to start containers. It also provide a new > engine driver based on a new API library (libcontainer) which is able > to handle Control Groups without using LXC tools. The main issue is > that if you are relying on lxc-attach to perform actions on your > container, like starting a shell inside the container, which is > insanely useful for developpment environment...

source

Please note that this will prevent the new host only networking optional feature of docker 0.11 from "working" and you will only see the loopback interface. bug report


It turns out that the solution to a different question was also the solution to this one:

> ...you can use docker ps -notrunc to get the full lxc container ID and > then use lxc-attach -n <container_id> run bash in that container as > root.

Update: You will soon need to use ps --no-trunc instead of ps -notrunc which is being deprecated.

enter image description here Find the full container ID

enter image description here Enter the lxc attach command.

enter image description here Top shows my apache process running that docker started.

Solution 4 - Docker

First step get container id:

docker ps

This will show you something like

> CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES > > 1170fe9e9460 localhost:5000/python:env-7e847468c4d73a0f35e9c5164046ad88 "./run_notebook.sh" 26 seconds ago Up 25 seconds 0.0.0.0:8989->9999/tcp SLURM_TASK-303337_0

1170fe9e9460 is the container id in this case.

Second, enter the docker :

docker exec -it [container_id] bash

so in the above case: docker exec -it 1170fe9e9460 bash

Solution 5 - Docker

What about running tmux/GNU Screen within the container? Seems the smoother way to access as many vty as you want with a simple:

$ docker attach {container id}

Solution 6 - Docker

docker exec -ti 'CONTAINER_NAME' sh

or

docker exec -ti 'CONTAINER_ID' sh

Solution 7 - Docker

nsenter does that. However I also needed to enter a container in a simple way and nsenter didn't suffice for my needs. It was buggy in some occasions (black screen plus -wd flag not working). Furthermore I wanted to login as a specific user and in a specific directory.

I ended up making my own tool to enter containers. You can find it at: https://github.com/Pithikos/docker-enter

Its usage is as easy as

./docker-enter [-u <user>] [-d <directory>] <container ID>

Solution 8 - Docker

docker exec -t -i container_name /bin/bash

Will take you to the containers console.

Solution 9 - Docker

The "nsinit" way is:

install nsinit

git clone [email protected]:dotcloud/docker.git
cd docker
make shell

from inside the container:

go install github.com/dotcloud/docker/pkg/libcontainer/nsinit/nsinit

from outside:

docker cp id_docker_container:/go/bin/nsinit /root/

use it

cd /var/lib/docker/execdriver/native/<container_id>/
nsinit exec bash

Solution 10 - Docker

I started powershell on a running microsoft/iis run as daemon using

docker exec -it <nameOfContainer> powershell

Solution 11 - Docker

On Windows 10, I have docker installed. I am running Jnekins on a container and I encountered the same error message. Here is a step by step guide to resolve this issue:

Step 1: Open gitbash and run docker run -p 8080:8080 -p 50000:50000 jenkins.

Step 2: Open a new terminal.

Step 3: Do "docker ps" to get list of the running container. Copy the container id.

Step 4: Now if you do "docker exec -it {container id} sh" or "docker exec -it {container id} bash" you will get an error message similar to " the input device is not a TTY. If you are using mintty, try prefixing the command with 'winpty'"

Step 5: Run command " $winpty docker exec -it {container id} sh"

vola !! You are now inside the terminal.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionProgramsterView Question on Stackoverflow
Solution 1 - DockerMichael_ScharfView Answer on Stackoverflow
Solution 2 - DockerHyperfocusView Answer on Stackoverflow
Solution 3 - DockerProgramsterView Answer on Stackoverflow
Solution 4 - Dockerpatapouf_aiView Answer on Stackoverflow
Solution 5 - DockersolrView Answer on Stackoverflow
Solution 6 - DockerFlavioView Answer on Stackoverflow
Solution 7 - DockerPithikosView Answer on Stackoverflow
Solution 8 - DockerDanstanView Answer on Stackoverflow
Solution 9 - DockerIvailo BardarovView Answer on Stackoverflow
Solution 10 - DockerAhmed SamirView Answer on Stackoverflow
Solution 11 - DockerDev 00721View Answer on Stackoverflow