Setting Up a Go Development Environment with Docker Compose and Air
Docker Compose provides a streamlined way to manage multi-container applications, while Air
enhances the development workflow by enabling hot reloading for Go applications. In this guide, we will break down how to create a Go development setup using Docker Compose and Air, based on an improved setup.
Dockerfile
# Specify the base image
FROM --platform=linux/arm64 golang:1.23.4-alpine
WORKDIR /app
# Install essential utilities
RUN apk add --no-cache git bash
# Set the GOPATH and include it in the PATH
ENV GOPATH=/go
ENV PATH=$GOPATH/bin:/usr/local/go/bin:$PATH
# Install Air for hot reloading
RUN go install github.com/air-verse/air@latest
# Copy the project files into the container
COPY . .
# Set the default command to run the application using Air
CMD ["air"]
Docker Compose Configuration (docker-compose.yml
)
version: '3.8'
services:
app:
build:
context: .
dockerfile: Dockerfile
ports:
- "8080:8080"
volumes:
- .:/app
command: air
environment:
- GOPATH=/go
- PATH=/go/bin:/usr/local/go/bin:$PATH
Benefits of Using Docker Compose with Air
- Simplified Management: Single command to start and stop services.
- Hot Reloading: Automatically reloads the application when code changes are detected.
- Consistent Development Environment: Same setup across all team members.
Running the Setup
- Build and Start the Project:
docker-compose up --build
- Access the Application:
- Visit
http://localhost:8080
- Visit
Useful Links
- Official Go Documentation
- Docker Official Documentation
- Air GitHub Repository
- Docker Compose Documentation
Conclusion
This improved setup ensures a modern, efficient Go development environment using Docker Compose and Air. The hot reloading feature provided by Air makes the development process faster and more efficient.