Need help to create Docker container in Go workspace (multi-modular) mode
Asked Answered
P

1

7

I need help, I'm working in the go workspace and there are multiple projects in the workspaces using the common functionality. So, What I did is to make a separate module that contains all the common functionalities that are used by the other modules.

All modules can share their code as we are using the go workspace.

The file structure is as follows:

Workspace
│
├── Project1
│      ├── main.go
│      ├── docker-compose.yml
│      ├── Dockerfile
│      └── go.mod
│
├── Project2
│      ├── main.go
│      ├── docker-compose.yml
│      ├── Dockerfile
│      └── go.mod
│
├── Common
│      ├── functionality.go
│      └── go.mod
│
└── go.work

Project1 and Project2 use the functionalities that are in the common/functionality.go by importing them into the project.

when I run the docker-compose up -d command in project1 It says the common module that you import in the project is not in the GOROOT. because here docker only containerizes the files that are in the directory Project1

How can I dockerize separately Project1 and Project2 ???

Premium answered 28/6, 2022 at 9:37 Comment(0)
R
3

To compile your application the compiler requires all imported packages. When you build a docker image the context determines what files are available; in your case this will be Project1 (for example). This means that when you run go build in your Dockerfile the only files available are those in the Project1 folder. The compiler will check for a workspace file but will not find one (because it's not in the context).

You can work around this by changing the context such that it includes the full workspace e.g. Project1/docker-compose.yml:

project1:
    build:
      context: ../
      dockerfile: Project1/dockerfile
    environment:
      ....

(you will need to update your Dockerfile as well because you will be copying in subdirectories etc).

However the above is not really ideal (it somewhat defeats the point of putting the projects in separate folders). Depending upon your release process a better option might be to check everything in (e.g. to a Git repo), do a go get -u (to update the go.mod), and then build the docker image (allowing go to retrieve the dependencies from the repo).

Ref answered 28/6, 2022 at 10:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.