Docker compose не может найти локальный пакет - PullRequest
2 голосов
/ 24 сентября 2019

Итак, я создал REST api и один сервис grpc в GOlang, и теперь я хочу создать его в docker compose.Мой docker-compose выглядит так:

version: '3'
services: 
  db:
    image: postgres
    environment:
      POSTGRES_DB: db_1
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: tajna
      PGDATA: /tmp
    volumes:
      - ./db/sql/user/1_create.sql:/docker-entrypoint-initdb.d/1-create_user.sql
      - ./db/sql/item/2_create.sql:/docker-entrypoint-initdb.d/2-create_item.sql
    ports:
      - 5432:5432
    networks: 
      apinetwork:
  service:
    build: ./item-service
    command: ["go", "run", "."]
    volumes:
      - .:/go/src/github.com/ajdinahmetovic/item-service
    ports:
      - "4040:4040"
    depends_on: 
      - db
    links:
      - db
    networks: 
      apinetwork:
  rest:
    image: go-rest
    build: 
      context: ./go-rest
      dockerfile: Dockerfile
    command: ["go", "run", "main.go"]
    volumes:
      - .:/go/src/github.com/ajdinahmetovic/go-rest
    ports:
      - "3000:3000"
    depends_on:
      - service
    networks: 
      apinetwork:
networks:
  apinetwork:
    driver: bridge

После запуска docker-compose я получаю сообщение об ошибке, что локальные пакеты, которые находятся внутри go-rest и item-service, не могут быть найдены.

service_1  | item.go:7:2: no Go files in /go/src/github.com/ajdinahmetovic/item-service/db
service_1  | main.go:8:2: cannot find package "github.com/ajdinahmetovic/item-service/logger" in any of:
service_1  |    /usr/local/go/src/github.com/ajdinahmetovic/item-service/logger (from $GOROOT)
service_1  |    /go/src/github.com/ajdinahmetovic/item-service/logger (from $GOPATH)
service_1  | item.go:8:2: cannot find package "github.com/ajdinahmetovic/item-service/proto/v1" in any of:
service_1  |    /usr/local/go/src/github.com/ajdinahmetovic/item-service/proto/v1 (from $GOROOT)
service_1  |    /go/src/github.com/ajdinahmetovic/item-service/proto/v1 (from $GOPATH)
ajdinahmetovic_service_1 exited with code 1
rest_1     | /go/src/github.com/ajdinahmetovic/go-rest/routes/item/post.go:9:2: no Go files in /go/src/github.com/ajdinahmetovic/go-rest/db
rest_1     | /go/src/github.com/ajdinahmetovic/go-rest/routes/refreshToken.go:9:2: cannot find package "github.com/ajdinahmetovic/go-rest/httputil" in any of:
rest_1     |    /usr/local/go/src/github.com/ajdinahmetovic/go-rest/httputil (from $GOROOT)
rest_1     |    /go/src/github.com/ajdinahmetovic/go-rest/httputil (from $GOPATH)
ajdinahmetovic_rest_1 exited with code 1

Однако, когда я перемещаю составной файл Docker в папку go-rest, приложение rest запускается успешно, но моя служба элементов не работает.Как я вижу, проблема в моем пути к приложениям.

1 Ответ

0 голосов
/ 25 сентября 2019

Через пару часов я наконец понял, в чем проблема.Мой объемный путь для item-service и go-rest был неверным.Потому что мой файл docker-compose находится на одном уровне с папками проектов item-service и go-rest

-go-rest
 -Project files
-item-service
 -Project files
-docker-compose.yaml

, а мои пути к томам были

volumes:
      - .:/go/src/github.com/ajdinahmetovic/go-rest
volumes:
      - .:/go/src/github.com/ajdinahmetovic/item-service

Они не смоглидоступ к папке моего проекта, поэтому их нужно изменить на

volumes:
      - .:/go-rest
volumes:
      - .:/item-service
...