Updates to the Dockerfile Build Processes
Understand how to update your Dockerfile to build individual microservices using multi-stage builds. Learn to configure Docker Compose profiles to start either the monolith or microservices, manage port conflicts by assigning unique host ports, and enhance your deployment with a reverse proxy to unify OpenAPI access. This lesson equips you to deploy a microservices-based application effectively in a cloud environment.
We'll cover the following...
We have only a single Dockerfile that builds the monolith. Going forward, we also need a way to compile the individual services. To accomplish this, we will use an additional Dockerfile that will make use of build arguments to target the right service to build.
The new Dockerfile will be named Dockerfile.microservices and live alongside the current one in /docker:
ARG svcFROM golang:1.18-alpine AS builderARG svcWORKDIR /mallbotsCOPY go.* ./RUN go mod downloadCOPY .. ./RUN go build -ldflags="-s -w" -v -o service \./${svc}/cmd/serviceFROM alpine:3 AS runtimeCOPY --from=builder /mallbots/docker/wait-for .RUN chmod +x /wait-forCOPY --from=builder /mallbots/service /mallbots/serviceCMD ["/mallbots/service"]
This is a multi-stage Dockerfile. In our first stage called builder, we compile the service into a binary. In the second stage, we copy the wait-for utility, which is used to wait for the database to be available, and the newly compiled binary. By using this Dockerfile, we keep the containers we produce very small, which helps with transferring them and loading them, among other things. ...