{"id":13442959,"url":"https://github.com/montanaflynn/golang-docker-cache","last_synced_at":"2025-03-22T04:30:31.369Z","repository":{"id":37499647,"uuid":"275760602","full_name":"montanaflynn/golang-docker-cache","owner":"montanaflynn","description":"Improved docker Golang module dependency cache for faster builds.","archived":false,"fork":false,"pushed_at":"2021-02-06T01:22:05.000Z","size":8,"stargazers_count":69,"open_issues_count":2,"forks_count":3,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-01T12:48:34.850Z","etag":null,"topics":["benchmark","build","devops","docker","golang","performance"],"latest_commit_sha":null,"homepage":"https://github.com/golang/go/issues/27719","language":"Dockerfile","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/montanaflynn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-06-29T07:23:34.000Z","updated_at":"2024-11-14T15:40:58.000Z","dependencies_parsed_at":"2022-08-26T11:40:44.908Z","dependency_job_id":null,"html_url":"https://github.com/montanaflynn/golang-docker-cache","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/montanaflynn%2Fgolang-docker-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/montanaflynn%2Fgolang-docker-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/montanaflynn%2Fgolang-docker-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/montanaflynn%2Fgolang-docker-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/montanaflynn","download_url":"https://codeload.github.com/montanaflynn/golang-docker-cache/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244181399,"owners_count":20411601,"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":["benchmark","build","devops","docker","golang","performance"],"created_at":"2024-07-31T03:01:53.874Z","updated_at":"2025-03-22T04:30:31.033Z","avatar_url":"https://github.com/montanaflynn.png","language":"Dockerfile","funding_links":[],"categories":["Dockerfile"],"sub_categories":[],"readme":"# Golang Docker Dependency Cache\n\nIf you've switched to go modules you might have found your docker builds have slowed down due `go build` needing to recompile all the dependencies even if they have already been downloaded or vendored. \n\nFor the simple program in this repo with a single dependency I was able to get 4x improvement over doing no dependency caching and 3x compared to using `go mod download` as the dependency cache. \n\nThere is an [open issue](https://github.com/golang/go/issues/27719) but until it's built into the `go` cli this could make your builds much faster.\n\n## Usage\n\n```\nCOPY go.mod go.sum ./\n\n# Add this line before `go build`\nRUN go mod graph | awk '{if ($1 !~ \"@\") print $2}' | xargs go get\n```\n\nFull Dockerfile example: [./Dockerfile](./Dockerfile)\n\n## Benchmarks\n\nI used `time` to measure how long it took to build a fresh docker image with `--no-cache` and then changed the main.go file by adding a comment so Docker would run the `go build` step again while keeping the preceeding cached docker layers. \n\n#### With `go mod graph | awk '{if ($1 !~ \"@\") print $2}' | xargs go get` dependency cache:\n\n```\n# Fresh build with no cache:\n$ time docker build --no-cache -t golang-docker-cache .\n...\n13.54 real         0.12 user         0.07 sys\n\n# Add comment to main.go:\n$ time docker build -t golang-docker-cache .\n2.74 real         0.12 user         0.10 sys\n```\n\n#### With `go mod download` dependency cache:\n\n```\n# Fresh build with no cache:\n$ time docker build --no-cache -t golang-docker-cache .\n15.13 real         0.13 user         0.09 sys\n\n# Add comment to main.go:\n$ time docker build -t golang-docker-cache .\n9.03 real         0.12 user         0.10 sys\n```\n\n#### Without any dependency cache:\n\n```\n# Fresh build with no cache:\n$ time docker build --no-cache -t golang-docker-cache .\n12.12 real         0.12 user         0.08 sys\n\n# Add comment to main.go:\n$ time docker build -t golang-docker-cache .\n12.19 real         0.12 user         0.10 sys\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmontanaflynn%2Fgolang-docker-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmontanaflynn%2Fgolang-docker-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmontanaflynn%2Fgolang-docker-cache/lists"}