The container logs command
When you run a container in the foreground, all of the output the container sends to standard output and standard error is displayed in the console for the session that ran the container.
However, when you use the –detach parameter, control of the session is returned as soon as the container starts so you don’t see the data sent to stdout and stderr.
If you want to see that data, you use the container logs command.
That command looks like this:
# the long form of the command
# Usage: docker container logs [OPTIONS] CONTAINER
docker container logs --follow --timestamps web-server
# the short form of the command
docker container logs -f -t web-server
# get just the last 5 lines (there is no short form for the "--tail"
docker container logs --tail 5 web-server
# the old syntax
docker logs web-server
The –details, –follow, –timestamps, and –tail parameters are all optional, but I have included them here for reference. When you use the container logs command with no optional parameters, it will just dump all of the contents of the container’s logs to the console. You can use the –tail parameter with a number to dump just the last number of lines. You can combine the parameters (except for –tail and –follow) to get the results you want. The –follow parameter is like using a tail -f command when viewing logs that are being continually written to, and will display each line as it is written to the log.
You use Ctrl + C to exit the log being followed. The –timestamps parameter is great for evaluating the frequency at which lines have been written to the container’s logs.