2

I am trying to run my simple golang API in a docker container so that it can be run across all machines, when I use docker tool box to navigate to the directory and run docker-compose up I get an error saying that stat main.go: no such file or directory I think it may have to do with the paths on my dockerfile/docker compose? Could anyone take a look and help me out please?

Dockerfile

FROM golang:latest

RUN mkdir -p /go/src/app
WORKDIR /go/src/app

ADD . /go/src/app

RUN go get -v

docker-compose.yml

version: '3'
services:
     db:
         image: postgres
     environment:
         POSTGRES_DB: Shoes
         POSTGRES_USER: postgres
         POSTGRES_PASSWORD: root
     ports:
         - 5432:5432
     app:
       build: .
       command: ["go", "run", "main.go"]
       volumes:
         - .:/go/src/app
     ports:
         - "8080:8080"
     depends_on:
         - db
     links:
         - db

Edit: New error after rearranging some files and getting my GOPATH working properly. I am having a problem connecting to the Database in docker I get my error message for when I cant connect.

dmc94
  • 536
  • 1
  • 5
  • 16
  • Add `GOPATH` to your Dockerfile using `ENV GOPATH` – dmitryro Oct 01 '18 at 01:26
  • check `docker run --rm golanng:latest printenv` and check the output for GOPATH – Rajesh Rajendran Oct 01 '18 at 01:28
  • @RajeshRajendran it says my is GOPATH=/go do I just place ENV GOPATH at the top of my dockerfile? Sorry I am brand new to docker like an hour into it haha – dmc94 Oct 01 '18 at 01:30
  • @dmitryro it says my is GOPATH=/go, shouldn't it be /go/src? – dmc94 Oct 01 '18 at 01:30
  • Depending on where your *go* is installed, for example if your `which go` will tell you `/usr/local/go/bin/go`, then `GOPATH` should be `/usr/local/go`. – dmitryro Oct 01 '18 at 01:32
  • @dmitryro so its /c/Go/bin/go so would I just add ENV GOPATH=/go to my dockerfile? – dmc94 Oct 01 '18 at 01:33
  • So it's whatever is prior to `/bin/go`. – dmitryro Oct 01 '18 at 01:34
  • @dmitryro I set ENV GOPATH=/Go and I still get the same error – dmc94 Oct 01 '18 at 01:36
  • In your Dockerfile remove `RUN mkdir -p /go/src/app`, add `COPY . /go/src/app` then next line `WORKDIR /go/src/app` - then somewhere later in Dockerfile `RUN go run main.go` - It's your `docker-compose` `command: ["go", "run", "main.go"]` command that fails. Don't forget to rebuild your Docker image. – dmitryro Oct 01 '18 at 01:50
  • @dmitryro Now I am getting an error from my code that says I cant connect to the database. (is that because I have hardcoded database credentials?) I am only getting that error within docker – dmc94 Oct 01 '18 at 01:52
  • That's already a different error - verify you specified valid credentials for Postgres – dmitryro Oct 01 '18 at 01:54
  • @dmitryro I did in the compose file. I removed `command: ["go", "run", "main.go"] and then I received no error but app_app_1 exited with code 0. It also just says that database system was shut down at a specific time – dmc94 Oct 01 '18 at 01:57
  • Watch your terminal and logs that relate to database service in docker-compose - it's already not a go issue. – dmitryro Oct 01 '18 at 01:59
  • I'd suggest to create a separate Dockerfile that sets up all the necessary things for your database connection - see here some more https://stackoverflow.com/questions/34751814/build-postgres-docker-container-with-initial-schema – dmitryro Oct 01 '18 at 02:31

0 Answers0