[{"data":1,"prerenderedAt":645},["ShallowReactive",2],{"blog-en-\u002Fblog\u002Fmulti-stage-build-go":3},{"id":4,"title":5,"body":6,"date":633,"description":634,"extension":635,"meta":636,"navigation":97,"path":637,"seo":638,"stem":639,"tags":640,"__hash__":644},"blog_en\u002Fblog\u002Fmulti-stage-build-go.md","Docker multi-stage build for a Go API",{"type":7,"value":8,"toc":625},"minimark",[9,13,163,168,185,190,204,208,227,234,257,277,294,346,395,431,435,439,447,452,478,483,501,506,526,531,596,601,621],[10,11,12],"p",{},"A multi-stage Dockerfile for compiling and packaging a Go API: a build stage with the Go toolchain, and a minimal final stage that only carries the compiled binary, ready for production.",[14,15,20],"pre",{"className":16,"code":17,"language":18,"meta":19,"style":19},"language-dockerfile shiki shiki-themes darcula","# build stage\nFROM golang:1.21-alpine AS build\nWORKDIR \u002Fsrc\nCOPY go.mod go.sum .\u002F\nRUN go mod download\nCOPY . .\nRUN CGO_ENABLED=0 GOOS=linux go build -o \u002Fapp .\u002Fcmd\u002Fserver\n\n# final stage\nFROM alpine:3.18\nRUN adduser -D appuser\nUSER appuser\nCOPY --from=build \u002Fapp \u002Fapp\nEXPOSE 8080\nENTRYPOINT [\"\u002Fapp\"]\n","dockerfile","",[21,22,23,32,49,58,67,76,84,92,99,105,113,121,130,138,147],"code",{"__ignoreMap":19},[24,25,28],"span",{"class":26,"line":27},"line",1,[24,29,31],{"class":30},"srRes","# build stage\n",[24,33,35,39,43,46],{"class":26,"line":34},2,[24,36,38],{"class":37},"sYMgc","FROM",[24,40,42],{"class":41},"sE2CK"," golang:1.21-alpine ",[24,44,45],{"class":37},"AS",[24,47,48],{"class":41}," build\n",[24,50,52,55],{"class":26,"line":51},3,[24,53,54],{"class":37},"WORKDIR",[24,56,57],{"class":41}," \u002Fsrc\n",[24,59,61,64],{"class":26,"line":60},4,[24,62,63],{"class":37},"COPY",[24,65,66],{"class":41}," go.mod go.sum .\u002F\n",[24,68,70,73],{"class":26,"line":69},5,[24,71,72],{"class":37},"RUN",[24,74,75],{"class":41}," go mod download\n",[24,77,79,81],{"class":26,"line":78},6,[24,80,63],{"class":37},[24,82,83],{"class":41}," . .\n",[24,85,87,89],{"class":26,"line":86},7,[24,88,72],{"class":37},[24,90,91],{"class":41}," CGO_ENABLED=0 GOOS=linux go build -o \u002Fapp .\u002Fcmd\u002Fserver\n",[24,93,95],{"class":26,"line":94},8,[24,96,98],{"emptyLinePlaceholder":97},true,"\n",[24,100,102],{"class":26,"line":101},9,[24,103,104],{"class":30},"# final stage\n",[24,106,108,110],{"class":26,"line":107},10,[24,109,38],{"class":37},[24,111,112],{"class":41}," alpine:3.18\n",[24,114,116,118],{"class":26,"line":115},11,[24,117,72],{"class":37},[24,119,120],{"class":41}," adduser -D appuser\n",[24,122,124,127],{"class":26,"line":123},12,[24,125,126],{"class":37},"USER",[24,128,129],{"class":41}," appuser\n",[24,131,133,135],{"class":26,"line":132},13,[24,134,63],{"class":37},[24,136,137],{"class":41}," --from=build \u002Fapp \u002Fapp\n",[24,139,141,144],{"class":26,"line":140},14,[24,142,143],{"class":37},"EXPOSE",[24,145,146],{"class":41}," 8080\n",[24,148,150,153,156,160],{"class":26,"line":149},15,[24,151,152],{"class":37},"ENTRYPOINT",[24,154,155],{"class":41}," [",[24,157,159],{"class":158},"sZ3PO","\"\u002Fapp\"",[24,161,162],{"class":41},"]\n",[164,165,167],"h2",{"id":166},"build-stage-vs-final-stage-multi-stage-build","Build stage vs final stage (multi-stage build)",[169,170,171,179],"ul",{},[172,173,174,178],"li",{},[175,176,177],"strong",{},"Build stage"," is where the code compiles: it has all the necessary tools (Go toolchain, dependencies, compilation).",[172,180,181,184],{},[175,182,183],{},"Final stage"," only takes the compiled result (the binary) and creates the image that goes to production, usually minimal, secure and small.",[186,187,189],"h3",{"id":188},"advantages-of-multi-stage-builds","Advantages of multi-stage builds",[169,191,192,195,198,201],{},[172,193,194],{},"Much smaller final image (no toolchain, cache, sources, etc).",[172,196,197],{},"Smaller attack surface (fewer packages, fewer vulnerabilities).",[172,199,200],{},"Separates responsibilities (compiling vs running).",[172,202,203],{},"Makes reproducibility and dependency caching easier.",[164,205,207],{"id":206},"line-by-line-explanation-of-the-structure","Line-by-line explanation of the structure",[14,209,211],{"className":16,"code":210,"language":18,"meta":19,"style":19},"# build stage\nFROM golang:1.21-alpine AS build\n",[21,212,213,217],{"__ignoreMap":19},[24,214,215],{"class":26,"line":27},[24,216,31],{"class":30},[24,218,219,221,223,225],{"class":26,"line":34},[24,220,38],{"class":37},[24,222,42],{"class":41},[24,224,45],{"class":37},[24,226,48],{"class":41},[10,228,229,230,233],{},"Base image for compiling. Has Go installed. ",[21,231,232],{},"AS build"," gives the stage a name so it can be referenced later.",[14,235,237],{"className":16,"code":236,"language":18,"meta":19,"style":19},"WORKDIR \u002Fsrc\nCOPY go.mod go.sum .\u002F\nRUN go mod download\n",[21,238,239,245,251],{"__ignoreMap":19},[24,240,241,243],{"class":26,"line":27},[24,242,54],{"class":37},[24,244,57],{"class":41},[24,246,247,249],{"class":26,"line":34},[24,248,63],{"class":37},[24,250,66],{"class":41},[24,252,253,255],{"class":26,"line":51},[24,254,72],{"class":37},[24,256,75],{"class":41},[169,258,259,274],{},[172,260,261,262,265,266,269,270,273],{},"Copies only the dependency files first to take advantage of caching: when ",[21,263,264],{},"go.mod","\u002F",[21,267,268],{},"go.sum"," don't change, ",[21,271,272],{},"go mod download"," uses the cache and Docker doesn't rerun the expensive steps.",[172,275,276],{},"This speeds up iterative builds.",[14,278,280],{"className":16,"code":279,"language":18,"meta":19,"style":19},"COPY . .\nRUN CGO_ENABLED=0 GOOS=linux go build -o \u002Fapp .\u002Fcmd\u002Fserver\n",[21,281,282,288],{"__ignoreMap":19},[24,283,284,286],{"class":26,"line":27},[24,285,63],{"class":37},[24,287,83],{"class":41},[24,289,290,292],{"class":26,"line":34},[24,291,72],{"class":37},[24,293,91],{"class":41},[169,295,296,302],{},[172,297,298,301],{},[21,299,300],{},"COPY . ."," copies the source code.",[172,303,304,305,307,308],{},"The ",[21,306,72],{}," line compiles:\n",[169,309,310,320,326,336],{},[172,311,312,315,316,319],{},[21,313,314],{},"CGO_ENABLED=0"," disables CGO — it forces Go to produce a ",[175,317,318],{},"static"," binary (no C dependencies). Makes it easy to run on distroless\u002Fscratch\u002FAlpine images without extra libraries.",[172,321,322,325],{},[21,323,324],{},"GOOS=linux"," guarantees the binary targets Linux (useful when compiling on Mac\u002FWindows).",[172,327,328,331,332,335],{},[21,329,330],{},"-o \u002Fapp"," places the executable at ",[21,333,334],{},"\u002Fapp",".",[172,337,338,341,342,345],{},[21,339,340],{},".\u002Fcmd\u002Fserver"," is the project's ",[21,343,344],{},"main"," package (should be adjusted to fit the project's needs).",[14,347,349],{"className":16,"code":348,"language":18,"meta":19,"style":19},"# final stage\nFROM alpine:3.18\nRUN adduser -D appuser\nUSER appuser\nCOPY --from=build \u002Fapp \u002Fapp\nEXPOSE 8080\nENTRYPOINT [\"\u002Fapp\"]\n",[21,350,351,355,361,367,373,379,385],{"__ignoreMap":19},[24,352,353],{"class":26,"line":27},[24,354,104],{"class":30},[24,356,357,359],{"class":26,"line":34},[24,358,38],{"class":37},[24,360,112],{"class":41},[24,362,363,365],{"class":26,"line":51},[24,364,72],{"class":37},[24,366,120],{"class":41},[24,368,369,371],{"class":26,"line":60},[24,370,126],{"class":37},[24,372,129],{"class":41},[24,374,375,377],{"class":26,"line":69},[24,376,63],{"class":37},[24,378,137],{"class":41},[24,380,381,383],{"class":26,"line":78},[24,382,143],{"class":37},[24,384,146],{"class":41},[24,386,387,389,391,393],{"class":26,"line":86},[24,388,152],{"class":37},[24,390,155],{"class":41},[24,392,159],{"class":158},[24,394,162],{"class":41},[169,396,397,403,410,420,426],{},[172,398,399,402],{},[21,400,401],{},"FROM alpine"," starts a new, lean image.",[172,404,405,406,409],{},"Creates\u002Fuses a non-root user (",[21,407,408],{},"appuser",") — a security practice.",[172,411,412,415,416,419],{},[21,413,414],{},"COPY --from=build \u002Fapp \u002Fapp"," copies the compiled binary from the ",[21,417,418],{},"build"," stage.",[172,421,422,425],{},[21,423,424],{},"EXPOSE 8080"," documents the port.",[172,427,428,430],{},[21,429,152],{}," defines the execution command.",[186,432,434],{"id":433},"important-technical-notes","Important technical notes",[10,436,437],{},[175,438,314],{},[169,440,441,444],{},[172,442,443],{},"Makes Go link statically. Good for using minimal images.",[172,445,446],{},"If some C library is needed (e.g., SQLite with cgo), you can't disable CGO without losing functionality.",[10,448,449],{},[175,450,451],{},"Alpine + static binary",[169,453,454,460],{},[172,455,456,457,459],{},"With ",[21,458,314],{},", a static binary generally runs fine on Alpine.",[172,461,462,463,466,467,470,471,265,474,477],{},"If the binary uses TLS\u002FCA, the ",[21,464,465],{},"alpine"," image may need ",[21,468,469],{},"ca-certificates"," for HTTPS requests. On ",[21,472,473],{},"scratch",[21,475,476],{},"distroless",", you sometimes need to install CA certs or use variants that already have them.",[10,479,480],{},[175,481,482],{},"Even smaller alternatives",[169,484,485,491],{},[172,486,487,490],{},[21,488,489],{},"FROM scratch"," (an empty image) is the smallest possible — it only works if the binary is truly static and doesn't need certs\u002FLD_LIBRARY.",[172,492,493,496,497,500],{},[21,494,495],{},"gcr.io\u002Fdistroless\u002Fcc"," or ",[21,498,499],{},"distroless\u002Fstatic"," are great options for production.",[10,502,503],{},[175,504,505],{},"Cache and speed",[169,507,508,519],{},[172,509,510,511,514,515,518],{},"The Dockerfile above already uses the ",[21,512,513],{},"COPY go.mod go.sum"," before ",[21,516,517],{},"COPY ."," technique — that's ideal.",[172,520,521,522,525],{},"For even faster builds in CI, use BuildKit with module cache mounts (",[21,523,524],{},"--mount=type=cache",") or Go module caching.",[10,527,528],{},[175,529,530],{},"Binary size",[169,532,533],{},[172,534,535,536,539,540,543,544,547,548,551,552],{},"Check it with ",[21,537,538],{},"docker build"," and ",[21,541,542],{},"docker image ls",", or ",[21,545,546],{},"du -h"," on the binary. Stripping and ",[21,549,550],{},"ldflags \"-s -w\""," reduce the size:",[14,553,557],{"className":554,"code":555,"language":556,"meta":19,"style":19},"language-bash shiki shiki-themes darcula","RUN CGO_ENABLED=0 GOOS=linux go build -ldflags \"-s -w\" -o \u002Fapp .\u002Fcmd\u002Fserver\n","bash",[21,558,559],{"__ignoreMap":19},[24,560,561,564,567,571,574,577,580,584,587,590,593],{"class":26,"line":27},[24,562,72],{"class":563},"s2lgc",[24,565,566],{"class":158}," CGO_ENABLED=",[24,568,570],{"class":569},"sZM-y","0",[24,572,573],{"class":158}," GOOS=linux",[24,575,576],{"class":158}," go",[24,578,579],{"class":158}," build",[24,581,583],{"class":582},"sk7qV"," -ldflags",[24,585,586],{"class":158}," \"-s -w\"",[24,588,589],{"class":582}," -o",[24,591,592],{"class":158}," \u002Fapp",[24,594,595],{"class":158}," .\u002Fcmd\u002Fserver\n",[10,597,598],{},[175,599,600],{},"Local debugging",[169,602,603],{},[172,604,605,606,609,610,612,613,617,618,620],{},"To debug on your machine: ",[21,607,608],{},"docker run --rm -it my-api:dev \u002Fbin\u002Fsh"," won't work if the final image only has the binary. Use a debug stage, run the ",[21,611,418],{}," image directly, or create a ",[614,615,616],"em",{},"debug"," image with ",[21,619,556],{}," installed.",[622,623,624],"style",{},"html pre.shiki code .srRes, html code.shiki .srRes{--shiki-default:#808080}html pre.shiki code .sYMgc, html code.shiki .sYMgc{--shiki-default:#CC7832}html pre.shiki code .sE2CK, html code.shiki .sE2CK{--shiki-default:#A9B7C6}html pre.shiki code .sZ3PO, html code.shiki .sZ3PO{--shiki-default:#6A8759}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html pre.shiki code .s2lgc, html code.shiki .s2lgc{--shiki-default:#FFC66D}html pre.shiki code .sZM-y, html code.shiki .sZM-y{--shiki-default:#6897BB}html pre.shiki code .sk7qV, html code.shiki .sk7qV{--shiki-default:#9876AA}",{"title":19,"searchDepth":34,"depth":34,"links":626},[627,630],{"id":166,"depth":34,"text":167,"children":628},[629],{"id":188,"depth":51,"text":189},{"id":206,"depth":34,"text":207,"children":631},[632],{"id":433,"depth":51,"text":434},"2025-10-26","How Docker's multi-stage build separates compiling a Go API from the final image: build stage, static binaries and security practices.","md",{},"\u002Fblog\u002Fmulti-stage-build-go",{"title":5,"description":634},"blog\u002Fmulti-stage-build-go",[641,642,643],"docker","go","devops","Wj4UwHWJX_S-0aDlfBRi9cP2jE96-vOsCj4uwhCTOJY",1794928449485]