Docker Logs
Descrição
O comando docker logs
mostra a saída padrão (stdout
) e a saída de erro padrão (stderr
) de um container específico.
Uso
docker logs [OPTIONS] CONTAINER
Exemplo de Uso
docker logs -f my_container
Exemplo de Saída
2024-09-07 15:00:00 INFO Starting application...
2024-09-07 15:01:00 ERROR Failed to connect to database
Opções Comuns
--follow
,-f
: Seguir os logs em tempo real, útil para ver novos logs à medida que são gerados.--tail
: Exibir apenas as últimas N linhas dos logs, por exemplo,--tail 100
para ver as últimas 100 linhas.
Quando Usar
Diagnóstico de Aplicações: Para verificar mensagens de erro ou informações de debug geradas por aplicações no container.
Verificação de Comportamento: Útil para monitorar o comportamento da aplicação e detectar problemas.
Laboratório
Ao criar o container nginx em modo interativo os logs são exibidos diretamente no terminal
$ docker container run -p 8080:80 nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2024/09/07 18:26:14 [notice] 1#1: using the "epoll" event method
2024/09/07 18:26:14 [notice] 1#1: nginx/1.27.1
2024/09/07 18:26:14 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14)
2024/09/07 18:26:14 [notice] 1#1: OS: Linux 5.15.146.1-microsoft-standard-WSL2
2024/09/07 18:26:14 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2024/09/07 18:26:14 [notice] 1#1: start worker processes
2024/09/07 18:26:14 [notice] 1#1: start worker process 30
2024/09/07 18:26:14 [notice] 1#1: start worker process 31
2024/09/07 18:26:14 [notice] 1#1: start worker process 32
2024/09/07 18:26:14 [notice] 1#1: start worker process 33
2024/09/07 18:26:14 [notice] 1#1: start worker process 34
2024/09/07 18:26:14 [notice] 1#1: start worker process 35
2024/09/07 18:26:14 [notice] 1#1: start worker process 36
2024/09/07 18:26:14 [notice] 1#1: start worker process 37
2024/09/07 18:26:14 [notice] 1#1: start worker process 38
2024/09/07 18:26:14 [notice] 1#1: start worker process 39
2024/09/07 18:26:14 [notice] 1#1: start worker process 40
2024/09/07 18:26:14 [notice] 1#1: start worker process 41
$ curl http://localhost:8080
172.17.0.1 - - [07/Sep/2024:18:26:53 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.81.0" "-"
Execução em modo detached:
$ docker container run -d -p 8080:80 nginx
9ba71cfedb2b114b2212fc4762b8ac52ca76bedfde8bb18d93c720c7a5c1e418
$ docker container logs 9ba71cfedb2b114b2212fc4762b8ac52ca76bedfde8bb18d93c720c7a5c1e418
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2024/09/07 18:28:30 [notice] 1#1: using the "epoll" event method
2024/09/07 18:28:30 [notice] 1#1: nginx/1.27.1
2024/09/07 18:28:30 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14)
2024/09/07 18:28:30 [notice] 1#1: OS: Linux 5.15.146.1-microsoft-standard-WSL2
2024/09/07 18:28:30 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2024/09/07 18:28:30 [notice] 1#1: start worker processes
2024/09/07 18:28:30 [notice] 1#1: start worker process 29
2024/09/07 18:28:30 [notice] 1#1: start worker process 30
2024/09/07 18:28:30 [notice] 1#1: start worker process 31
2024/09/07 18:28:30 [notice] 1#1: start worker process 32
2024/09/07 18:28:30 [notice] 1#1: start worker process 33
2024/09/07 18:28:30 [notice] 1#1: start worker process 34
2024/09/07 18:28:30 [notice] 1#1: start worker process 35
2024/09/07 18:28:30 [notice] 1#1: start worker process 36
2024/09/07 18:28:30 [notice] 1#1: start worker process 37
2024/09/07 18:28:30 [notice] 1#1: start worker process 38
2024/09/07 18:28:30 [notice] 1#1: start worker process 39
2024/09/07 18:28:30 [notice] 1#1: start worker process 40
$ curl http://localhost:8080
$ docker container logs 9ba71cfedb2b114b2212fc4762b8ac52ca76bedfde8bb18d93c720c7a5c1e418
...
172.17.0.1 - - [07/Sep/2024:18:29:38 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.81.0" "-"
Usos comuns:
# Segue os logs em tempo real
$ docker container logs --follow <id>
# Mostra os logs dos últimos 1 minuto
$ docker container logs --since 1m <id>
# Mostra os logs até 1 minuto atrás
$ docker container logs --until 1m <id>
# Mostra os logs desde a data e hora especificadas
$ docker container logs --since '2024-09-07T20:02:00Z' <id>
# Mostra as últimas 3 linhas dos logs
$ docker container logs --tail 3 <id>
# Mostra as últimas 10 linhas dos logs
$ docker container logs --tail 10 <id>
Last updated