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?
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.
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) ...
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.
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> .
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With