From b2a0b18cb4e00d9fd088d06bbea0babb787ef057 Mon Sep 17 00:00:00 2001 From: Rohan Verma Date: Fri, 15 Nov 2019 12:51:37 +0530 Subject: [PATCH] fix: update dockerfile for gopls blog --- content/blog/2019/2019-11-05-docker-gopls.md | 85 ++++++++++++++------ 1 file changed, 59 insertions(+), 26 deletions(-) diff --git a/content/blog/2019/2019-11-05-docker-gopls.md b/content/blog/2019/2019-11-05-docker-gopls.md index c1a6754..ad9a688 100644 --- a/content/blog/2019/2019-11-05-docker-gopls.md +++ b/content/blog/2019/2019-11-05-docker-gopls.md @@ -36,33 +36,37 @@ Let us look at the Dockerfile for this system. ```Dockerfile 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 -# 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 -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/gopls@v0.2.0 +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 @@ -77,13 +81,28 @@ inside the container. Finally, we install gopls in the container and then start it in the container 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. ```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, @@ -96,8 +115,22 @@ flags we use for the language server to connect to the remote gopls server. 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 -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 in the comments if this was helpful for you. \ No newline at end of file