R.Roshan
R.Roshan

Reputation: 199

golang build by copying vendor folder

My Project structure

/github.com/user
 - libraries
 - services
     - service-api-signup
          - Dockerfile
          - main.go
     - service-api-second
          - ...
 - vendor

Docker file inside service-api-signup

FROM golang
COPY . /go/src/github.com/user/services/service-api-signup

# need to copy all the dependencies on vendor to /go/src/github.com/..

How can copy all my dependencies on vendor and libraries folder to go path on docker image to build ? (need to copy from parent directory and build)

Any way to setup my development environment ?

Thanks.

Upvotes: 1

Views: 2889

Answers (2)

Zak
Zak

Reputation: 5898

Assuming you have a kind of monorepo setup, with vendor at the top level and then dockerfiles in each of the sub directories (/services) you could use a top level docker-compose.yml file (docs) to define all the services and then use volume mounts to ensure that vendor ends up inside the containers.

Taking your example

.../repo
 - libraries
 - services
     - service-api-signup
          - Dockerfile
          - main.go
     - service-api-second
          - ...
 - vendor
 - docker-compose.yml

The docker-compose.yml would look something like:

version: '3'
services:
  service-api-signup:
    container_name: api-signup
    build:
      context: ./service-api-signup
    volumes:
      - ./vendor:/go/src/github.com/user/services/service-api-signup/vendor
  service-api-second:
    container_name: api-second
    build:
      context: ./service-api-second
    volumes:
      - ./vendor:/go/src/github.com/user/services/service-api-second/vendor

There's also an option to override the command and entrypoint of a dockerfile allowing you to set custom scripts as the entrypoint when running through docker-compose

entrypoint: /go/src/github.com/user/services/service-api-signup

Maybe that script (that you would need to create and put in your repo somewhere) could make use of a live reload tool like gin and might look like:

#!/bin/bash

go get github.com/codegangsta/gin

exec gin --immediate -appPort 8080 run

This would still mean that you are building the application inside docker (which in OS's that aren't linux will mean worse performance). But it does mean that you get docker-compose convenience, live reload and don't need to rebuild containers.

Upvotes: 0

Jonathan Hall
Jonathan Hall

Reputation: 79586

The normal way is to use a dependency manager such as dep.

An alternative is just to copy the vendor directory directly in your Dockerfile, but this is not ideal, as it will copy the entire vendor directory verbatim, even if it contains old or unused imports.

Upvotes: 1

Related Questions