Saturday, July 14, 2018

Docker Multi-Stages

multistages

Docker Build with Multi-Stages

Here is the data structure.

root@dockerce:~/dockerbuild1# tree
.
├── build.sh
├── Dockerfile
├── README.md
├── run.sh
└── src
    ├── main.go
    └── test_test.go

The Dockerfile.


# build stage 1
FROM golang:alpine AS unittest-stage
ADD ./src /src
RUN cd /src && go test ./...

# build stage 2
FROM golang:alpine AS build-stage
COPY --from=unittest-stage /src/ /src/
RUN cd /src && go build -o goapp

# final stage
FROM alpine
WORKDIR /app
COPY --from=build-stage /src/goapp /app/
ENTRYPOINT ./goapp
docker build -t jj/bd2:0.2 .

Source Code

main.go

package main

import (
    "fmt"
    "time"
)

func testit() int {
    fmt.Println("test on function testit")
    return 2
}

func main() {
    fmt.Println("running on go ya")
    testit()
    for {
        time.Sleep(time.Second * 1)
    }
}

test_test.go

package main

import (
        //"fmt"
        "testing"
)

func Test_AmILeader(t *testing.T) {
        aa := testit()
        if aa != 2 {
                t.Error("not eq 2")
        } else {
                t.Log("ha")
        }

}

No comments:

Post a Comment