Why is "docker start" outputting the name of the container? - docker

I have a small minimal test container made using the ruby image. The ruby script is simple, and outputs the single string "Twitter".
When I first run the image and create the container, I get this output:
$ docker run -it --name my-running-script my-ruby-app
Twitter
Great so far - the script completes and the container exits.
But when I try to start it again, it first outputs the name of the container:
$ docker start -a my-running-script
my-running-script
Twitter
What is causing this output, and how can I get it to stop? (It's printed on stdout, and redirecting stderr doesn't help.)
I don't know if it's relevant, but this is running on OS X using boot2docker.

This is the expected behavior for docker start. Then you can do things like assign the containerID to a variable, etc.

Related

How to see the logs of a docker container

I have a simple code for which I have created a docker container and the status shows it running fine. Inside the code I have used some print() commands to print the data. I wanted to see that print command output.
For this I have seen docker logs . But it seems not to be working as it shows no logs. How to check logs.?
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a3b3fd261b94 myfirstdocker "python3 ./my_script…" 22 minutes ago Up 22 minutes elegant_darwin
$ sudo docker logs a3b3fd261b94
<shows nothing>
The first point you need to print your logs to stdout.
To check docker logs just use the following command:
docker logs --help
Usage: docker logs [OPTIONS] CONTAINER
Fetch the logs of a container
Options:
--details Show extra details provided to logs
-f, --follow Follow log output
--help Print usage
--since string Show logs since timestamp
--tail string Number of lines to show from the end of the logs (default "all")
-t, --timestamps Show timestamps
Some example:
docker logs --since=1h <container_id>
4
Let's try using that docker create start and then logs command again and see what happens.
sudo docker create busybox echo hi there
output of the command
now I will take the ID and run a docker start and paste the ID that starts up the container it executes echo high there inside of it and then immediately exits.
Now I want to go back to that stopped container and get all the logs that have been emitted inside of it.
To do so I can run at docker logs and then paste the ID in and I will see that when the container had been running it had printed out the string Hi there.
One thing to be really clear about is that by running docker logs I am not re-running or restarting the container to in any way shape or form, I am just getting a record of all the logs that have been emitted from that container.
docker logs container_id
If there's not so much supposed output (e.g. script just tries to print few bytes), I'd suspect python is buffering it.
Try adding more data to the output to be sure that buffer is flushed, and also using PYTHONUNBUFFERED=1 (although, python3 still may do some buffering despite of this setting).

docker run vs create+start: why are created containers different?

Related to
docker container started in Detached mode stopped after process execution
https://serverfault.com/questions/661909/the-right-way-to-keep-docker-container-started-when-it-used-for-periodic-tasks
I do understand the difference between docker run and create + start, but don't understand how the actual containers created in these two ways differ.
Say I create and run a container with
docker run -dit debian:testing-slim
and then stop it. The created container can later be started with
docker start silly_docker_name
and it'll run in the background, because the entry command for the image is bash.
But when a container is first created
docker create --name silly_name debian:testing-slim
and then started with
docker start silly_name
then it'll exit immediately. Why isn't bash started, or how come it exits in this case?
The difference for a container process that is a shell (like bash in your debian example) is that a shell started without a terminal+interactive "mode" exits without doing anything.
You can test this by changing the command of a create'd container to something that doesn't require a terminal:
$ docker create --name thedate debian date
Now if I run thedate container, each time I run it it outputs the date (in the logs) and exits. docker logs thedate will show this; one entry for each run.
To be explicit, your docker run command has flags -dit: detached, interactive (connect STDIN), and tty are all enabled.
If you want a similar approach with create & start, then you need to allocate a tty for the created container:
$ docker create -it --name ashell debian
Now if I start it, I ask to attach/interactively to it and I get the same behavior as run:
$ docker start -ai ashell
root#6e44e2ae8817:/#
NOTE: [25 Jan 2018] Edited to add the -i flag on create as a commenter noted that as originally written this did not work, as the container metadata did not have stdin connected at the create stage

Docker Exec -it bash Terminal Output Improperly Formatted [duplicate]

This question already has answers here:
Missing Carriage Return in Docker for Mac Containers
(3 answers)
Closed 5 years ago.
The command promp after running docker exec -it ... bash is not formatted nicely. The problem happens on the OSX terminal or iTerm2. Any setting for the iTerm2 I need to change or is that related to the bash command? Thanks.
Picture:
I think I can tell from the picture that you're not actually talking about docker logs (which is a docker real docker command that you didn't run). It looks like you want to know why your console output is not printing with nice formatting after you get to a command prompt inside a container with docker exec -it ... bash.
I suspect your PS1 length is being calculated incorrectly either inside the container or on your host machine. Read about it here. The prompt inside the container looks like the standard prompt and the prompt on your host machine looks fancy, so I bet you have a problem with the PS1 on your OS X host.
To see if your host machine is causing the problem, drop into a subshell with a minimal PS1, then run Docker and see if the problem persists:
$ bash --rcfile <(echo "PS1='$ '") -i
$ docker exec -it ... bash
# ls
If the problem goes away, it is a problem with the PS1 on your host machine. Fix it permanently by following the directions in the link above so the length is calculated correctly.

Using Docker's CMD to run a Shell Script + Get Output

I've been using a docker file for about 6 months now without issue but after a few changes in compute engine, I'm hitting a weird issue where something in my start up script is behaving the way it should / it used to.
I have a shell script that does a couple tweaks to the environment before starting a web server which is started like so:
ADD src/docker/startup.sh /home/gauntface/docker/startup.sh
CMD /home/gauntface/docker/startup.sh
startup.sh echo's logs but I can't find a way to view these logs, does anyone have any advice?
docker logs shows nothing for my container
Additional Notes
I'm running the docker command with daemon mode. Without Daemon mode, docker throws this error:
the input device is not a TTY
The Docker file and start up script are here:
https://github.com/gauntface/gf-site/blob/staging/src/docker/Dockerfile-base
https://github.com/gauntface/gf-site/blob/staging/src/docker/startup.sh
docker logs by default will show the output from stdout/stderr:
$ docker run -it --name test-it busybox echo hello world
hello world
$ docker logs test-it
hello world
We'd need to know more about your shell script, what output it generates, and what debugging you've done to give a more detailed answer about why it's not working.

Docker: reattach to `docker exec` process

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

Resources