This template is designed to help you get started with Go and Docker. Follow the steps below to set up your development environment and run your first Go application inside a Docker container.
Before you begin, make sure you have the following installed on your system:
First, clone this repository to your local machine using the following command:
git clone https://github.com/jaygaha/go-docker-beginner-template.git
cd go-docker-beginner-template
Create a new Go file named main.go
in the project directory with the following content:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
Create a Dockerfile
in the project directory with the following content:
# Use the official Golang image
FROM golang:1.24
# Set the working directory inside the container
WORKDIR /var/www/html
# Copy the Go source code to the container
COPY . .
# Build the Go application
RUN go build -o main .
# Command to run the executable
CMD ["./main"]
Build using docker compose command:
docker compose build
OR
Build the Docker image using the following command:
docker build -t golang-beginner .
Run using docker compose command:
docker compose up -d
OR
Run the Docker container using the following command:
docker run --rm golang-beginner
You should see the output Hello, World!
in your browser.
Congratulations! You have successfully set up a Go development environment and run your first Go application inside a Docker container. Feel free to explore and modify the code to learn more about Go and Docker.
Happy coding!
This project is licensed under the MIT License