I can't get an interactive shell to my Docker container inside a screen session. Outside from screen everything works fine. After starting the container is working in background and is not attachable at all.
Any ideas?
If your container is running and you're using a relatively up-to-date version of Docker (I can't remember the version number when this was added), you can do the following to get shell access to the container:
docker exec -it my_container_name_or_hash bash
Related
I have a docker service/image I'm using which restarts as soon as starts.
I'm unable to fix the issue by getting into the container using
docker exec -it CONTAIER_NAME
since it restarts/terminates as soon as it boots.
Is there anyway I can pause it directly? I can't rebuild the image as I don't have access to the internet on the server. (Yes I'm sure the rebuild or build--no-cache will fix the issue)
The issue should be easily fixable if I modify permissions for a certain folder, but I'm not sure how to do this inside the container when I can't access it. The image doesn't have a docker file and is used directly from the docker hub.
If we do not get any information from the container's logs, we have the option to start the process "manually". For this, we start the container with an interactive terminal (-it, -i to keep STDIN open, -t to open a pseudo-TTY) and override the entrypoint to be a shell, e.g. bash. For good measure, we want the container to be removed when it terminates (i.e. when we exit the termainal, --rm):
docker run ... -it --rm --entrypoint /bin/bash
Once inside the container, we can start the process that would have normally started through the entrypoint from the container's terminal and extract error information from here.
I'm running on a windows host different server core VMs (hyper-v) and in each one docker service. The containers I try to run, using docker run nanoserver/iis-php command, are created but immediately disappear, exited with exit code 0, no error messages. Since it happens in different VMs, I believe it is something in the VMs host. Any idea?
according to https://hub.docker.com/r/nanoserver/iis-php
use docker run --name nanoiis -d -it -p 80:80 nanoserver/iis-php
so that the container is started in daemon mode, interactive mode and with tty
I do not know iis-php, but from the dockerfile of the image below, the last command is just to make webrequet, do not see any server process started.
https://github.com/nanoserver/IIS-PHP/blob/master/Dockerfile/Dockerfile
the containers [...] are created but immediately disappear, exited with exit code 0, no error messages.
So as I understand they don't disappear but simply exit immediately with an exit code of 0 which you still can see with docker ps -a?
The exit code 0 indicates "success". So it looks like the containers were created and started successfully and the processes started in them executed "successfully". But what this means depends an the actual command started in the containers.
But neither the Dockerfile of the nanoserver/iis-php image nor the Dockerfile of its base image nanoserver/iis specify a CMD. Also no command is specified in your docker run command.
docker logs gives me nothing just: Microsoft Windows [Version 10.0.14393] (c) 2016 Microsoft Corporation. All rights reserved. C:\>
This looks like an interactive command prompt expecting user input. So what most probably is happening here is that the containers start a simple interactive shell since no other command to execute is explicitly specified. But since no stdin is attached to the container it can not read any input and will exit again.
You can test that the containers / your docker setup is working correctly with
docker run -ti nanoserver/iis-php
This should drop you into the interactive shell inside the container. You could then interactively execute commands in the container.
In order to have it run in the background you have to pass and command to execute to docker run
# this is just an example! The exact command you need
# depends on what you actually want to run inside the container
docker run -d nanoserver/iis-php php index.php
In my CentOS server I use docker created a container,
I opened two sessions connected to the container by command:
docker attach container-name
but there is an issue, in each window I execute command the other window is display the same information.
so I cannot control the container when it is installing package.
is it possible to avoid this issue?
The docker attach command attaches to the currently running process as defined by CMD. You can attach as many times as you want, but they all connect to the same process.
If you want to access the container and have different sessions to it, use:
docker exec -it container-name bash
Or whatever shell is available. bash is common, but you may need to use sh or find out what's used, if any is there at all. Some containers are super stripped down.
The -it flag enables "interactive" mode, as otherwise it just runs that command and shows you the output.
Running docker run with a -d option is described as running the container in the background. This is what most tutorials do when they don't want to interact with the container. On another tutorial I saw the use of bash style & to send the process to the background instead of adding the -d option.
Running docker run -d hello_world only outputs the container ID. On the other hand docker run hello_world & still gives me the same output as if i had run docker run hello_world.
If I do the both experiments with docker run nginx I get the same behavior on both (at least as far as I can see), and both show up if I run docker ps.
Is the process the same in both cases(apart from the printing of the ID and output not being redirected with &)? If not, what is going on behind the scenes in each?
docker is designed as C-S architecture: docker client, docker daemon(In fact still could fined to container-d, shim, runc etc).
When you execute docker run, it will just use docker client to send things to docker daemon, and let daemon call runc etc to start a container.
So:
docker run -d: It will let runc run container in background, you can use docker logs $container_name to see all logs later, the background happend on server side.
docker run &: It will make the linux command run at background, which means the docker run will be in background, this background run on client side. So you still can see stdout etc in terminal. More, if you leave the terminal(even you bash was set as nohup), you will not see it in terminal, you still need docker logs to see them.
If I use docker exec to fire up a shell,
docker exec -ti <CONTAINER> /bin/bash
I could use Ctrl+p Ctrl+q to detach this shell process. Then this shell is still running inside the container, but how can I reattach to that one particular shell (the one started by docker exec, not docker run)?
Sadly, this is not possible yet; see this issue on GitHub. I've also wanted this functionality, but at the moment it seems like there's no direct way to do this.
A workaround has been proposed, to take care of the case where you're accessing a box via ssh and running docker exec on the remote box (or, for the case where your terminal emulator is unstable and may crash on you): Always run your docker exec commands inside screen or tmux. If you do this, whenever you get detached from the screen/tmux session, you can re-attach to it later and still have your docker exec commands accessible. (this is a bit different than what was suggested by #vodolaz095, since it involves running screen or tmux outside the container, making it suitable for use with containers that don't run screen/tmux as their main process)
docker exec is specifically for running new things in an already started container, be it a shell or some other process.
docker attach is for attaching to a running process, so you can use only one instance of shell.
Run you container(process)
docker run -tid --name <CONTAINER> <IMAGE>:<TAG> bin/bash
Then
docker attach <CONTAINER>
To detach Ctrl+p + Ctrl+q
On this way you can attach and detach multiple times with only one instance of shell