In my development series regarding Docker containers for the web application developer, I had already gone over deploying Java and Node.js applications as containers alongside Couchbase Server Containers.  This time around I thought it would be cool to deploy a Golang web application as a Docker container alongside Couchbase.

The process is very similar to what I had already demonstrated in the previous tutorials, but it is worth exploring.  We’re going to explore containerizing an application that I had explained in a previous tutorial on the topic of URL shortening services.

If you’re interested, you can check out Use Docker to Deploy a Containerized Java with Couchbase Web Application and Deploy a Node.js with Couchbase Web Application as Docker Containers, depending on your language preferences.

The Requirements

There is only one dependency that must be met to be successful with this guide.  You need Docker installed on a host machine.  You don’t need Golang installed or Couchbase Server since that is the beauty of the Docker Engine.

While you don’t have to, I would recommend becoming familiar with the application in question.  To learn about the code behind the application, check out the tutorial I wrote called, Create a URL Shortener with Golang and Couchbase NoSQL.

Establishing a Docker Project for the Golang Application

The source code to the application, and what we’ll be bundling, can be seen below.  You should save it as a Go source code file such as main.go.

The only difference between the code above and the previous tutorial on the subject is the use of the os.Getenv("COUCHBASE_HOST") and os.Getenv("COUCHBASE_BUCKET") commands.  This will allow us to define the Couchbase connection information at container runtime via environment variables rather than hardcoding it into the application.

If you had Couchbase Server running, you could launch the web application by doing:

Of course the project would need to be in your $GOPATH, but it would be accessible from http://localhost:12345 as defined in the code.

The goal here is not to run this project locally, but via a Docker container.  Create a Dockerfile file next to the main.go file or whatever you called it.  The Dockerfile file should contain the following:

As a base we’ll be using the official Golang Alpine Linux image for Docker, but we’ll be customizing it.  During build time we are installing Git, copying the main.go file to the image, installing the project dependencies found in the import section, and installing the application.  At runtime we are executing the installed application.

To build an image from our custom Docker blueprint, we would execute the following:

After all the compile-time steps complete we’ll be left with a golang-project Docker image.  Before we try to run this image, we need to worry about Couchbase.  While we could run Couchbase outside a container, where is the fun in that?

Creating a Custom Couchbase Server Docker Image

Just like with Golang, an official Docker image exists for Couchbase Server.  While it is a perfectly acceptable solution, it is not an automated solution.  This means that you’ll have to manually configure the database after the container starts.  We probably want to avoid that.

Instead, create a directory somewhere on your computer with a Dockerfile file and configure.sh file in it.  The plan is to create a Docker blueprint that will execute the configuration script for us.

Open the Dockerfile file and include the following:

We’re basically just copying the script into the base image and running it.  The real magic happens inside of the script.

Open the configure.sh file and include the following:

Couchbase Server has a RESTful interface which will allow us to complete the configuration wizard through a script.  The above commands will define instance specs and services, administrative credentials, and a Bucket.

You’ll notice things like $COUCHBASE_ADMINISTRATOR_USERNAME within the script.  Just like in the Golang application we are leveraging environment variables to prevent having to hardcode values into our image.  These values can be defined during the container deployment.

More information on provisioning a Couchbase Server container can be found in a previous article that I wrote on the subject.

Finally we can build this custom Couchbase Server image.  From the Docker Shell, execute the following:

The above command will leave us with a couchbase-custom image.

Deploying the Containerized Microservice

There are a few ways to deploy the images that we built.  We’ll explore two of them here.

Given what we know about our images, we can deploy our containers with the following commands:

The above command will deploy Couchbase Server while mapping the necessary ports to the host operating system.  We’re passing each environment variable in with the command and defining an operating network.  The network is important because you want both your database and application to be on the same container network.

The above command will deploy our Golang image while mapping the correct ports again.  For the host you’ll notice that we’re using couchbase which is actually the name of our other container.  The container names also act as host names.

The commands you just saw are a very manual way to do things in my opinion.  Instead you can create a Docker Compose file to handle this for you.

Create a docker-compose.yml file somewhere on your computer with the following:

Everything in a Compose file will be on the same network.  To deploy each service, execute the following:

If you’re familiar with Docker Compose you’ll be familiar with docker-compose up, but we can’t use that here.  Couchbase Server doesn’t have a mechanism to tell us it is ready, so we don’t want the Golang application to try to connect before it is configured.  Definitely not a big deal when it comes to container deployment.

Conclusion

You just saw how to create a Golang microservice as a Docker container that communicates to Couchbase Server, also running within a Docker container.  By turning your application into a Docker image it becomes very easy to distribute because you won’t have to worry about the environment you’re trying to deploy to.  As long as the Docker Engine is available you can deploy your application.  A useful example of this would be in continuous deployment of containers using Jenkins.

Want to see how to containerize your Java application?  Check out this tutorial I wrote.  I also have a tutorial for containerizing a Node.js application here.

Want more information on using Golang with Couchbase? Check out the Couchbase Developer Portal for documentation and examples.

Posted by Nic Raboy, Developer Advocate, Couchbase

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.

One Comment

  1. […] Deploy a Golang Web Application and Couchbase as Docker Containers (Nic Raboy) […]

Leave a reply