Viewing Docker Logs
- Docker container logs can be viewed by using the command
docker logs <container>
- Lets experiment by executing the jenkins container
docker container run --name jenkins -d -P jenkins
- Now execute the following command
docker container logs jenkins
- Docker Swarm service or task logs can be viewed by using the
docker service logs
- The above two commands shows the command output redirected to STDOUT and STDERR streams
- In some cases the logs of your application might be redirected to the file or an external databases and the above commands will not be useful.
- Majority of the users while creating Docker Image redirect their logs to /dev/stdout. (which is a best practice)
- However docker includes multiple logging mechanisms to get info from running services & containers. These mechanisms are referred as logging drivers.
- Each Docker daemon has a default loggin driver, which every container uses, if the logging driver is not explicitly configured.
- Docker give the options to extend/implement/install logging dirivers. Reference for more info.
- For the complete list of supported logging drivers click here
Resource Constraints
- By default, container has no resource restrictions & it can use as much of a given resource as the host’s kernel allows.
- On Docker container memory, cpu and GPU constraints can be imposed using docker run or docker container run command
Lets start experimentation with following containers
- In these commands we generally follow number by suffix of b,k,m,g to indicate bytes, kilobytes, megabytes, gigabytes respectively.
- Lets create a jenkins container with memory restriction of 512 MB and one without it.
docker container run --name r-mem-jenkins -P -d --memory 512m jenkins
docker container run --name jenkins -P -d jenkins
- Execute
docker stats
and you should see the output as
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
f3ba989220d7 r-mem-jenkins 0.07% 489.8MiB / 512MiB 95.66% 4.01MB / 19.5kB 2.22MB / 23.5MB 34
6cbfdf4ca307 jenkins 0.12% 499.3MiB / 3.794GiB 12.85% 4.01MB / 23kB 0B / 23.5MB 34
## Specific Area of interest
NAME MEM USAGE / LIMIT
r-mem-jenkins 489.8MiB / 512MiB
jenkins 499.3MiB / 3.794GiB
-
similarly the other options like
- –memory-swap
- –memory-swappiness
- –memory-reservation
-
Similarly we can impose restrictions on cpu’s as well. In the following example we would create two containers, in which one container to get a guaranteed to get at the most one cpu"
docker container run --name r-memcpu-jenkins -P -d --cpus="1" --memory 512m jenkins
docker container run --name jenkins -P -d jenkins
- Execute docker stats and observe the output
- There are other command line options for GPU as well.
- Execute
docker container run --help
to explore all the other options available for restrictions