Junior — Middle
How can you build and prepare a container with an application using Docker?
sobes.tech AI
Answer from AI
To build and prepare a container with a Go application using Docker, a Dockerfile is usually created, describing the build and run stages of the application. Multi-stage builds are often used to reduce the size of the final image.
Example Dockerfile for a Go application:
# Build stage
FROM golang:1.20-alpine AS builder
WORKDIR /app
COPY . .
RUN go build -o myapp
# Run stage
FROM alpine:latest
WORKDIR /app
COPY --from=builder /app/myapp .
CMD ["./myapp"]
Commands for building and running:
docker build -t my-go-app .
docker run -p 8080:8080 my-go-app
Thus, the application is first compiled into a Go image, then a minimal image with only the binary for running is created.