We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Creating an Environment

You may be thinking, "What's the point of dockerizing this simple service"? Well, at the moment, there are only a couple of benefits:

  • Anyone with Docker can run your image, regardless of their OS
  • You can easily deploy containers of your image on any cloud service that uses images (most of them) or on an orchestration server like Kubernetes.
  • If your server were written in a language like Python or JavaScript, you could bundle the interpreter and dependencies inside the image so that you don't need to reconfigure them on the server.

That said, because our app is so simple, there's just not much environment required, and one of the best things about Docker is that it allows you to ship an entire environment.

So... let's make it more interesting!

Assignment

We're going to make the port that our server binds to configurable: it will be set by an environment variable.

port := os.Getenv("PORT")

Make sure that the os package is imported:

import (
	"fmt"
	"log"
	"net/http"
	"os"
	"time"
)
export PORT="8999"
go build
./goserver
ENV PORT=8991

If you're not on Linux, rebuild the Go binary for Linux first: GOOS=linux GOARCH=amd64 go build

docker build . -t goserver:latest
docker run -p 8991:8991 goserver

Open http://localhost:8991 in your browser or curl it to confirm the container is serving on the Docker-set port.

Run and submit the CLI tests from your working directory against http://localhost:8991.