How to use docker logs - docker

The question may be a bit of newbie.
I run docker exec -it mycontainer bash to enter into a daemon container(postgresSQL ),
and echo something.
now I exit it , and use docker logs mycontainer so as to see my echos.
According to
The docker logs command batch-retrieves logs present at the time of execution.
The docker logs --follow command will continue streaming the new output from the container's STDOUT and STDERR.
The docker logs listen STDOUT of the container, why I don't see my string just echoed inside it?

Docker engine only stores stdout from process with ID 0 (i.e. the process launched by the CMD directive of your Dockerfile).
By the way, on your Docker host, you can inspect the content of container's logs by viewing the file /var/lib/docker/containers/<ID of your container>/<ID of your container>-json.log.
This file stores logs in JSON format.

I assume logging only occurs for the main process in a container. As exec creates a new process, it won't get logged.
Note that docker logs works for processes given in the run command e.g:
$ ID=$(docker run -d debian sh -c "while true; do echo "hello"; sleep 1; done;")
$ docker logs $ID
hello
hello
hello
hello
hello

Related

Command docker start -a <Container ID> do nothing, "Ubuntu 20.04.02 LTS"

i'm trying to run the start command in docker after creating a container.
for example :
$ docker create busybox echo hi there
it gives me the id of that container like 4e59d0fe8584bb4dcaf44dbce100253b6767bf51546edc27f29f39f52ed57957
and when i try to start that container without any flags like: -a flag
it works, but it only gives me that id back again.
but when i try to show the output using the attach -a flag, actually nothing happened, even it didn't give me an error, simply the command still running without anything happened.
i also couldn't kill the command and stop the execution by Ctrl+c, so the only option i had to close the terminal
i tried to make the problem clear as possible as i can
You can run the image via this command:
docker run -it busybox
It goes you to shell environment and you have -i (interactive) -t (tty) terminal, which means you see the terminal.
The default CMD(PID1) for busybox image is sh, see this.
For docker create busybox echo hi there, the COMMAND becomes echo hi there. This means after container starts, it will first execute echo hi there, then as PID1 exit, the container exit too. If you use docker ps, you won't find your container, you could just find your exited container with docker ps -a.
So,
If you intend to run a one time task, then as the container finish its task, it's normal you could not enter into container anymore.
If you intend to run a daemon task, leave the container service there, you should choose a command which won't finish after run, then your container will still there.
For your case, to quick let you understand it, you could use next to have a quick understanding, use tail -f /dev/null to let the container not exit:
# docker create busybox sh -c "echo hi there; tail -f /dev/null"
840d7c972a96712e48c9aa391aa63638fb10e12307797e338157105bdfb6934e
root#shlava:~# docker start 840d7c972a96712e48c9aa391aa63638fb10e12307797e338157105bdfb6934e
840d7c972a96712e48c9aa391aa63638fb10e12307797e338157105bdfb6934e
root#shlava:~# docker logs 0ae2d689e63a8688213d1eaf285e555ba3d672b8953f0d2730a1897c9d648a26
hi there
root#shlava:~# docker exec -it 0ae2d689e63a8688213d1eaf285e555ba3d672b8953f0d2730a1897c9d648a26 /bin/sh
/ #

docker logs effected by -it parameter in docker run

I have a strange behavior when searching with grep in docker logs which I don't understand.
As an example, I start a jupyter notebook container with following command.
docker run -it -d --rm --name test -p 8888:8888 jupyter/minimal-notebook
With that running Container I can now display the containers log and grep for a part of interest (eg. URL with token of the running jupyter server). This is done with the following Command and shows me the expected result.
docker logs test --tail 5 | grep -ozE "http://127.*"
But when the Container is started without the -it option this will not work and it just prints the whole log message.
Can someone explain this behavior?
The pipe between commands only pipes stdout. If the application inside the container outputs to stderr, this will be displayed bypassing the pipe and grep. You can adjust this by sending stderr to stdout before the pipe with a 2>&1:
docker logs test --tail 5 2>&1 | grep -ozE "http://127.*"

Nothing happends when running docker attach

docker pull httpd
..
docker run -d --name MyWebServer httpd
..
docker attach MyWebServer
(nothing printed here - so I press enter + ls)
ls
(nothing printed here - so I press enter multiple times and still nothing..)
When I try to do the same thing with ubuntu image, it works. Why it isn't working here?
The difference is the running process. With Ubuntu, you are being attached to the sh. With httpd you are being attached to the apache main process, which is not a shell.
Do this instead of attach:
docker exec -it MyWebServer sh
With that, you are launching a shell inside the same container that runs apache process.
Have you read the docs? The attach returns any messages that the container produces. I bet you wanted to do the exec instead.
It’s either
docker exec -it container_name_or_id bash
or
docker-compose exec service_name bash
if you use the docker-compose app.
You can execute any command (unless the container has no fixed entrypoint), for instance
docker exec container ls /

Running dev container exec bash not responding

I have following Dockerfile:
FROM elixir:1.4.5
COPY . /
RUN mix compile
CMD echo "Application started" && elixir --name $MY_POD_NAMESPACE#$MY_POD_IP --no-halt --cookie $ERLANG_COOKIE -S mix run
It starts and runs well, but when I try either attatch or exec XXX bash it does not respond at all.
Both the commands are different as such
docker attach containerid gets your to main process which was running and if it doesn't output anything further then you will not see anything. You should rather use docker logs containerid to see the output of your code
docker exec containerId bash means you want to get to a bash process inside the container. This command would execute and end immediately as you have not specified the interactive and the tty flags. Update it to use it as below
docker exec -it containerId bash
And you should be able to get a bash. If it still doesn't work then use docker stats containerId to see what kind of CPU and memory usage your container has
If docker exec -it container-id bash doesn't work then try docker exec -it container-id sh
Sometimes bash command doesn't work.

How can I keep docker container running?

I want to run multiple containers automatically and create something,
but some images, such as swarm, will automatically stop after run or start.
I already try like that
docker run -d swarm
docker run -d swarm /bin/bash tail -f /dev/null
docker run -itd swarm bash -c "while true; do sleep 1; done"
but 'docker ps' show nothing, and I tried to build Dockerfile by typing:
FROM swarm
ENTRYPOINT ["echo"]
and The image does not run with error message :
docker: Error response from daemon: invalid header field value "oci runtime error: container_linux.go:247: starting container process caused \"exec: \\\"echo\\\": executable file not found in $PATH\"\n".
I can't understand this error... How can I keep swarm container running..?
(Sorry,my English is not good))
using -d is recommended because you can run your container with just one command and you don’t need to detach terminal of container by hitting Ctrl + P + Q.
However, there is a problem with -d option. Your container immediately stops unless the commands are not running on foreground.
Docker requires your command to keep running in the foreground. Otherwise, it thinks that your applications stops and shutdown the container.
The problem is that some application does not run in the foreground.
In this situation, you can add tail -f /dev/null to your command.
By doing this, even if your main command runs in the background, your container doesn’t stop because tail is keep running in the foreground.
docker run -d swarm tail -f /dev/null
docker ps shows container
Now you can attach your container by using docker exec container_name command
or
docker run -d swarm command tail -f /dev/null
First of all you don't want to mix the -i and -d switches. Either you would like to run the container in interactive or detached mode. In your case in detached mode:
docker run -d swarm /bin/bash tail -f /dev/null
There are also no need to allocate a tty using the -t flag, since this only needs to be done in interactive mode.
You should have a look at the Docker run reference
Docker container does two type of task. One is to perform and exit & other is to run it in background.
To run docker container in background, there are few options.
Run using shell. docker run -it <image> /bin/bash
For continuously running container. docker run -d -p 8080:8080 <image>. Assuming image will expose port 8080 and in listening mode.
It's fine to to a tail on /dev/null, but why not make it do something useful?
The following command will reap orphan processes, so no zombie (defunct) precesses are left floating around. Also some init.d / restart scripts doesn't allow this.
exec sh -c 'while true ;do wait ;done'
You are right docker run -itd swarm ( Without give argument for container( bash -c "while true; do sleep 1; done" ) )works fine .If you pass argument for docker run it will run the command and terminates the container.If you want to run the container permanently first start the container with docker run -itd swarm and check if the container runs or not by docker ps now the container runs , if you want to execute any command in container use docker exec -itd container_name command Remember : Only use the command which not stop the container. bash -c "while true; do sleep 1; done this command will stop the container ( Because this is complete command if we execute in normal terminal it execute and terminate , this type of command also terminates the container ).
I Hope this Helps..
Basically this is the method , but your docker image is swarm so it is different and i don't know about swarm docker image and i am not using that .But after i research about that . First i run the docker swarm image it shows.,
After that i understand we run docker swarm image by using only five commands in picture like create, list manage, join, help . if we run swarm image without command like docker run -itd swarm it takes command as --help . Sorry, But i don't know what is the purpose of swarm image. For more usage check https://hub.docker.com/_/swarm/ .
The answer that i added docker run -itd image tail -f /dev/null is not for swarm image , it is for docker images like ubuntu,fedora, centos.
Just read the usage of swarm image and why it is used .
After if you have any issue post your issue in https://github.com/docker/swarm-library-image/issues
Thank You...
have a container running
docker run --rm -d --name=tmp ubuntu sleep infinity
example of requesting a command from the dorment container
docker exec tmp echo hello from container
notes:
--rm removes the container if it is stopped
-d runs the container in the background
--name=tmp name the container so you control how to denote it
ubuntu pushes a light image to exec your commands
sleep infinity keeps the container dorment

Resources