Run Go (golang) CLI from docker

Go is an open source programming language that makes it easy to build simple, reliable, and efficient software. Now we can run it from golang’s official docker ( https://hub.docker.com/_/golang/ ) with current terminal path.

First install Docker Engine.

curl -L https://get.docker.com | sudo bash -

Then add go run script into /usr/local/bin/go

You can custom go environment like run as user, gopath, goroot and forward port

#!/usr/bin/env bash
export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
echo "Current working directory: '"$(pwd)"'"
docker run -it --rm \
--user $(id -u):$(id -g) \
--env SSH_AUTH_SOCK=/ssh-auth.sock \
-v $SSH_AUTH_SOCK:/ssh-auth.sock \
-v /etc/passwd:/etc/passwd:ro \
-v /etc/group:/etc/group:ro \
-v "$(pwd)/goroot/github.com":/usr/local/go/src/github.com \
-v "$(pwd)/gopath/github.com":/go/src/github.com \
-v "$(pwd)":/usr/src/myapp \
-w /usr/src/myapp \
-p 8080:8080 \
-p 8443:8443 \
zercle/docker-alpine-golang \
go "[email protected]"
exit 0

And add execute permission.

sudo chmod +x /usr/local/bin/go
exit

Now we can use go’s cli from terminal. Test it run some command.

go version
go version
go version from docker
โซเชียล

Kawin Viriyaprasopsook

System administrator at Zercle Technology Co., Ltd.
System admin กากๆ
รักในการเล่นเกม
ชอบดู Anime ญี่ปุ่น
โซเชียล

Leave a Reply