Access log of container running in detached mode

As we know that we can see log or print statement of our code at console. While running the docker image, we see these logs and print statements it depends on how we are running the docker image or container.

When we run a docker image with docker run command, it start in attach mode. So it will show all log/print statements at console.

Usage:  docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

However, we can run a container with docker start command but by default it start in detach mode.

Usage:  docker start [OPTIONS] CONTAINER [CONTAINER...]

But we can start any container in attach mode by passing -a as option in command. Now it will show all print/log statements at console.

Usage:  docker start -a <container_name>

However, if we have started our container or image in detach mode even then we can see the log at console. Using docker logs command we can achieve it.

Usage:  docker logs [OPTIONS] CONTAINER

We can use -f option to print continuous log at console. by default, it will all log and stop.

docker logs -f <container_name>

Leave a Reply