1

I trying run my Golang-app in development mode from Docker

Dockerfile

FROM golang 
WORKDIR /usr/src/app
COPY . /usr/src/app
RUN go get -d -v .
RUN go build -o main . 
CMD ["./main"]

docker-compose.yml

version: "3.3"

services:
  database:
    image: postgres
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
    ports:
      - 6060:5432

  api:
    build: ./demoapp-api
    volumes:
      - ./demoapp-api:/usr/src/app
    ports:
      - 5000:5000
    depends_on:
      - database

File structure looks like this:

enter image description here

When I start build I get this error (full log):

Recreating f97ae865ad6d_demoapp_api_1_6f198d4b1db4 ... error

ERROR: for f97ae865ad6d_demoapp_api_1_6f198d4b1db4  Cannot start service api: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"./main\": stat ./main: no such file or directory": unknown

ERROR: for api  Cannot start service api: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"./main\": stat ./main:no such file or directory": unknown
ERROR: Encountered errors while bringing up the project.

Inside the container file ./main exists

MBP-Pavel:demoapp pavel$ docker run --rm -it 4ae6ff9cf413 sh
# pwd
/usr/src/app
# ls
Dockerfile  main  main.go
#

If delete volumes from api service all is good, but I need to full rebuild my app (with downloading dependencies). What i'm doing wrong?

Pavel Perevezencev
  • 2,596
  • 3
  • 28
  • 49

2 Answers2

1

When running the container via docker-compose, your demoapp-api folder is mounted on top of /usr/src/app inside the container, while your go app is in that very folder in the container. Since demoapp-api does not contain a main binary, docker can't find and execute it.

You need to compile your main .go locally in your demoapi-app folder and then run docker-compose up.

Antoine Boisier-Michaud
  • 1,575
  • 2
  • 16
  • 30
protenhan
  • 35
  • 8
  • I want compile and run my application inside docker. And inside last container of build `docker-compose` have a binary file `./main` – Pavel Perevezencev Dec 17 '18 at 06:09
  • But then it is perfectly fine to remove the `volume` section in your `docker-compose.yaml`, since you are building the container anyway, every time you update your sources. Our do you also want to recompile the go application inside the container, when it is already running? – protenhan Dec 17 '18 at 20:16
  • No, i want run `docker-compose up` command insted of `docker-compose up --build` – Pavel Perevezencev Dec 18 '18 at 10:40
  • I think now I understand what you are trying to achieve. You just want to recompile the Go application on `docker-compose up` but not rebuild the full Docker image. Is that correct? The issue, from my experience, is that after a `COPY` statement Docker will invalidate the cache and perform every step afterwards every time. – protenhan Dec 18 '18 at 19:45
  • At least for Go you could circumvent that issue by [vendoring your dependencies](https://stackoverflow.com/questions/30300279/golang-dependency-management-best-practice) – protenhan Dec 18 '18 at 19:54
-1

guy from above means that your volume clean up your main binary file, so you can't find that inside docker container. I think it will better to remove your volume if that not so necessary, or just run it by 'go run main.go' but that is not a good way to do this. Also you could move folder, that contains .go files to for example src folder and build it like 'go build src/main.go' so in this case your volume shouldn't clear up your binary.