This guide explains how to set up and build a Go application using CompileDaemon for local development and Docker/Docker Compose for containerized deployment.
-
Go Installed: Ensure Go is installed on your system. Verify your installation by running:
go version
-
CompileDaemon Installed: Install CompileDaemon using:
go install github.com/githubnemo/CompileDaemon@latest
-
Docker Installed: Ensure Docker is installed on your system. Check your installation with:
docker --version
-
Docker Compose Installed (for Docker Compose only): Check Docker Compose installation:
docker-compose --version
-
Setting Environment Variables:
-
Create a
.envfile in your project directory and set the following variables:PORT=1323 # PostgreSQL connection variables DB="host=# user=# password=# dbname=# port=# sslmode=#" JWT_SECRET=
-
Replace
host=#,user=#,password=#,dbname=#,port=#, andsslmode=#with your PostgreSQL connection details. -
Set
JWT_SECRETusing a Linux command:openssl rand -base64 32
-
-
Navigate to Your Project Directory: Open your terminal and change to your Go project directory.
cd /path/to/your/project -
Organize Your Project Structure: Ensure your
main.gofile is located in thecmd/appdirectory (or adjust paths accordingly). -
Run CompileDaemon: Start CompileDaemon to automatically build and restart your server on file changes.
CompileDaemon --build="go build -o main ./cmd/app/main.go" --command="./main"
--build="go build -o main ./cmd/app/main.go": Specifies the build command to compilemain.gointo an executable namedmain.--command="./main": Defines the command to run the compiled executable.
-
Verify Output: Upon running CompileDaemon, you should see output indicating successful builds and server restarts on file changes.
-
Ensure Dockerfile: Confirm you have a
Dockerfilein your project directory -
Build Docker Image: Build the Docker image using the existing
Dockerfile:docker build -t goland-app:latest .
-
Run Docker Container: Start a Docker container based on the built image:
docker run -d --name auth-go -p 1323:1323 goland-app:latest
-d: Runs the container in detached mode.--name auth-go: Assigns the nameauth-goto the container.-p 1323:1323: Maps port 8080 from the container to your host machine.
-
Ensure
docker-compose.yml: Confirm you have adocker-compose.ymlfile in your project directory -
Build and Start Docker Compose: Build and start your application using Docker Compose:
docker-compose up --build
This command builds the Docker image defined in your
Dockerfile, starts the container, and exposes port 1323.
Integrating CompileDaemon for local development and Docker/Docker Compose for containerized deployment simplifies the build and deployment process of your Go applications. It enhances development efficiency and ensures consistency across different environments.