Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between "docker logs" and "docker attach"?

Tags:

docker

They both allow you to connect to the stdout/stderr of a running container. In particular docker logs --follow seems to work similarly to docker attach.

Is one command an obsolete version of the other, or are there significant differences?

like image 696
danidiaz Avatar asked Oct 01 '14 20:10

danidiaz


People also ask

What is docker attach?

Description. Use docker attach to attach your terminal's standard input, output, and error (or any combination of the three) to a running container using the container's ID or name. This allows you to view its ongoing output or to control it interactively, as though the commands were running directly in your terminal.

What is the difference between docker attach and Docker exec?

docker exec executes a new command / create a new process in the container's environment, while docker attach just connects the standard input/output/error of the main process(with PID 1) inside the container to corresponding standard input/output/error of current terminal(the terminal you are using to run the command) ...

What is docker log?

The docker logs command shows information logged by a running container. The docker service logs command shows information logged by all containers participating in a service. The information that is logged and the format of the log depends almost entirely on the container's endpoint command.

What is attach and detach mode in docker?

In this mode, the console you are using to execute docker run will be attached to standard input, output and error. That means your console is attached to the container's process. In detached mode, you can follow the standard output of your docker container with docker logs -f <container_ID> .


1 Answers

docker logs just pipes you stderr/stdout, while attach attaches stdin/out/err and proxies signals.

For example, the docs for attach (https://docs.docker.com/engine/reference/commandline/attach/#description) state "You can detach from the container again (and leave it running) with CTRL-p CTRL-q (for a quiet exit), or CTRL-c which will send a SIGKILL to the container, or CTRL-\ to get a stacktrace of the Docker client when it quits. When you detach from the container's process the exit code will be returned to the client."

If the container has a pty I would assume you get attached to the pty and have full pty features (haven't tested that).

Hope this helps a bit!

like image 97
TvE Avatar answered Oct 16 '22 17:10

TvE