Use Docker Compose to define your Go application and its dependencies in a docker-compose.yml file, then run docker-compose up --build to compile and start the services.
version: '3.8'
services:
app:
build: .
ports:
- "8080:8080"
environment:
- GOOS=linux
- GOARCH=amd64
- Create a
Dockerfilein your project root withFROM golang:1.21-alpine,WORKDIR /app,COPY . .,RUN go build -o main ., andCMD ["./main"]. - Add the
docker-compose.ymlfile shown above to define the service. - Run
docker-compose up --buildto build the image and start the container.