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_nameUseful options:--tail 100– show last 100 lines.-for--follow– stream logs in real time (liketail -f).--since 10m– show logs from the last 10 minutes.
docker logs -f --tail 50 webserverExecuting 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 bashIf the container doesn't have bash, use sh.Run a one‑off command:
docker exec container_name ls -lCopying Files Between Host and Container
Although not part of logs/exec, this is useful:
docker cpCopy from container to host:
docker cp container_name:/path/in/container /local/pathCopy 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 nginxView logs:docker logs webserverExecute a shell inside and browse the filesystem:docker exec -it webserver bashInside, you can run ls /usr/share/nginx/html to see the default webpage.Two Minute Drill
docker logsshows container output; use-fto follow.docker exec -it container bashopens a shell in a running container.- Use
docker cpto 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
