When running applications using Docker, one of the important things we need to know is how to view container logs. These logs are very useful for checking whether the application is running normally, if there are errors, or if the service inside the container has started successfully.
In this example, we won't use a specific container name. So use a general format like:
container-name
Or, if you want it easier to understand, assume your container name is:
project-name
Later, just replace project-name with your own Docker container name.
To check the names of running containers, use:
docker ps
Look at the NAMES column. That is the container name you can use for Docker logs commands.
1. View Container Logs
To view logs from a container, use the command:
docker logs project-name
Replace project-name with your container name.
General format example:
docker logs container-name
This command will display the logs recorded from that container. These logs usually contain information such as application processes when starting, errors, warnings, or messages indicating that the server is active.
2. View Logs Live
If you want to view logs in real-time or continuously, use the -f option.
docker logs -f project-name
The -f option means follow. So the terminal will keep displaying the latest logs as long as the container is running.
This is very useful when you are debugging. For example, after restarting a container, updating the application, or wanting to see if there are any new errors when the website is accessed.
To exit live log mode, press:
CTRL + C
This only stops the log display in the terminal, not the container itself.
3. View Only Recent Logs
Sometimes container logs are too long, especially if the application has been running for a long time. If you only want to see the last few lines, use --tail.
docker logs --tail=100 project-name
The command above will display the last 100 lines of the container logs.
If you want to see the last 50 lines:
docker logs --tail=50 project-name
If you want to see recent logs while still following new logs, you can combine --tail and -f.
docker logs --tail=100 -f project-name
With this command, Docker will display the last 100 lines, then continue to show new logs live.
4. View Logs by Time
Docker can also display logs based on a specific time. For example, you only want to see logs from the last 10 minutes:
docker logs --since=10m project-name
Or from the last hour:
docker logs --since=1h project-name
If you want to see logs up to a certain time, you can use:
docker logs --until=10m project-name
This feature is useful if you want to focus on finding errors that just occurred, without having to read logs from the beginning.
5. Enter the Container
Besides viewing logs from outside, sometimes you also need to enter the container to check files, folders, or run specific commands.
Use the command:
docker exec -it project-name sh
Usually, lightweight Linux-based containers use sh.
If your Docker image supports bash, you can also use:
docker exec -it project-name bash
Once inside the container, you can check the current folder location with:
pwd
Then list files and folders:
ls
If you want more detail:
ls -la
6. Check Application from Inside the Container
After entering the container, you can check if the application is running by trying to access localhost on the port used by the application.
The general format is:
wget -qO- http://localhost:PORT
Replace PORT with the internal port of the application used inside the container.
Example:
wget -qO- http://localhost:8080
Or if curl is available inside the container, you can use:
curl http://localhost:PORT
Example:
curl http://localhost:8080
If the application is running normally, it will usually display a response such as HTML, JSON, or specific text from the application.
7. Check Application from VPS or Host
Besides from inside the container, you can also check the application directly from the VPS or host server.
The general format:
curl http://localhost:PORT
Replace PORT with the port opened on the VPS or Docker port mapping.
Example:
curl http://localhost:8080
If using Docker, make sure the container port is mapped to the host port. You can check with:
docker ps
You will see the PORTS section, for example:
0.0.0.0:8080->8080/tcp
That means port 8080 on the VPS is directed to port 8080 inside the container.
8. Check Container Name
If you're not sure what the container name is, run:
docker ps
This command will display a list of running containers.
If you want to see all containers, including stopped ones:
docker ps -a
Then you can see the NAMES column, and use that name for the docker logs command.
Example:
docker logs container-name
9. Simple Debugging Flow Example
Typically, when a Docker application has issues, you can start by checking the container first.
First, check if the container is still running:
docker ps
Then view the recent logs:
docker logs --tail=100 project-name
If you want real-time monitoring:
docker logs -f project-name
If you need to enter the container:
docker exec -it project-name sh
Then check the application folder:
pwd
ls -la
Then check if the application responds on the internal port:
wget -qO- http://localhost:PORT
Or:
curl http://localhost:PORT
Conclusion
To view Docker container logs, the most basic command used is:
docker logs project-name
If you want to view logs live:
docker logs -f project-name
If you only want to see the end of the log:
docker logs --tail=100 project-name
And if you need to enter the container:
docker exec -it project-name sh
With these simple commands, you can check the condition of the application in Docker, find errors, see running processes, and ensure the services inside the container are active and working properly.
Written by
Wilan
A regular contributor to Bali Island Tekno who actively shares knowledge about technology, programming, and the world of software engineering.