skip to Main Content

I’m trying to make a production build of my Golang API with Docker container. It worked in Development (with Air), but keeps telling me, the executable was not found in production one.
I’ve tested it and it’s correctly compiled and it exists and is executable.

I added the text to Dockerfile to be sure;

# first build the app binary
FROM golang:1.20-alpine

WORKDIR /app

COPY goapp/ .

RUN go mod download

RUN go build -o /app/executable

# check if the binary exists and is executable
RUN [ -f /app/executable -a -x /app/executable ]

CMD /app/executable

I run it using docker-compose:

version: '3.3'

services:
  # db...
  goapp:
    build:
      context: ./
      dockerfile: ./docker/go/Dockerfile_prod
    environment:
      - SECRET=${SECRET}
      - PORT=3000
      - GO_ENV=production
      - GIN_MODE=release
      - MYSQL_HOST=${MYSQL_HOST}
      - MYSQL_PORT=${MYSQL_PORT}
      - MYSQL_USER=${MYSQL_USER}
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
      - MYSQL_DATABASE=${MYSQL_DATABASE}
      - SMTP_HOST=${SMTP_HOST}
      - SMTP_USER=${SMTP_USER}
      - SMTP_PASSWORD=${SMTP_PASSWORD}
      - SENTRY_DSN=${SENTRY_DSN}
    ports:
      - 3000:3000
    volumes:
      - ./goapp:/app
      - ./data/goapp:/app/data
    depends_on:
      db:
        condition: service_healthy

I’ve been building it with --no-cache flag on docker-compose -f ... build

Tried to change CMD for ENTRYPOINT and with brackets or without. Also tried

ENTRYPOINT ["/bin/sh", "-c", "/app/executable"]

But I keep geting this:

goapp_1    | /bin/sh: /app/executable: not found
project_goapp_1 exited with code 127

Why is it not running and how can I make it work? Thanks.

2

Answers


  1. volumes:
      - ./goapp:/app
    

    ./goapp on the host machine is mounted into a non-empty directory (/app) on the container. The /app directory’s existing contents are obscured by the bind mount. Obviously, this is not what you want.

    See Mount into a non-empty directory on the container.

    You should pick the files and directories that you want to mount into the container and mount them separately. But if your app depends on those files and directories, a better approach is to modify the Dockerfile to copy them into the image.

    Login or Signup to reply.
  2. volumes:
      - ./goapp:/app
    

    Since you mount /app directory inside your container into the host machine directory goapp, then the executable should located in your goapp directory.

    build the binary in your local machine

    go build -o goapp/executable goapp/*.go
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search