Docker Powered Golang Development on Windows Without Installing Go

25 Aug, 2019

containers

Photo by frank mckenna

Containers are a solution to develop an application without actually installing the SDK. Despite the use of Golang, this technique should work with any programming language. The idea is to run our source code in a container while being able to change it outside the container

Using Bind Mounts By Sharing Drives

Share the physical drive containing the source code with Docker

# RUN
docker run --rm -it -v D:/source/textparser:/app alpine

# --rm : removes container after the usage
# -it : starts an interactive shell inside the container
# -v : bind mounts D:/source/textparser on local machine to app inside the container
# alpine : is the docker image

VS Code has Remote - Containers extension to support developing in a container. The extension uses same concept to spin up a container & bind mount the source directory

Despite the simple process, sharing drives might not always be possible. It may need admin privileges or (this may no longer be the case as Docker Desktop v2.15.0 runs many processes within user identity) might not be possible at all in secure environments. Let’s look at a way to achieve the same thing without sharing drives with Docker

Using Docker Volume with Samba

Start a samba container with persistent volume & share it over the network

# RUN
docker run -d --name godisk --net=host -e USERID=0 -e GROUPID=0 -v godisk:/godisk dperson/samba -s "godisk;/godisk;yes;no"

# -d : starts the container detached from active shell
# --name : names the container as godisk
# --net=host : shares the network namespace of host with container
# -e USERID=0 -e GROUPID=0 : sets environment variables USERID & GROUPID
# -v : creates or uses a persistent volume godisk mounted in the container
# dperson/samba : is the docker image
# -s : arguments to configure samba network share godisk (https://github.com/dperson/samba#configuration)

Now the persistent volume is available at \\10.0.75.2\godisk as a network share. It’s time to start a new golang container mounted with godisk volume

# RUN
docker run -d -v godisk:/go/src --name godev golang:alpine

# -v : uses godisk volume & mounts it to /go/src inside the container
# --name : names the container as godev
# golang:alpine : is the docker image

The godev container uses the same persistent volume godisk

# RUN
docker exec -it godev sh

# exec : executes the command in container
# godev : is the container name
# sh : is the command to execute

Open \\10.0.75.2\godisk in any IDE & start writing golang

It’s not a straight forward setup but it’s a one time effort. The containers shutdown when not in use & can start on demand by docker start -i godev. This doesn’t need admin privileges but may not be safe as mentioned in a post that inspired the thought. But, it works & that’s a good thing!