{"id":14068189,"url":"https://github.com/ColinFay/dockerstats","last_synced_at":"2025-07-30T02:33:28.598Z","repository":{"id":134241465,"uuid":"254373677","full_name":"ColinFay/dockerstats","owner":"ColinFay","description":"R Wrapper Around 'docker stats'","archived":false,"fork":false,"pushed_at":"2020-05-15T21:37:09.000Z","size":281,"stargazers_count":7,"open_issues_count":0,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-04-25T02:42:09.200Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ColinFay.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-04-09T13:06:22.000Z","updated_at":"2024-06-10T08:49:19.542Z","dependencies_parsed_at":null,"dependency_job_id":"527818c4-2e07-4c50-af09-2f6866672abf","html_url":"https://github.com/ColinFay/dockerstats","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ColinFay%2Fdockerstats","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ColinFay%2Fdockerstats/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ColinFay%2Fdockerstats/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ColinFay%2Fdockerstats/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ColinFay","download_url":"https://codeload.github.com/ColinFay/dockerstats/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228075778,"owners_count":17865549,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-13T07:06:00.814Z","updated_at":"2024-12-04T08:31:45.631Z","avatar_url":"https://github.com/ColinFay.png","language":"R","readme":"---\noutput: github_document\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n```{r, include = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/README-\",\n  out.width = \"100%\"\n)\n```\n\n# dockerstats\n\n\u003c!-- badges: start --\u003e\n\u003c!-- badges: end --\u003e\n\n`{dockerstats}` is a small wrapper around `docker stats` that returns the output of this command as an R data.frame. \n\nNote that this package calls `system(\"docker stats\")` so you should be able to do this from your R command line. I'll probably refactor that at some point. \n\n## Installation\n\nYou can install the released version of `{dockerstats}` from GitHub with:\n\n``` r\nremotes::install_github(\"ColinFay/dockerstats\")\n```\n\n## How to \n\n```{r}\nlibrary(dockerstats)\n```\n\n\n### dockerstats()\n\nBy default, `dockerstats()` returns the stats for running containers. \n\n```{r}\ndockerstats()\n```\n\nYou can return stats for all containers (not just running)\n\n```{r}\ndockerstats(all = TRUE)\n```\n\nOr from a subset of containers:\n\n```{r}\ndockerstats(\"mongo\", \"proxy\")\n```\n\nThe `extra` param is used to add extra information to the recording, which can be usefull is you want to tag the specific recording. \n\nFor example, here we mimic a connection on the `hexmake` container\n\n```{r echo = FALSE}\nsystem(\"docker run --name hexmake --rm -p 2811:80 colinfay/hexmake\", wait = FALSE)\nSys.sleep(5)\n```\n\n```{r eval = FALSE}\nsystem(\"docker run --name hexmake --rm -p 2811:80 colinfay/hexmake\", wait = FALSE)\n\n```\n\n\n\n```{r, eval = FALSE}\nlibrary(crrri)\nchrome \u003c- Chrome$new(\n  bin = pagedown::find_chrome(), \n  debug_port = httpuv::randomPort()\n)\n\nclient \u003c- chrome$connect(callback = function(client) {\n  Page \u003c- client$Page\n  Page$navigate(url = \"http://localhost:2811\")\n  print({\n    dockerstats::dockerstats(\"hexmake\", extra = \"Connection via chrome\")\n  })\n})\n\nchrome$close()\n```\n\n\n### dockerstats_recurse\n\n`dockerstats_recurse()` is a wrapper around `dockerstats()` that runs every `every` seconds, calling the `callback` function everytime a loop is completed. \n\nBy default, the callback is `print`, but you can define your own. For example, this function will run the `dockerstats()` fun every 2 seconds and save it to a file. \n\n```{r eval = FALSE}\ndockerstats_recurse(\n  \"hexmake\",\n  callback = function(res){\n    print(\n      paste(\"Mem usage: \", res$MemUsage)\n    )\n    write.table(\n      res, \n      \"dockerstats.csv\", \n      append = TRUE, \n      col.names = FALSE, \n      row.names = FALSE, \n      sep = \",\"\n    )\n  }\n)\n```\n\nAs this is a pretty common use-case, a wrapper for this is implemented: \n\n```{r eval = FALSE}\ndockerstats_recurse(\n  \"hexmake\",\n  callback = append_csv(\"dockerstats.csv\", print = TRUE)\n)\n```\n\nKill the container once done\n\n```{r}\nsystem(\"docker kill hexmake\")\n```\n\n\n## Byte conversions\n\nTo handle the `MemUsage` columns, expressed as `byte`, `{dockerstats}` provides a series of functions to convert to byte, kib, mib or gib. \n\nThe unit is kept in `attr(\"units\")` of the result\n\n```{r}\ndock_stats \u003c- dockerstats()\ndock_stats$MemUsag\n# Convert to kib\nto_kib(dock_stats$MemUsag)\n```\n\n\n## How columns are transformed\n\n+ `CPUPerc` and `MemPerc` are stripped from the trailing `%`, and are turned into numeric\n\n+ `MEM USAGE / LIMIT` is splitted into two columns, `MemUsage` and `MemLimit\n\n+ `NET I/O` and `BLOCK I/O` are splitted into two columns, respectively `NetI` \u0026 `NetO`, and `BlockI` \u0026 `BlockO`\n\n## Manipulate columns expressed in bite size\n\nYou can call `as_fs_byte()` from the `{fs}` package to manipulate the columns which are expressed in bytes. \n\n```{r}\ndock_stats \u003c- dockerstats()\n\ndock_stats$MemUsage \u003c- to_kib(dock_stats$MemUsag)\n```\n\n```{r}\nlibrary(ggplot2)\nggplot(\n  dock_stats, \n  aes(\n    reorder(Name, MemUsage), \n    MemUsage\n  )\n) + \n  geom_col() +\n  scale_y_continuous(labels = scales::label_bytes(units = \"kiB\")) + \n  coord_flip() + \n  labs(\n    title = \"MemUsage of running containers\", \n    y = \"MemUsage in kiB\", \n    x = \"Containers\"\n  )\n```\n","funding_links":[],"categories":["R"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FColinFay%2Fdockerstats","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FColinFay%2Fdockerstats","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FColinFay%2Fdockerstats/lists"}