« Back to Index

Go: Docker Go/TinyGo Application

View original Gist on GitHub

Tags: #docker #go

Docker Go and TinyGo Application.md

The following build command is used…

docker buildx build --platform linux/arm64 -t testing-tinygo . -f ./Dockerfile

…to avoid the following error:

The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested

The Dockerfile used is:

FROM golang:1.18.1 AS golang

# Move to fiddle workspace
WORKDIR /app

# Copy default workspace
COPY src src
COPY go.mod .

# Enable access to internal repositories
RUN apt-get update && apt-get install -y git
RUN git config --global url."https://<REDACTED>:x-oauth-basic@github.com/".insteadOf "https://github.com/"
RUN mkdir -p /root/.ssh
RUN echo "StrictHostKeyChecking no" > /root/.ssh/config
ENV GOPRIVATE=github.com/fastly

# Fetch dependencies ahead of time
RUN go get ./...
RUN go mod download

FROM tinygo/tinygo:0.23.0

# Move to fiddle workspace
WORKDIR /tmp/fiddle

# Pull files from previous build stage.
COPY --from=golang /go /go
COPY --from=golang /app/src ./src
COPY --from=golang /app/go.mod ./go.mod
COPY --from=golang /app/go.sum ./go.sum

# Enable access to internal repositories
RUN apt-get update && apt-get install -y git
RUN git config --global url."https://<REDACTED>:x-oauth-basic@github.com/".insteadOf "https://github.com/"
RUN mkdir -p /root/.ssh
RUN echo "StrictHostKeyChecking no" > /root/.ssh/config
ENV GOPRIVATE=github.com/fastly

# Compile the project
RUN mkdir bin/
RUN tinygo build -target=wasi -wasm-abi=generic -gc=conservative -scheduler=asyncify -o bin/main.wasm ./src

NOTE: We don’t need multi-stage build. We can just build straight from tinygo image (see below).

FROM tinygo/tinygo:0.23.0

# Move to fiddle workspace
WORKDIR /tmp/fiddle

# Copy over template files
COPY src src
COPY go.mod .

# Enable access to internal repositories
RUN apt-get update && apt-get -y install git
RUN git config --global url."https://<REDACTED>:x-oauth-basic@github.com/".insteadOf "https://github.com/"
RUN mkdir -p /root/.ssh
RUN echo "StrictHostKeyChecking no" > /root/.ssh/config
ENV GOPRIVATE=github.com/fastly

# Fetch dependencies ahead of time
RUN go get ./...
RUN go mod download

# Compile the project
RUN mkdir bin/
RUN tinygo build -target=wasi -wasm-abi=generic -gc=conservative -scheduler=asyncify -o bin/main.wasm ./src