Setting up your environment
Setting up a development environment, as well as a production environment, is an important topic today. While spinning up a virtual machine and installing software by hand is perhaps the accepted way of doing things, recently I’ve learned to systematize my development by using Docker containers.
The biggest benefit of Docker containers is a “zero install” way of running software - as you’re about to run a container, it downloads the image containing all the software dependencies you need. You can take this image and copy it to your production server, where you can run it without any change in environment.
Networking
When you have docker set up, we will need to create a network so the services that we’re going to use can talk to each other. Creating a network is simple, all you need to do is run the following command:
$ docker network create -d bridge --subnet 172.25.0.0/24 party
This command will create a network named party on the specified subnet. All docker containers which will run on this network will have connectivity to each other. That means that when we run our Go container, it will be able to connect to another Redis container on the same network.
Setting up a runner for your Go programs
There is an official Go image available on Docker. Getting a Docker image and running it is very simple, requiring just one line:
$ docker run --net=party -p 8080:80 --rm=true -it -v `pwd`:/go/src/app -w /go/sr\
c/app golang go "$@"
Save this code snippet as the file ‘go’, make it executable and copy it to your execution path (usually /usr/local/bin is reserved for things like this).
Let’s quickly go over the arguments provided:
- –net=party - runs the container on the shared network
- -p - network forwarding from host:8080 to container:80 (HTTP server)
- –rm=true - when the container stops, clean up after it (saves disk space)
- -v option - creates a volume from the current path (pwd) in the container
- ”$@” - passes all arguments to go to the container application
Very simply, what this command does is run a Docker container in your
current folder, execute the go binary in the container, and clean up
after itself when the program finishes.
An example of running go would be:
$ go version
go version go1.6 linux/amd64
And, what we will do through most of this book is run the following command:
- go run [file] - compile and run Go program
$ go run hello_world.go
Hello world!
1 package main
2
3 import "fmt"
4
5 func main() {
6 fmt.Printf("Hello world!\n")
7 }
Setting up Redis
We will also use Docker to run Redis, which is as simple as setting up Go. Redis is an in-memory data structure store. It provides functionality to store and retrieve data in various structures, beyond a simple key-value database like Memcache. We will use this service later in the book when implementing our example API endpoints.
To run an instance of Redis named ‘redis’:
$ docker run --restart=always -h redis --name redis --net=party -d redis
Just like Go, Redis provides an official build on the Docker Hub. Interacting with Redis will be covered in detail in later chapters.
Other services
In addition to Go and Redis, other services are also available on the Docker Hub. Depending on your use case, you might want to install additional services via docker.
Popular projects which I use from Docker on a daily basis:
- nginx (and nginx-extras flavours)
- Percona (MySQL, MariaDB) - also covered later in the book
- letsencrypt
- redis
- samba
- php
Docker is a very powerful tool which gives you all the software you might need. It’s very useful also for testing, as you can use it to set up a database, populate it with test data, and then tear down and clean up after it. It’s a very convenient way to run programs that are isolated from your actual environment, and may only be active temporary.