With everything in place, we can now talk about how we want to deploy our chat server!

We want our builds to be repeatable and our deploys to be contained. We will however still want some flexibility to handle the fact that our database is actually a file, and building and deploying a new version of our application shouldn't require us to copy a potentially very large file around.

In order to retain these advantages whilst still allowing for decent flexibility, we are going to use Docker containers. Docker maintains several install guides for whatever operating system you might be using. You will need to install it to continue with the next sections.


The central part of the Docker setup is the Dockerfile. We need to set one up that creates a minimal environment for our chat server whilst still supporting every piece we need. So, thinking about the specifics of what we want is important.

For the chat server, we need access to a file system in order to load our HTML files and to store our file-based database, SQLite. We also need a port to be exposed from the container so we can connect to it. Lastly, we need sufficient packages to build our Go code. Because we are using SQLite, a C program, our program now requires cgo to build. cgo allows our Go program to call out to C code directly, rather than running a C program as a separate service an connecting via some other means. If you want to see some examples of cgo, you should take a look at the Go package that GORM uses to interface with SQLite - https://github.com/mattn/go-sqlite3

With that, let's make the Dockerfile.

This will set the port number to 8080, set a location for our database and install dependencies.

An important point to note here is the VOLUME section for our database. A volume is a concept in Docker that allows a container to access a file on the underlying host. This is super important for us here, because if we didn't have it, our database file would be inside the container. That would mean that if we ever recreated our container, our database would be recreated when we started it up. However, because we are using a volume here, Docker will keep track of our database file outside of our container, so every change we make that involves recreating the container will not get rid of all of our data.

We can build our Dockerfile by running docker build --file Dockerfile . --tag chat-server in our project directory. This will create the container of our chat server build and tag it with chat-server for ease of use. You can confirm this worked with docker images and looking for chat-server latest.


This page is a preview of Reliable Webservers with Go

Start a new discussion. All notification go to the author.