Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build multi-platform Docker image #1022

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions build/Dockerfile
Expand Up @@ -15,13 +15,13 @@
# docker build "$PWD" --build-arg commit="$(git rev-parse --short HEAD)" --build-arg version=v2.1.1 -t heroiclabs/nakama:2.1.1
# docker build "$PWD" --build-arg commit="$(git rev-parse --short HEAD)" --build-arg version="$(git rev-parse --short HEAD)" -t heroiclabs/nakama-prerelease:"$(git rev-parse --short HEAD)"

FROM golang:1.20.3-bullseye as builder
FROM --platform=$BUILDPLATFORM golang:1.20.3-bullseye as builder

ARG commit
ARG version

ENV GOOS linux
ENV GOARCH amd64
ENV GOOS $TARGETOS
ENV GOARCH $TARGETARCH
ENV CGO_ENABLED 1

RUN apt-get update && \
Expand Down
6 changes: 3 additions & 3 deletions build/Dockerfile.dsym
Expand Up @@ -15,13 +15,13 @@
# docker build "$PWD" --build-arg commit="$(git rev-parse --short HEAD)" --build-arg version=v2.1.1 -t heroiclabs/nakama:2.1.1
# docker build "$PWD" --build-arg commit="$(git rev-parse --short HEAD)" --build-arg version="v2.1.1-$(git rev-parse --short HEAD)" -t heroiclabs/nakama-prerelease:"2.1.1-$(git rev-parse --short HEAD)"

FROM golang:1.20.3-bullseye as builder
FROM --platform=$BUILDPLATFORM golang:1.20.3-bullseye as builder

ARG commit
ARG version

ENV GOOS linux
ENV GOARCH amd64
ENV GOOS $TARGETOS
ENV GOARCH $TARGETARCH
ENV CGO_ENABLED 1

RUN apt-get update && \
Expand Down
61 changes: 38 additions & 23 deletions build/README.md
Expand Up @@ -39,8 +39,7 @@ These steps are run for each new release.
3. Execute the cross-compiled build helper.

```
xgo --targets=darwin/arm64,darwin/amd64,linux/amd64,linux/arm64,windows/amd64 --trimpath --ldflags "-s -w -X main.version=2.1.0 -X main.commitID=$(git rev-parse --short HEAD 2>/dev/null)"
github.com/heroiclabs/nakama
xgo --targets=darwin/arm64,darwin/amd64,linux/amd64,linux/arm64,windows/amd64 --trimpath --ldflags "-s -w -X main.version=2.1.0 -X main.commitID=$(git rev-parse --short HEAD 2>/dev/null)" github.com/heroiclabs/nakama
```

This will build binaries for all target platforms supported officially by Heroic Labs.
Expand All @@ -57,55 +56,71 @@ github.com/heroiclabs/nakama

With the release generated we can create the official container image.

1. Build the container image.
These steps are one off to install the required build utilities.

1. Install Docker Desktop.

or

1. Install the Docker buildx client manually. (https://docs.docker.com/build/architecture/#install-buildx)

2. Install the emulators for foreign architectures.

```
cd build
docker build "$PWD" --platform "linux/amd64" --file ./Dockerfile --build-arg commit="$(git rev-parse --short HEAD 2>/dev/null)" --build-arg version=2.1.0 -t heroiclabs/nakama:2.1.0
docker run --privileged --rm tonistiigi/binfmt --install all
```

2. Push the image to the container registry.
3. Create a new builder instance that supports multi-platform builds, and switch to use it.

```
docker tag <CONTAINERID> heroiclabs/nakama:latest
docker push heroiclabs/nakama:2.1.0
docker push heroiclabs/nakama:latest
docker buildx create --name mybuilder --driver docker-container --bootstrap --use
```

These steps are run for each new release.

1. Use an existing builder that supports multi-platform builds.

```
docker buildx use mybuilder
```

2. Build the container image and push to the container registry.

```
cd build
docker buildx build "$PWD" --platform linux/amd64,linux/arm64 --file ./Dockerfile --build-arg commit="$(git rev-parse --short HEAD 2>/dev/null)" --build-arg version=2.1.0 -t heroiclabs/nakama:2.1.0 -t heroiclabs/nakama:latest --push
```

## Build Nakama Image (dSYM)

With the release generated we can also create an official container image which includes debug symbols.

1. Build the container image.
1. Use an existing builder that supports multi-platform builds.

```
cd build
docker build "$PWD" --platform "linux/amd64" --file ./Dockerfile.dsym --build-arg commit="$(git rev-parse --short HEAD 2>/dev/null)" --build-arg version=2.1.0 -t heroiclabs/nakama-dsym:2.1.0
docker buildx use mybuilder
```

2. Push the image to the container registry.
2. Build the container image and push to the container registry.

```
docker tag <CONTAINERID> heroiclabs/nakama-dsym:latest
docker push heroiclabs/nakama-dsym:2.1.0
docker push heroiclabs/nakama-dsym:latest
cd build
docker buildx build "$PWD" --platform linux/amd64,linux/arm64 --file ./Dockerfile.dsym --build-arg commit="$(git rev-parse --short HEAD 2>/dev/null)" --build-arg version=2.1.0 -t heroiclabs/nakama-dsym:2.1.0 -t heroiclabs/nakama-dsym:latest --push
```

## Build Plugin Builder Image

With the official release image generated we can create a container image to help with Go runtime development.

1. Build the container image.
1. Use an existing builder that supports multi-platform builds.

```
cd build/pluginbuilder
docker build "$PWD" --platform "linux/amd64" --file ./Dockerfile --build-arg commit="$(git rev-parse --short HEAD 2>/dev/null)" --build-arg version=2.1.0 -t heroiclabs/nakama-pluginbuilder:2.1.0
docker buildx use mybuilder
```

2. Push the image to the container registry.
2. Build the container image.

```
docker tag <CONTAINERID> heroiclabs/nakama-pluginbuilder:latest
docker push heroiclabs/nakama-pluginbuilder:2.1.0
docker push heroiclabs/nakama-pluginbuilder:latest
cd build/pluginbuilder
docker buildx build "$PWD" --platform linux/amd64,linux/arm64 --file ./Dockerfile --build-arg commit="$(git rev-parse --short HEAD 2>/dev/null)" --build-arg version=2.1.0 -t heroiclabs/nakama-pluginbuilder:2.1.0 -t heroiclabs/nakama-pluginbuilder:latest --push
```
6 changes: 3 additions & 3 deletions build/pluginbuilder/Dockerfile
Expand Up @@ -15,7 +15,7 @@
# docker build "$PWD" --file ./Dockerfile.pluginbuilder --build-arg commit="$(git rev-parse --short HEAD)" --build-arg version=v2.1.1 -t heroiclabs/nakama-pluginbuilder:2.1.1
# docker build "$PWD" --file ./Dockerfile.pluginbuilder --build-arg commit="$(git rev-parse --short HEAD)" --build-arg version="v2.1.1-$(git rev-parse --short HEAD)" -t heroiclabs/nakama-prerelease:"2.1.1-$(git rev-parse --short HEAD)"

FROM golang:1.20.3-bullseye as builder
FROM --platform=$BUILDPLATFORM golang:1.20.3-bullseye as builder

MAINTAINER Heroic Labs <support@heroiclabs.com>

Expand All @@ -26,8 +26,8 @@ LABEL version=$version
LABEL variant=nakama-pluginbuilder
LABEL description="A support container to build Go code for Nakama server's runtime."

ENV GOOS linux
ENV GOARCH amd64
ENV GOOS $TARGETOS
ENV GOARCH $TARGETARCH
ENV CGO_ENABLED 1

RUN apt-get update && \
Expand Down