Last active
February 24, 2025 20:06
-
-
Save novabyte/cc6d57022e2d3baa40e98d8fc91dc4c8 to your computer and use it in GitHub Desktop.
A small Docker-based build setup to create and build Go code with Nakama server.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: '3' | |
services: | |
postgres: | |
container_name: game_backend_postgres | |
environment: | |
- POSTGRES_DB=nakama | |
- POSTGRES_PASSWORD=localdb | |
expose: | |
- "8080" | |
- "5432" | |
image: postgres:9.6-alpine | |
ports: | |
- "5432:5432" | |
- "8080:8080" | |
volumes: | |
- data:/var/lib/postgresql/data | |
nakama: | |
build: . | |
container_name: game_backend | |
depends_on: | |
- postgres | |
entrypoint: | |
- "/bin/sh" | |
- "-ecx" | |
- > | |
/nakama/nakama migrate up --database.address postgres:localdb@postgres:5432/nakama && | |
exec /nakama/nakama --database.address postgres:localdb@postgres:5432/nakama | |
expose: | |
- "7349" | |
- "7350" | |
- "7351" | |
healthcheck: | |
test: ["CMD", "curl", "-f", "http://localhost:7350/"] | |
interval: 10s | |
timeout: 5s | |
retries: 5 | |
links: | |
- "postgres:db" | |
ports: | |
- "7349:7349" | |
- "7350:7350" | |
- "7351:7351" | |
restart: unless-stopped | |
volumes: | |
data: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM heroiclabs/nakama-pluginbuilder:3.2.0 AS builder | |
ENV GO111MODULE on | |
ENV CGO_ENABLED 1 | |
WORKDIR /backend | |
COPY . . | |
RUN go build --trimpath --mod=vendor --buildmode=plugin -o ./backend.so | |
FROM heroiclabs/nakama:3.2.0 | |
COPY --from=builder /backend/backend.so /nakama/data/modules |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module github.com/account/reponame | |
go 1.16 | |
require github.com/heroiclabs/nakama-common v1.13.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"database/sql" | |
"github.com/heroiclabs/nakama-common/runtime" | |
) | |
//noinspection GoUnusedExportedFunction | |
func InitModule(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, initializer runtime.Initializer) error { | |
logger.Info("Backend loaded.") | |
return nil | |
} |
You must also remember to run before you run the first build via Docker:
go mod vendor
This ensures that the dependencies for the project are local to the filesystem when the build runs. For more information see Go modules and vendored dependencies.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To rebuild the container when code is changed use:
You can see which containers are active with the
docker ps
command.