Home > Blockchain >  I got error when try to run docker-compose up -d
I got error when try to run docker-compose up -d

Time:08-29

Im just getting started with Golang and am trying to build go gin with docker-compose. Here is my Dockerfile

FROM golang:1.18

WORKDIR /docker-go

# pre-copy/cache go.mod for pre-downloading dependencies and only redownloading them in subsequent builds if they change
# COPY all file go ./

COPY ./app/main.go ./
COPY . /docker-go
# To initialize a project with go module, create go.mod

RUN go mod init shipping-go

# Add missing and/or remove unused modules
RUN go mod tidy

# This will bring all the vendors to your projects /vendor directory so that 
# you don't need to get the modules again if working from another machine on this project.
RUN go mod vendor

RUN go mod download && go mod verify

COPY . .
RUN go build -v -o /docker-go/app .

RUN chmod  x /docker-go
USER root

and my docker-compose

version: "3.7"
services:
  go-web:
    build:     
      context: ./
      dockerfile: Dockerfile
    restart: 'no'
    working_dir: /docker-go
    ports:
      - "8080:8080"
    entrypoint: ["./start.sh"]
    volumes:
      - ./:/docker-go

And i got error when i check logs container with command

docker logs learn-docker-go_go-web_1

/docker-go
go: cannot find main module, but found .git/config in /docker-go
    to create a module there, run:
    go mod init
/docker-go

its seem go can't find module file but i have install in Dockerfile. For Detail i push code in my repository here https://github.com/duyanh1904/learn-docker-go

CodePudding user response:

There are multiple issues with your Dockerfile and docker-compose.yml and I have been unable to replicate the issue you are seeing (but your setup does not work for me either). A few of the problems I see are:

  • COPY . /docker-go will copy the current folder (including subfolders) into the image at /docker-go/. This will result in a folder /docker-go/app. The existence of this folder means that go build -v -o /docker-go/app . will store the executable as /docker-go/app/shipping-go (you are trying to execute a folder).
  • Your intention is for the executable to be in /docker-go but you then obscure that folder with a mount (./:/docker-go). As per the docs "If you bind-mount into a non-empty directory on the container, the directory’s existing contents are obscured by the bind mount.".
  • Your start.sh (not shown in the question) is not starting the executable (see the docs).
  • in main.go (also not shown in the question) replace router.Run("127.0.0.1:8080") with router.Run(":8080"). Listening on 127.0.0.1 means that only local connections will be accepted; within a container that means only connections from the container itself.

Here is a working setup to get you going (this is probably not optimal but should provide a starting point that will enable you to experiment further). Note that you need to make the above change in main.go first.

Dockerfile

FROM golang:1.18

WORKDIR /docker-go

# Note: A better solution would be to copy an existing go.mod into the image
RUN go mod init shipping-go
COPY ./app/main.go ./
# Determine required modules and download them
RUN go mod tidy
RUN go build -v -o /docker-go/app
RUN chmod  x /docker-go/app

docker-compose.yml

version: "3.7"
services:
  go-web:
    build:     
      dockerfile: Dockerfile
    restart: 'no'
    ports:
      - "8080:8080"
    command: ["./app"]
  • Related