Home  >  Article  >  Backend Development  >  Docker run error: exec /app/backend/server: No such file or directory

Docker run error: exec /app/backend/server: No such file or directory

WBOY
WBOYforward
2024-02-08 21:21:181114browse

Docker 运行错误:exec /app/backend/server:没有这样的文件或目录

When using Docker, you sometimes encounter some running errors, such as the error message "exec /app/backend/server: No such file or directory". This error can leave people confused as to how to fix it. In response to this problem, PHP editor Yuzai has provided some solutions for everyone, hoping to help everyone. Next, let’s take a look at how to solve this problem.

Question content

I'm having trouble trying to run a Docker container. I built an image from a Dockerfile:

docker build -t server -f ./backend/Dockerfile .

Run it:

docker run -it -p 8081:8081 server

An error occurred:

exec /app/backend/server: no such file or directory

When I check from Docker Desktop, I see that the file exists inside the container and has been created to the location where it should be.

I also tried changing the second stage FROM golang:1.21-alpine but still got the same error.

With from gcr.io/distroless/base-debian11 I get:

/app/backend/server: /lib/aarch64-linux-gnu/libc.so.6: version `GLIBC_2.33' not found (required by /app/backend/server)
/app/backend/server: /lib/aarch64-linux-gnu/libc.so.6: version `GLIBC_2.32' not found (required by /app/backend/server)
/app/backend/server: /lib/aarch64-linux-gnu/libc.so.6: version `GLIBC_2.34' not found (required by /app/backend/server)

I looked here, here and tried a lot of things. I'm relatively new to Docker and don't know how to solve this problem. Can anyone help me understand what might be causing this error and how to fix it? Thanks in advance!

The following is my Dockerfile:

# Stage 1: Building the application
FROM golang:1.21 AS builder

WORKDIR /app

COPY go.mod go.sum ./
RUN go mod download

COPY . ./

RUN apt-get update && apt-get install -y sqlite3 libsqlite3-dev
RUN CGO_ENABLED=1 GOOS=linux go build -a -installsuffix cgo -o /app/backend/server ./backend/backend.go

# Stage 2: Production stage using Alpine
FROM alpine:latest

RUN apk --no-cache add ca-certificates sqlite 

COPY ./backend/configs/config /app/configs/config
COPY ./database/sqlite/schema.sql /app/database/sqlite/schema.sql

COPY ./tls/server.crt /tls/server.crt
COPY ./tls/server.key /tls/server.key

COPY --from=builder /app/backend/server /app/backend/server

EXPOSE 8081

ENTRYPOINT ["/app/backend/server"]

Workaround

I replicated your problem with a simplified dockerfile and application (please try to provide a minimal, reproducible example - I had to guess which sqlite library you are using):

backend.go

package main

import (
    "database/sql"
    "log"
    "os"

    _ "github.com/mattn/go-sqlite3"
)

func main() {
    os.Remove("./foo.db")

    db, err := sql.Open("sqlite3", "./foo.db")
    if err != nil {
        log.Fatal(err)
    }
    defer db.Close()

    sqlStmt := `
    create table foo (id integer not null primary key, name text);
    delete from foo;
    `
    _, err = db.Exec(sqlStmt)
    if err != nil {
        log.Printf("%q: %s\n", err, sqlStmt)
        return
    }
}

dockerfile:

# Stage 1: Building the application
FROM golang:1.21 AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . ./
RUN apt-get update && apt-get install -y sqlite3 libsqlite3-dev
RUN CGO_ENABLED=1 GOOS=linux go build -a -installsuffix cgo -o /app/server

# Stage 2: Production stage using Alpine
FROM alpine:latest
RUN apk --no-cache add ca-certificates sqlite
COPY --from=builder /app/server /app/server
EXPOSE 8081
ENTRYPOINT ["/app/server"]

Start a shell in the container (docker run -it --entrypoint /bin/sh server) We can see the executable is there, the permissions are fine, but it doesn't run:

/ # ls -al /app/server
-rwxr-xr-x    1 root     root       6816280 Sep 22 02:29 /app/server
/ # /app/server
/bin/sh: /app/server: not found
/ # ldd /app/server
        /lib64/ld-linux-x86-64.so.2 (0x7ff8cb4ba000)
        libc.so.6 => /lib64/ld-linux-x86-64.so.2 (0x7ff8cb4ba000)
Error relocating /app/server: fcntl64: symbol not found

It's easy to see the error "not found" and think that it must be due to the file not being where you expect it to be, or the permissions being incorrect. However, the same error is displayed when something the executable depends on is missing. ldd Display problem - executable depends on fcntl64; the library is provided by glibc but is notmusl (as used in Alpine - incompatibilities between glibc and musl are not uncommon).

The simplest solution is to compile the application using the same operating system as the one it is running on:

# Stage 1: Building the application
FROM golang:1.21-alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . ./
RUN apk --no-cache add gcc g++ sqlite
RUN CGO_ENABLED=1 GOOS=linux go build -a -installsuffix cgo -o /app/server

# Stage 2: Production stage using Alpine
FROM alpine:latest
RUN apk --no-cache add ca-certificates sqlite
COPY --from=builder /app/server /app/server
EXPOSE 8081
ENTRYPOINT ["/app/server"]

Then run this (no output from my executable, but I confirmed the database was created):

/ # ls -al /app/server
-rwxr-xr-x    1 root     root       6838120 Sep 22 02:39 /app/server
/ # /app/server
/ # ldd /app/server
        /lib/ld-musl-x86_64.so.1 (0x7fabcb701000)
        libc.musl-x86_64.so.1 => /lib/ld-musl-x86_64.so.1 (0x7fabcb701000)
/ # ls -al ./foo.db 
-rw-r--r--    1 root     root          8192 Sep 22 02:40 ./foo.db

Another option is to use the pure go library (no CGO required).

The above is the detailed content of Docker run error: exec /app/backend/server: No such file or directory. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete