Basic Docker Setup for HTTP Server (using docker-compose)
·
281 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
|
|
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.
|
|
Once everything is set correctly, it should be all running and look like this: