| ```Dockerfile | ```Dockerfile | ||||
| FROM ubuntu:xenial AS builder | FROM ubuntu:xenial AS builder | ||||
| RUN apt-get update | |||||
| # Install system dependencies that we might need | |||||
| RUN apt-get install -y gcc g++ | |||||
| RUN apt-get update && \ | |||||
| apt-get install -y gcc g++ libssl-dev cmake wget libpcre3 libpcre3-dev git | |||||
| # Install Depencies like Go and Swig in our container. | |||||
| WORKDIR /tmp | WORKDIR /tmp | ||||
| # Install Go | |||||
| RUN wget https://dl.google.com/go/go1.12.9.linux-amd64.tar.gz | |||||
| RUN tar -C /usr/local -xzf go1.12.9.linux-amd64.tar.gz | |||||
| # Install Swig | |||||
| RUN wget http://prdownloads.sourceforge.net/swig/swig-4.0.1.tar.gz | |||||
| RUN tar -zxvf swig-4.0.1.tar.gz | |||||
| RUN cd /tmp/swig-4.0.1 && ./configure && make && make install | |||||
| # Create user | |||||
| RUN useradd -m -r -u 1000 myuser | |||||
| RUN groupmod -g 1000 myuser | |||||
| # Replicate/fake the host system tree | |||||
| RUN mkdir -p /home/myuser/code/go/myhostdir/myproject | |||||
| RUN chown -R myuser:myuser /home/myuser/code/go/myhostdir/myproject | |||||
| # Install Go. | |||||
| RUN wget https://dl.google.com/go/go1.12.9.linux-amd64.tar.gz && \ | |||||
| tar -C /usr/local -xzf go1.12.9.linux-amd64.tar.gz | |||||
| # Install Swig. | |||||
| RUN wget http://prdownloads.sourceforge.net/swig/swig-4.0.1.tar.gz && \ | |||||
| tar -zxvf swig-4.0.1.tar.gz && \ | |||||
| cd /tmp/swig-4.0.1 && ./configure && make && make install | |||||
| ARG MY_USER_ID | |||||
| ENV MY_USER_ID ${MY_USER_ID} | |||||
| ARG MY_PWD | |||||
| ENV MY_PWD ${MY_PWD} | |||||
| RUN mkdir -p $MY_PWD | |||||
| RUN useradd -r -m -u $MY_USER_ID myuser | |||||
| RUN groupmod -g $MY_USER_ID myuser | |||||
| USER myuser | USER myuser | ||||
| WORKDIR /home/myuser/code/go/myhostdir/myproject | |||||
| ENV PATH="/usr/local/go/bin:${MY_PWD}/go/bin:${PATH}" | |||||
| RUN GOPROXY=https://proxy.golang.org GO111MODULE=on go get golang.org/x/tools/[email protected] | |||||
| WORKDIR $MY_PWD | |||||
| RUN GO111MODULE=on go get golang.org/x/tools/gopls@latest | |||||
| CMD gopls -listen=":7050" | |||||
| CMD /home/myuser/go/bin/gopls -listen=":7050" | |||||
| ``` | ``` | ||||
| Firstly, we install all the dependencies and then create | Firstly, we install all the dependencies and then create | ||||
| Finally, we install gopls in the container and then start it in the container | Finally, we install gopls in the container and then start it in the container | ||||
| with the `-listen=":7050"` flag. | with the `-listen=":7050"` flag. | ||||
| We will now build the image. | |||||
| ```bash | |||||
| mkdir -p go | |||||
| docker build \ | |||||
| --build-arg MY_PWD=${PWD} \ | |||||
| --build-arg=MY_USER_ID=${MY_USER_ID} \ | |||||
| -t myapp:latest . | |||||
| ``` | |||||
| Note, we also create a directory called `go` which will be the mock GOPATH | |||||
| inside the container. | |||||
| Now we can expose this port to our host machine. | Now we can expose this port to our host machine. | ||||
| ```bash | ```bash | ||||
| docker run -d --name "myproject" -u `id -u` -p 7050:7050 \ | |||||
| -v /home/myuser/code/go/myhostdir/myproject:/home/myuser/code/go/myhostdir/ \ | |||||
| myproject myproject:latest | |||||
| docker run -d --name "myapp" \ | |||||
| -u `id -u ${USER}` \ | |||||
| -e "GOPATH=${PWD}/go" \ | |||||
| -p 7050:7050 \ | |||||
| -v ${PWD}:${PWD} myapp:latest | |||||
| ``` | ``` | ||||
| Notice, that we also mount the host source in the container with the same path, | Notice, that we also mount the host source in the container with the same path, | ||||
| gopls -remote "localhost:7050" | gopls -remote "localhost:7050" | ||||
| ``` | ``` | ||||
| For VSCode, you can add the following to your `settings.json` | |||||
| ```json | |||||
| { | |||||
| "go.languageServerFlags": [ | |||||
| "-remote=localhost:7050", | |||||
| "-v" | |||||
| ], | |||||
| "go.useLanguageServer": true, | |||||
| "go.gopath": "go" | |||||
| } | |||||
| ``` | |||||
| After adding this flag, you will now be able to edit your source with all the | After adding this flag, you will now be able to edit your source with all the | ||||
| added benefits that come with a language server. | |||||
| added benefits that come with a language server like autocompletion and | |||||
| autoformatting. | |||||
| Since this is an experimental feature, it might break (a lot). Do let me know | Since this is an experimental feature, it might break (a lot). Do let me know | ||||
| in the comments if this was helpful for you. | in the comments if this was helpful for you. |