# Multistage build na prática

## Laboratório

Aplicação web escrita em Go.

<figure><img src="https://2056950903-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7JJDMtYZqFc5W4Gd6mo1%2Fuploads%2FzycaowwfzdH5T5Zrhvol%2Fimage.png?alt=media&#x26;token=b46a667f-c5b2-4dcf-be11-ee958f177a23" alt=""><figcaption></figcaption></figure>

go.mod:

```
module barbieri.dev/webteste

go 1.18
```

main.go:

```go
package main

import (
	"fmt"
	"net/http"
)

func hello(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "<h1>Página de teste</h1>")
}

func main() {
	http.HandleFunc("/", hello)
	fmt.Println("Servidor rodando na porta 8080")
	http.ListenAndServe(":8080", nil)
}

```

```bash
$ pwd
/tmp/go
$ ls
go.mod  main.go
```

### Execução Local

{% embed url="<https://go.dev/>" %}

```bash
$ sudo apt update
$ sudo apt install golang
$ go run main.go
Servidor rodando na porta 8080
```

<figure><img src="https://2056950903-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7JJDMtYZqFc5W4Gd6mo1%2Fuploads%2F4w7dfSOj4rPVuIiTNe4n%2Fimage.png?alt=media&#x26;token=e1916533-fc77-4627-9551-2c1407029be3" alt=""><figcaption></figcaption></figure>

Criação da imagem de container:

```bash
$ touch Dockerfile.simples
$ code .
```

{% embed url="<https://hub.docker.com/_/golang>" %}

```bash
$ go version
go version go1.18.1 linux/amd64
```

{% embed url="<https://hub.docker.com/layers/library/golang/1.18.1/images/sha256-5b75b529da0f2196ee8561a90e5b99aceee56e125c6ef09a3da4e32cf3cc6c20?context=explore>" %}

O GO é uma linguagem compilada. Faz-se necessário compilar a aplicação para obter o binário.

<figure><img src="https://2056950903-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7JJDMtYZqFc5W4Gd6mo1%2Fuploads%2FDe2soYTxfYiAv99wrMVa%2Fimage.png?alt=media&#x26;token=cd7ff699-da5a-4155-b499-4128a5316da2" alt=""><figcaption></figcaption></figure>

```docker
FROM golang:1.18.1
WORKDIR /app
COPY . .

# compilacao da aplicacao
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .

CMD ["./main"]
```

Construção da imagem:

```bash
$ pwd
/tmp/go
$ ls
Dockerfile.simples  go.mod  main.go
$ docker build -t marcelodpbarbieri/app-multi-stage:simples -f Dockerfile.simples .
[+] Building 9.0s (9/9) FINISHED                             docker:default
 => [internal] load build definition from Dockerfile.simples           0.0s
 => => transferring dockerfile: 200B                                   0.0s
 => [internal] load metadata for docker.io/library/golang:1.18.1       1.1s
 => [internal] load .dockerignore                                      0.0s
 => => transferring context: 2B                                        0.0s
 => [1/4] FROM docker.io/library/golang:1.18.1@sha256:12d3995156cb0dc  0.0s
 => [internal] load build context                                      0.0s
 => => transferring context: 92B                                       0.0s
 => CACHED [2/4] WORKDIR /app                                          0.0s
 => [3/4] COPY . .                                                     0.0s
 => [4/4] RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo  7.6s
 => exporting to image                                                 0.3s
 => => exporting layers                                                0.2s
 => => writing image sha256:8e8245117427e08df5b2125f8d45c3f0dee8b70cd  0.0s
 => => naming to docker.io/marcelodpbarbieri/app-multi-stage:simples   0.0s
$ docker image ls
REPOSITORY                                TAG       IMAGE ID       CREATED          SIZE
marcelodpbarbieri/app-multi-stage         simples   8e8245117427   32 seconds ago   1.02GB
```

Execução do container:

```bash
$ docker container run -d -p 8080:8080 marcelodpbarbieri/app-multi-stage:simples
1f5867bf8e016dc8b8aa85e38a17856ddec35a8028b5aaf76e5a57b578858a4f
$ docker container ls
CONTAINER ID   IMAGE                                       COMMAND    CREATED          STATUS          PORTS                                       NAMES
1f5867bf8e01   marcelodpbarbieri/app-multi-stage:simples   "./main"   41 seconds ago   Up 40 seconds   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   nifty_merkle
```

<figure><img src="https://2056950903-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7JJDMtYZqFc5W4Gd6mo1%2Fuploads%2FJ0C3DkRWxtDpSuPhpOfy%2Fimage.png?alt=media&#x26;token=4109f8fa-7a3e-4d0e-a78e-ce3185dfe421" alt=""><figcaption></figcaption></figure>

> Funcionou, porém a imagem construída possui aproximadamente 1GB de tamanho. Todo o SDK do GoLang está sendo carregado.

Para verificar o tamanho da imagem do GoLang, pode ser feito pelo history da imagem ou com o pull:

```bash
$ docker image history marcelodpbarbieri/app-multi-stage:simples
IMAGE          CREATED         CREATED BY                                      SIZE      COMMENT
8e8245117427   4 minutes ago   CMD ["./main"]                                  0B        buildkit.dockerfile.v0
<missing>      4 minutes ago   RUN /bin/sh -c CGO_ENABLED=0 GOOS=linux go b…   60.1MB    buildkit.dockerfile.v0
<missing>      4 minutes ago   COPY . . # buildkit                             463B      buildkit.dockerfile.v0
<missing>      7 minutes ago   WORKDIR /app                                    0B        buildkit.dockerfile.v0
<missing>      2 years ago     /bin/sh -c #(nop) WORKDIR /go                   0B
<missing>      2 years ago     /bin/sh -c mkdir -p "$GOPATH/src" "$GOPATH/b…   0B
<missing>      2 years ago     /bin/sh -c #(nop)  ENV PATH=/go/bin:/usr/loc…   0B
<missing>      2 years ago     /bin/sh -c #(nop)  ENV GOPATH=/go               0B
<missing>      2 years ago     /bin/sh -c set -eux;  arch="$(dpkg --print-a…   431MB
<missing>      2 years ago     /bin/sh -c #(nop)  ENV GOLANG_VERSION=1.18.1    0B
<missing>      2 years ago     /bin/sh -c #(nop)  ENV PATH=/usr/local/go/bi…   0B
<missing>      2 years ago     /bin/sh -c set -eux;  apt-get update;  apt-g…   228MB
<missing>      2 years ago     /bin/sh -c apt-get update && apt-get install…   152MB
<missing>      2 years ago     /bin/sh -c set -ex;  if ! command -v gpg > /…   19MB
<missing>      2 years ago     /bin/sh -c set -eux;  apt-get update;  apt-g…   10.7MB
```

```bash
$ docker pull golang:1.18.1
1.18.1: Pulling from library/golang
6aefca2dc61d: Already exists
967757d56527: Already exists
c357e2c68cb3: Already exists
c766e27afb21: Already exists
d6a8ea6bd5f8: Already exists
13a9b904f742: Already exists
3447e3508f49: Already exists
Digest: sha256:12d3995156cb0dcdbb9d3edb5827e4e8e1bf5bf92436bfd12d696ec997001a9a
Status: Downloaded newer image for golang:1.18.1
docker.io/library/golang:1.18.1
$ docker image ls
REPOSITORY                                TAG       IMAGE ID       CREATED         SIZE
golang                                    1.18.1    65375c930b21   2 years ago     964MB
```

### Utilizar o Multistage Build

```bash
$ touch Dockerfile.multistage
$ code .
```

{% embed url="<https://hub.docker.com/layers/library/alpine/3.20.2/images/sha256-eddacbc7e24bf8799a4ed3cdcfa50d4b88a323695ad80f317b6629883b2c2a78?context=explore>" %}

Verificação do tamanho das imagens Alpine e Ubuntu:

<pre class="language-bash"><code class="lang-bash">$ docker image pull alpine:3.20.2
3.20.2: Pulling from library/alpine
c6a83fedfae6: Already exists
Digest: sha256:0a4eaa0eecf5f8c050e5bba433f58c052be7587ee8af3e8b3910ef9ab5fbe9f5
Status: Downloaded newer image for alpine:3.20.2
docker.io/library/alpine:3.20.2
<strong>$ docker image ls
</strong>REPOSITORY                                TAG       IMAGE ID       CREATED          SIZE
alpine                                    3.20.2    324bc02ae123   4 weeks ago      7.8MB
</code></pre>

```bash
$ docker image pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
31e907dcc94a: Already exists
Digest: sha256:8a37d68f4f73ebf3d4efafbcf66379bf3728902a8038616808f04e34a9ab63ee
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest
$ docker image ls
REPOSITORY                                TAG       IMAGE ID       CREATED          SIZE
ubuntu                                    latest    edbfe74c41f8   3 weeks ago      78.1MB
```

Dockerfile.multistage:

```docker
# IMAGEM INTERMEDIARIA
# imagem base nomeada como build
FROM golang:1.18.1 AS build
WORKDIR /build
COPY . .
# compilacao da aplicacao
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .

# IMAGEM FINAL
# imagem base nomeada como app
FROM alpine:3.20.2 AS app
WORKDIR /app
# copia do binario da imagem intermediaria (build) para a imagem final
COPY --from=build /build/main .
CMD ["./main"]
```

Construção da imagem:

```bash
$ docker image build -t marcelodpbarbieri/app-multi-stage:multistage -f Dockerfile.multistage . -
[+] Building 7.6s (13/13) FINISHED                                                               docker:default
 => [internal] load build definition from Dockerfile.multistage                                            0.0s
 => => transferring dockerfile: 387B                                                                       0.0s
 => [internal] load metadata for docker.io/library/alpine:3.20.2                                           0.0s
 => [internal] load metadata for docker.io/library/golang:1.18.1                                           0.0s
 => [internal] load .dockerignore                                                                          0.0s
 => => transferring context: 2B                                                                            0.0s
 => [build 1/4] FROM docker.io/library/golang:1.18.1                                                       0.0s
 => [internal] load build context                                                                          0.0s
 => => transferring context: 477B                                                                          0.0s
 => [app 1/3] FROM docker.io/library/alpine:3.20.2                                                         0.0s
 => CACHED [app 2/3] WORKDIR /app                                                                          0.0s
 => CACHED [build 2/4] WORKDIR /build                                                                      0.0s
 => [build 3/4] COPY . .                                                                                   0.0s
 => [build 4/4] RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .                      7.3s
 => [app 3/3] COPY --from=build /build/main .                                                              0.1s
 => exporting to image                                                                                     0.1s
 => => exporting layers                                                                                    0.0s
 => => writing image sha256:067e8f0af08aa2f9ab71c1629ec0979bb0dcfb62c84ed7184440b6a90867c9bc               0.0s
 => => naming to docker.io/marcelodpbarbieri/app-multi-stage:multistage                                    0.0s
$ docker image ls
REPOSITORY                                TAG          IMAGE ID       CREATED          SIZE
marcelodpbarbieri/app-multi-stage         multistage   067e8f0af08a   9 seconds ago    14MB
marcelodpbarbieri/app-multi-stage         simples      8e8245117427   21 minutes ago   1.02GB
```

> &#x20;A imagem agora possui tamanho de 14MB

```bash
$ docker history 067e
IMAGE          CREATED          CREATED BY                                      SIZE      COMMENT
067e8f0af08a   9 minutes ago    CMD ["./main"]                                  0B        buildkit.dockerfile.v0
<missing>      9 minutes ago    COPY /build/main . # buildkit                   6.24MB    buildkit.dockerfile.v0
<missing>      10 minutes ago   WORKDIR /app                                    0B        buildkit.dockerfile.v0
<missing>      4 weeks ago      /bin/sh -c #(nop)  CMD ["/bin/sh"]              0B
<missing>      4 weeks ago      /bin/sh -c #(nop) ADD file:99093095d62d04215…   7.8MB
```

Execução do container:

```bash
$ docker container run -d -p 8080:8080 marcelodpbarbieri/app-multi-stage:multistage
fd52b4647f011f8dca6be2fbc9f9b752caf0babe735acf122d9de2b44fbf61f7
```

<figure><img src="https://2056950903-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7JJDMtYZqFc5W4Gd6mo1%2Fuploads%2FYeIGdk8PGsrwZ2mEWChG%2Fimage.png?alt=media&#x26;token=a8faa06f-9072-400a-a83a-a6f326a03dfa" alt=""><figcaption></figcaption></figure>
