<commit_before>---
layout: post
categories: ["tips", "tutorias"]
tags: ["tips", "tutorias"]
title: "Automated golang Deployments"
date: "2017-01-15 21:00 -0500"
published: True
comments: True
---

# Intro
In this post I am going to create an outline for the process that I have successfully used to automatically test and deploy golang web applications to a production ready cloud environment using Docker, CircleCi, GitHub, and Digital Ocean. 

Using Docker as a contianerization service as well as an orchestration layer has allowed me to support clustering and scaling of my web applications in a seamless manor.

When coupled with a continuous integration service like CircleCi I have been able to perform deploys using just a single code commit that include end to end testing for the applications for each deploy. 

# Set up a repository on GitHub

While this is a bit out of scope for this guide you can follow the steps [here](https://help.github.com/articles/create-a-repo/) to get a repository started for the application. The important thing here is that the repository is made and houses the application as well as the configuration files that will be described throughout the guide. This will be the primary means of notifying CircleCi whenever a change has been made to the application and provides a central resource of configuation for CircleCi and Docker.

# Setting up a Golang web app

In this section I will set up a simple golang web application using the http library provided by the standard library. I am not going to go into much depth as to what the process is here for setting up and getting started with go, I assure you there are many great tutorials just a quick search away.

First I create the following file in my golang projects directory.

main.go
```golang
package main

import (
    "fmt"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "I love Go!")
}

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":80", nil)
}
```
If you have your gopath set up then at this point you're all set to build the application if you wish to try things out.

Otherwise if you haven't set up the path yet or if you're not sure how then the next section will actually outline the process of containerizing the application in a docker container. So technically you don't even need to install the golang toolkit on your computer as the docker container will contain all of the dependencies and requirements for building and running the golang app. This is really where Docker shines as you can do all of your development locally testing your changes against a docker container. Once you're done adding the feature all you'll need to do is push up the container to Dockerhub and it is ready to go!


# Docker
## Writing a Dockerfile
The first step is to write a Dockerfile using the oficially maintained golang  container.

Dockerfile
```
FROM golang:latest
# Put your info on the following line
MAINTAINER <NAME> <<EMAIL>>

# Create the directory for the application to run from
RUN mkdir /app 

# Add the contents of the current directory to the dockerimage
ADD . /app/ 

#Set your working directory
WORKDIR /app 

# While not needed for this particular example you can include any dependencies
# here that you need for your app.
# The following is an example of getting the mysql package
RUN go get github.com/go-sql-driver/mysql

# Build the application
RUN go build -o main . 

# Set the port that your web application communicates on
EXPOSE 80:80

# This is the command that runs by defualt when the container is run, include
# any flags or arguments for your app here.
CMD ["/app/main"]
```

Now we just need to build and test our app and contianer replacing the repository owner and name where appropriate:

```bash
docker build -t . derfoh/golang-deploys-tutorial
docker run -it -p 80:80 derfoh/golang-deploys-tutorial
```
Note that we have specified the port with the `-p 80:80` tag this tells docker that you want the internal port 80 on the container to listen on port 80 of the host. You can change this to your liking but since our application is already written to listen on port 80 for this example we will continue to use the standard http port. 

Once you have navigated to [http://localhost/](http://localhost/) you will be greeted by the message "I love Go!". This confirms that the application was properly built and ran. Eventually we will automate this test using a simple `curl` defined within the CircleCi settup.

To exit the container use ctrl+c.

Next we need to push the initial container to Dockerhub replacing my info with yours where appropriate
```bash
docker push derfoh/golang-deploys-tutorial
```

## Connecting Docker cloud and Docker hub
This section will detail how to put your docker image hosted at [hub.docker.com](http://hub.docker.com) into the docker cloud at [cloud.docker.com](http://cloud.docker.com).

After logging into [Docker Cloud](http://cloud.docker.com) you will want to go to the repository link along the left navigation bar.

Then select the container that you wish to deploy.

The next page will show some information about the application as well as some configuration options for it as well. We want to launch the service so go ahead and click on the button that says "Launch Service".

The next page that will come up will be the general settings for the application. 

We will want to toggle some options on this page. First turn on autoredeploy. This will tell Docker that as soon as the configurations are changed to automatically restart the container to reflect those configuration changes.

Scroll down to the ports section and enter port 80 for the node port. Tick the "Published" box, then click "Add Port". This tells dockercloud to publish the service on that port on that host. You can also set this to be alocated dynamically if you don't want to use port 80.

## Configuring Dockerhub service to auto deploy when a new image is pushed
This section will detail the process of automatically deploying when a new docker image is pushed to docker hub. The container living in docker cloud will automattically redeploy it's self as soon as a new docker build is available. This step while not required can be skipped if you prefer a manual deployment but for the sake of this guide we'll be automating the whole process from the git push to the deployed application.<commit_msg>Fixup code blocks<commit_after>---
layout: post
categories: ["tips", "tutorias"]
tags: ["tips", "tutorias"]
title: "Automated golang Deployments"
date: "2017-01-15 21:00 -0500"
published: True
comments: True
---

# Intro
In this post I am going to create an outline for the process that I have successfully used to automatically test and deploy golang web applications to a production ready cloud environment using Docker, CircleCi, GitHub, and Digital Ocean. 

Using Docker as a contianerization service as well as an orchestration layer has allowed me to support clustering and scaling of my web applications in a seamless manor.

When coupled with a continuous integration service like CircleCi I have been able to perform deploys using just a single code commit that include end to end testing for the applications for each deploy. 

# Set up a repository on GitHub

While this is a bit out of scope for this guide you can follow the steps [here](https://help.github.com/articles/create-a-repo/) to get a repository started for the application. The important thing here is that the repository is made and houses the application as well as the configuration files that will be described throughout the guide. This will be the primary means of notifying CircleCi whenever a change has been made to the application and provides a central resource of configuation for CircleCi and Docker.

# Setting up a Golang web app

In this section I will set up a simple golang web application using the http library provided by the standard library. I am not going to go into much depth as to what the process is here for setting up and getting started with go, I assure you there are many great tutorials just a quick search away.

First I create the following file in my golang projects directory.

main.go

```golang
package main

import (
    "fmt"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "I love Go!")
}

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":80", nil)
}
```

If you have your gopath set up then at this point you're all set to build the application if you wish to try things out.

Otherwise if you haven't set up the path yet or if you're not sure how then the next section will actually outline the process of containerizing the application in a docker container. So technically you don't even need to install the golang toolkit on your computer as the docker container will contain all of the dependencies and requirements for building and running the golang app. This is really where Docker shines as you can do all of your development locally testing your changes against a docker container. Once you're done adding the feature all you'll need to do is push up the container to Dockerhub and it is ready to go!


# Docker
## Writing a Dockerfile
The first step is to write a Dockerfile using the oficially maintained golang  container.

Dockerfile

```dockerfile
FROM golang:latest
# Put your info on the following line
MAINTAINER <NAME> <<EMAIL>>

# Create the directory for the application to run from
RUN mkdir /app 

# Add the contents of the current directory to the dockerimage
ADD . /app/ 

#Set your working directory
WORKDIR /app 

# While not needed for this particular example you can include any dependencies
# here that you need for your app.
# The following is an example of getting the mysql package
RUN go get github.com/go-sql-driver/mysql

# Build the application
RUN go build -o main . 

# Set the port that your web application communicates on
EXPOSE 80:80

# This is the command that runs by defualt when the container is run, include
# any flags or arguments for your app here.
CMD ["/app/main"]
```

Now we just need to build and test our app and contianer replacing the repository owner and name where appropriate:

```bash
docker build -t . derfoh/golang-deploys-tutorial
docker run -it -p 80:80 derfoh/golang-deploys-tutorial
```

Note that we have specified the port with the `-p 80:80` tag this tells docker that you want the internal port 80 on the container to listen on port 80 of the host. You can change this to your liking but since our application is already written to listen on port 80 for this example we will continue to use the standard http port. 

Once you have navigated to [http://localhost/](http://localhost/) you will be greeted by the message "I love Go!". This confirms that the application was properly built and ran. Eventually we will automate this test using a simple `curl` defined within the CircleCi settup.

To exit the container use ctrl+c.

Next we need to push the initial container to Dockerhub replacing my info with yours where appropriate

```bash
docker push derfoh/golang-deploys-tutorial
```

## Connecting Docker cloud and Docker hub
This section will detail how to put your docker image hosted at [hub.docker.com](http://hub.docker.com) into the docker cloud at [cloud.docker.com](http://cloud.docker.com).

After logging into [Docker Cloud](http://cloud.docker.com) you will want to go to the repository link along the left navigation bar.

Then select the container that you wish to deploy.

The next page will show some information about the application as well as some configuration options for it as well. We want to launch the service so go ahead and click on the button that says "Launch Service".

The next page that will come up will be the general settings for the application. 

We will want to toggle some options on this page. First turn on autoredeploy. This will tell Docker that as soon as the configurations are changed to automatically restart the container to reflect those configuration changes.

Scroll down to the ports section and enter port 80 for the node port. Tick the "Published" box, then click "Add Port". This tells dockercloud to publish the service on that port on that host. You can also set this to be alocated dynamically if you don't want to use port 80.

## Configuring Dockerhub service to auto deploy when a new image is pushed
This section will detail the process of automatically deploying when a new docker image is pushed to docker hub. The container living in docker cloud will automattically redeploy it's self as soon as a new docker build is available. This step while not required can be skipped if you prefer a manual deployment but for the sake of this guide we'll be automating the whole process from the git push to the deployed application.
{'fraction_non_alphanumeric': 0.04533105643095552, 'fraction_numerical': 0.00620973375766514, 'mean_word_length': 4.207760711398545, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5318578', 'n_tokens_mistral': 3339, 'n_tokens_neox': 3071, 'n_words': 2048}