liveBook · Manning

Dockerfile

As explained in gRPC Microservices in Go

A Dockerfile is a text-based build specification containing instructions for creating a Docker image.

A Dockerfile is a text-based build specification that defines how to create a Docker image for a service. In a microservice test stack, each service gets its own Dockerfile so Docker Compose or test containers can build and run the required services. section 7.4.1 section 8.1.1

A practical Go Dockerfile uses a multistage build. A Go parent image compiles the service, then a scratch image receives only the generated executable. This separates compile-time dependencies from runtime contents and produces a smaller runtime image. section 7.4.5 section 8.1.1

Order service image

Use golang:1.18 as the builder, copy the service source into /usr/src/app, compile a Linux order executable with CGO disabled, copy it into scratch, and start it with CMD ["./order"]. A Payment service follows the same structure with a payment executable. Docker Compose can then build each service and provide ports, database URLs, and service endpoints through environment variables. section 7.4.6 section 7.4.5

FROM golang:1.18 AS builder                                           #1
WORKDIR /usr/src/app
COPY . .                                                              #2
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o order 
 ./cmd/main.go                                                      #3
 
FROM scratch
COPY --from=builder /usr/src/app/order ./order                        #4
CMD ["./order"]                                                       #5

Inspect the complete multistage Dockerfile for the Order service, including the builder stage, artifact transfer, and runtime command. section 8.1.1

Build an image by preparing the Dockerfile and running docker build -t order . from the service directory. The directory becomes the build context. Tag and push the resulting image to a registry when a Kubernetes cluster must pull it; local development may instead copy images directly to cluster nodes. section 8.1.1

How gRPC Microservices in Go covers it

How deep it goes
Explains multistage Go builds, build context, executable transfer, and image commands, while deferring detailed multistage-build treatment.
Examples
Uses separate Order and Payment Dockerfiles with Go builder stages, scratch runtimes, Docker Compose services, and docker build commands.
What it assumes
Assumes familiarity with Docker containers and Go services while introducing Dockerfile stages, image builds, and runtime configuration.
What you can do after
Build service images from Dockerfiles and prepare them for Compose-based tests or Kubernetes deployment.
How it connects
Connects Docker images with Docker Compose, test containers, Go, Kubernetes, environment variables, and container registries.

Also explained in

Compare how 18 books explain Dockerfile →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of gRPC Microservices in Go. The section links point to the parts of the book it draws on.