Using Docker

In the last section of this chapter, you will learn how to use a Docker image in order to compile and execute your Go code inside the Docker image.

As you might already know, everything in Docker begins with a Docker image; you can either build your own Docker image from scratch or begin with an existing one. For the purposes of this section, the base Docker image will be downloaded from Docker Hub and we will continue with building the Go version of the Hello World! program inside that Docker image.

The contents of the Dockerfile that will be used are as follows:

FROM golang:alpine 
 
RUN mkdir /files 
COPY hw.go /files 
WORKDIR /files 
 
RUN go build -o /files/hw hw.go 
ENTRYPOINT ["/files/hw"] 

The first line defines the Docker image ...

Get Mastering Go - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.