Build a binary program on and executed it in Docker. DockerFile: alpine + golang

FROM golang:1.14-alpine AS build

We are creating our image using the base image golang:alpine. This is basically an image just like what we want to create and is available for us on a Docker repository. This image runs the alpine Linux distribution which is small in size and has Golang already installed which is perfect for our use case. 

From golang:alpine

RUN mkdir /files
COPY hello.go /files
WORKDIR /files

RUN go build -o /files/hello hello.go
ENTRYPOINT [ "/files/hello" ]

create a folder in docker and copy the golang source file to the created folder. Choose the created folder as our WORKDIR. Define Docker RUN to build a binary with go tool (build -o) to use the source file and specify the output. Docker ENTRYPOINT to run the binary file after Docker image has been loaded.

docker build -t go_hello:v1 .

Docker ‘build’ command with ‘-t’ options tag a new name for image and version. Docker ‘images’ command will list show an image name if above docker command has not error of build a new image file.

docker run –rm go_hello:v1

Docker ‘run’ command load the go_hello:v1 into a container and execute the the ENTRYPOINT defined in the Dockerfile.

docker tag go_hello:v1 "shankuo/go_hello:v1"
docker push "shankuo/go_hello:v1"

Docker ‘push’ command delivery the copy of docker image to the Docker Hub.