Skip to content

Instantly share code, notes, and snippets.

@novabyte
Last active April 29, 2022 14:01
Show Gist options
  • Save novabyte/cc6d57022e2d3baa40e98d8fc91dc4c8 to your computer and use it in GitHub Desktop.
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.
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:
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
module github.com/account/reponame
go 1.16
require github.com/heroiclabs/nakama-common v1.13.0
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
}
@novabyte
Copy link
Author

novabyte commented Mar 3, 2020

To rebuild the container when code is changed use:

docker-compose -f ./docker-compose.yml up -d --build nakama

You can see which containers are active with the docker ps command.

@novabyte
Copy link
Author

novabyte commented Oct 24, 2020

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