Basic Docker Setup for HTTP Server (using docker-compose)

· 252 words · 2 minutes read

There are many guides on how to setup a docker container running Go, but the aim of this post is to provide a basic starting point - as often they become complicated and split across many files. It’s also aimed at getting it running locally for development purposes quickly, so it might not be production ready.

To begin with we create a basic http server, which just says status: ok on the root endpoint. We then create a Dockerfile and docker-compose.yml to define how the container should be built.

Example Go HTTP Server: main.go

package main

import (
    "io"
    "log"
    "net/http"
    "os"
)

func main() {

    http.HandleFunc("/", ExampleHandler)

    port := os.Getenv("PORT")
    if port == "" {
        port = "8080"
    }

    log.Println("** Service Started on Port " + port + " **")
    if err := http.ListenAndServe(":"+port, nil); err != nil {
        log.Fatal(err)
    }
}

func ExampleHandler(w http.ResponseWriter, r *http.Request) {
    w.Header().Add("Content-Type", "application/json")
    io.WriteString(w, `{"status":"ok"}`)
}

Dockerfile

FROM golang:1.11.5

ENV APP_NAME myproject
ENV PORT 8080

COPY . /go/src/${APP_NAME}
WORKDIR /go/src/${APP_NAME}

RUN go get ./
RUN go build -o ${APP_NAME}

CMD ./${APP_NAME}

EXPOSE ${PORT}

docker-compose.yml

version: '3'
services:
  web:
    build: .
    ports:
     - "8080:8080"

Once we’re all setup, we can use docker-compose to get us up and running. Note that this implementation doesn’t have any form of live reload, so any changes made you’ll need to stop the current container and rebuild on up with the command below.

docker-compose up --build

Once everything is set correctly, it should be all running and look like this:

basic docker setup

Image of Author Edd Turtle

Author:  Edd Turtle

Edd is the Lead Developer at Hoowla, a prop-tech startup, where he spends much of his time working on production-ready Go and PHP code. He loves coding, but also enjoys cycling and camping in his spare time.

See something which isn't right? You can contribute to this page on GitHub or just let us know in the comments below - Thanks for reading!