Deploy Golang server

Hello, how can i deploy my golang server(backend).

Each time i tried to deploy i got an error :

023-01-21T14:04:26.093 app[906d968c] fra [info] Preparing to run: `./main` as root

2023-01-21T14:04:26.097 app[906d968c] fra [info] Error: UnhandledIoError(Os { code: 2, kind: NotFound, message: "No such file or directory" })

2023-01-21T14:04:26.098 app[906d968c] fra [info] [ 0.080852] Kernel panic - not syncing: Attempted to kill init! exitcode=0x00000100

2023-01-21T14:04:26.098 app[906d968c] fra [info] [ 0.081953] CPU: 0 PID: 1 Comm: init Not tainted 5.12.2 #1

2023-01-21T14:04:26.099 app[906d968c] fra [info] [ 0.082732] Call Trace:

2023-01-21T14:04:26.099 app[906d968c] fra [info] [ 0.083089] show_stack+0x52/0x58

2023-01-21T14:04:26.100 app[906d968c] fra [info] [ 0.083562] dump_stack+0x6b/0x86

2023-01-21T14:04:26.100 app[906d968c] fra [info] [ 0.083912] panic+0xfb/0x2bc

2023-01-21T14:04:26.100 app[906d968c] fra [info] [ 0.084226] do_exit.cold+0x60/0xb0

2023-01-21T14:04:26.101 app[906d968c] fra [info] [ 0.084644] do_group_exit+0x3b/0xb0

2023-01-21T14:04:26.101 app[906d968c] fra [info] [ 0.085172] __x64_sys_exit_group+0x18/0x20

2023-01-21T14:04:26.102 app[906d968c] fra [info] [ 0.085749] do_syscall_64+0x38/0x50

2023-01-21T14:04:26.103 app[906d968c] fra [info] [ 0.086241] entry_SYSCALL_64_after_hwframe+0x44/0xae

2023-01-21T14:04:26.103 app[906d968c] fra [info] [ 0.086921] RIP: 0033:0x7f1e9ef2c1a9

2023-01-21T14:04:26.106 app[906d968c] fra [info] [ 0.087447] Code: eb ef 48 8b 76 28 e9 a5 03 00 00 64 48 8b 04 25 00 00 00 00 48 8b b0 b0 00 00 00 e9 af ff ff ff 48 63 ff b8 e7 00 00 00 0f 05 <ba> 3c 00 00 00 48 89 d0 0f 05 eb f9 66 2e 0f 1f 84 00 00 00 00 00

2023-01-21T14:04:26.107 app[906d968c] fra [info] [ 0.090076] RSP: 002b:00007ffe25ef0b28 EFLAGS: 00000246 ORIG_RAX: 00000000000000e7

2023-01-21T14:04:26.108 app[906d968c] fra [info] [ 0.091135] RAX: ffffffffffffffda RBX: 00007f1e9eca9050 RCX: 00007f1e9ef2c1a9

2023-01-21T14:04:26.109 app[906d968c] fra [info] [ 0.092144] RDX: 0000000000000000 RSI: 0000000000000000 RDI: 0000000000000001

2023-01-21T14:04:26.110 app[906d968c] fra [info] [ 0.093148] RBP: 0000000000000001 R08: 00007f1e9f005b08 R09: 0000000000000000

2023-01-21T14:04:26.111 app[906d968c] fra [info] [ 0.094153] R10: 0000000000000000 R11: 0000000000000246 R12: 00007ffe25ef0b88

2023-01-21T14:04:26.112 app[906d968c] fra [info] [ 0.095169] R13: 00007ffe25ef0b98 R14: 0000000000000000 R15: 0000000000000000

2023-01-21T14:04:26.112 app[906d968c] fra [info] [ 0.096158] Kernel Offset: disabled

2023-01-21T14:04:26.113 app[906d968c] fra [info] [ 0.096674] Rebooting in 1 seconds..

The docker file is shown below

# First stage: Build the binary
FROM golang:latest AS build

# Set the working directory in the container
WORKDIR /app

# Copy the go mod and sum files
COPY go.mod go.sum ./

# Download dependencies
RUN go mod download

# Copy the source code to the container
COPY . .

# Build the binary
RUN go build -o main .

# Second stage: Copy the binary to a minimal image
FROM alpine:latest

# Set the working directory
WORKDIR /root/

# Copy the binary from the build stage
COPY --from=build /app/main .

# Expose the port that the application will run on
EXPOSE 9090

# Run the binary
CMD ["./main"]

Thanks for your help

Hi!

edit: whoops, my answer is wrong I think. Altho the error is “file not found”.

Can you try setting the full path as the CMD instead of ./main ?

(is this a dockerfile generated by Fly or did you create it yourself?)

Dockerfile generated by Fly

what full path should i give ?

if i modify the Dockerfile to print

# First stage: Build the binary

## ....... as previous
RUN ls  # this print all my files path

## ....... as previous

# Second stage: Copy the binary to a minimal image

RUN ls  # this print "main

# Run the binary
CMD ["./main"]

I can reproduce the issue, it’s due to missing shared libraries. There is something wrong with the Dockerfile (I am not sure where it came from, Fly doesn’t generate it for me).

Here are two possible solutions:

A. Ensure the binary is static by changing RUN go build -o main . to RUN CGO_ENABLED=0 go build -o main . (see go - How does CGO_ENABLED affect dynamic vs static linking? - Stack Overflow).

B. Use the same base OS for both stages, e.g. by changing FROM golang:latest AS build to FROM golang:1-alpine AS build.

Thank for your reply.

Is working now , the major changes made are

RUN chmod +x main
RUN CGO_ENABLED=0 go build -o main main.go

The Dockerfile now looks

# First stage: Build the binary
FROM golang:latest AS build

# Set the working directory in the container
WORKDIR /app

# Copy the go mod and sum files
COPY go.mod go.sum ./

# Download dependencies
RUN go mod download

# Copy the source code to the container
COPY . .
RUN echo "APP_STAGE= STAGING" > app.env
# Build the binary
RUN CGO_ENABLED=0 go build -o main main.go

# Second stage: Copy the binary to a minimal image
FROM alpine:latest

# Set the working directory
WORKDIR /
# Copy the binary from the build stage
COPY --from=build /app/main .

COPY --from=build /app/app.env .
# Make the binary executable
RUN chmod +x main

# Expose the port that the application will run on
EXPOSE 8080

# Run the binary
ENTRYPOINT [ "./main" ]


1 Like

If you’re building static Go binaries, you can do one better and use distroless as your deployment image; ex:

Note though, fly ssh console doesn’t work with distroless.

Thanks thanks for the information