Loading

Quipoin Menu

Learn • Practice • Grow

docker / Container Logs and Exec
tutorial

Container Logs and Exec

When containers run in the background, you need ways to see their output and interact with them. Two important commands are docker logs and docker exec.

Viewing Container Logs

To see the output (stdout/stderr) of a container:
docker logs container_name
Useful options:
  • --tail 100 – show last 100 lines.
  • -f or --follow – stream logs in real time (like tail -f).
  • --since 10m – show logs from the last 10 minutes.
Example:
docker logs -f --tail 50 webserver

Executing Commands Inside a Running Container

docker exec runs a command in an already running container. It's like SSH into the container, but without SSH.

To get a shell inside:
docker exec -it container_name bash
If the container doesn't have bash, use sh.

Run a one‑off command:
docker exec container_name ls -l

Copying Files Between Host and Container

Although not part of logs/exec, this is useful: docker cp
Copy from container to host:
docker cp container_name:/path/in/container /local/path
Copy from host to container:
docker cp /local/file container_name:/path/

Practical Example

Run an Nginx container in the background:
docker run -d --name webserver -p 8080:80 nginx
View logs:
docker logs webserver
Execute a shell inside and browse the filesystem:
docker exec -it webserver bash
Inside, you can run ls /usr/share/nginx/html to see the default webpage.


Two Minute Drill
  • docker logs shows container output; use -f to follow.
  • docker exec -it container bash opens a shell in a running container.
  • Use docker cp to copy files to/from containers.
  • These commands are essential for debugging and interacting with containers.

Need more clarification?

Drop us an email at career@quipoinfotech.com