> For the complete documentation index, see [llms.txt](https://docs.release.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.release.com/guides-and-examples/common-setup-examples/nginx-postgres-go-docker.md).

# Golang with Postgres and Nginx

{% hint style="danger" %}
Unfortunately, as of October 2022, this example application has been reported to us as broken. We will fix the example presented here to work with the new software release(s) shortly.
{% endhint %}

## Overview

In this walkthrough, we’ll look at the code and configuration required to deploy a Go server that connects to a Postgres database, running behind an Nginx proxy on Release.

You can find the completed code for the project [here](https://github.com/awesome-release/nginx-golang-postgres).

## Fork the project

[Fork the project](https://github.com/awesome-release/nginx-golang-postgres/fork) on GitHub to create a copy of the repository. Your fork of the project will allow you to integrate it with Release using your account, and experiment and make changes without affecting the original project.

## Project structure

Our example project uses `docker-compose` to orchestrate our backend, Nginx, and Postgres.

### The backend

The backend is a simple Go server, using the [Gorilla web toolkit](https://github.com/gorilla) to serve JSON content over HTTP. It connects to a PostgreSQL database, which stores the data that is served at `/` via the backend.

We have a simple route handler, anchored at `/`, which executes a `blogHandler` function.

```jsx
func blogHandler(w http.ResponseWriter, r *http.Request) {
	db, err := connect()
	if err != nil {
		w.WriteHeader(500)
		return
	}
	defer db.Close()

	rows, err := db.Query("SELECT title FROM blog")
	if err != nil {
		w.WriteHeader(500)
		return
	}
	var titles []string
	for rows.Next() {
		var title string
		err = rows.Scan(&title)
		titles = append(titles, title)
	}
	json.NewEncoder(w).Encode(titles)
}

func main() {
	...
	r := mux.NewRouter()
	r.HandleFunc("/", blogHandler)
	log.Fatal(http.ListenAndServe(":8000", handlers.LoggingHandler(os.Stdout, r)))
}
```

The blog handler queries the database, and returns the query result as JSON.

Additionally, on startup, the backend will bootstrap the required tables and data into the database.

### Nginx

Our Nginx server is configured to listen on port `80` and forward any requests to the backend service.

```jsx
server {
    listen       80;
    server_name  localhost;
    location / {
        proxy_pass   http://backend:8000;
    }

}
```

We build a Docker image for our Nginx server with a straightforward Dockerfile.

```jsx
FROM nginx:1.13-alpine
COPY conf /etc/nginx/conf.d/default.conf
```

### Docker Compose

Our backend, Nginx, and PostgreSQL are orchestrated via `docker-compose`.

```jsx
services:
  backend:
    build: backend
    secrets:
      - db-password
    depends_on:
      - db
  db:
    image: postgres
    restart: always
    secrets:
      - db-password
    volumes:
      - db-data:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=example
      - POSTGRES_PASSWORD_FILE=/run/secrets/db-password
    
  proxy:
    build: proxy
    ports:
      - 80:80
    depends_on: 
      - backend
volumes:
  db-data:
secrets:
  db-password:
    file: db/password.txt
```

## Run the project locally

To run the codebase on your development machine, ensure you have Docker installed.

Run the following command via the terminal:

```jsx
docker-compose up
```

Once the application has started, you can run the following command to check if everything is working:

```jsx
$ curl localhost:80
["Blog post #0","Blog post #1","Blog post #2","Blog post #3","Blog post #4"]
```

Lastly, to stop and remove the containers created by `docker-compose` , run:

```jsx
docker-compose down
```

## Deploy to Release

To deploy this application to Release, log in to your Release account and navigate to the dashboard.

From the dashboard, select the **Create New App** button. This will take you to the Create Your Application screen.

![](https://585411240-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M1neGLLQ0sDXeK6ooSo%2Fuploads%2Fgit-blob-1094a46bc812cb7ebc7313184aa32c3b5db2fb54%2F1-create-app-go-nginx-new.png?alt=media\&token=8cbf0e8f-b779-4626-a954-c88fb0d05dcf)

Refresh the repository list, and select the forked version of the [`nginx-golang-postgres`](https://github.com/cishiv/nginx-golang-postgres) repository from the dropdown.

![](https://585411240-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M1neGLLQ0sDXeK6ooSo%2Fuploads%2Fgit-blob-4586f0bd3f2a761ad865c8c5c56153c53eaae38b%2F2-repo-select-go-nginx-new.png?alt=media\&token=3f114e13-afb4-4579-b7fd-82ab4d47f9de)

Using the `docker-compose` configuration, Release will be able to automatically detect the services in the codebase.

![](https://585411240-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M1neGLLQ0sDXeK6ooSo%2Fuploads%2Fgit-blob-2e18e3c636b4e7c29f14499342f2e34007651aee%2F3-app-config-go-nginx.png?alt=media\&token=e8ea928a-e4a3-4d83-bd34-ad37cdbc02a8)

Select a name for your application, and click **Generate App Template**. It's a good idea to keep the name short but descriptive, as it will be used in your hostnames for this application.

![](https://585411240-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M1neGLLQ0sDXeK6ooSo%2Fuploads%2Fgit-blob-1220a3b0b2f3b42a8978d48d582afedb095a2322%2F4-app-name-go-nginx.png?alt=media\&token=a405f0c3-f71d-4791-86e7-88ee42887a9e)

Release will then allow you to modify the Application Template that was generated. For the purposes of this application, nothing needs to be modified here, so click **Save & Continue**.

![](https://585411240-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M1neGLLQ0sDXeK6ooSo%2Fuploads%2Fgit-blob-3687a623493b34832c3182362482429b13529aae%2F5-app-template-go-nginx.png?alt=media\&token=4b0a015d-ddb8-4efe-b120-870edc8f7ae9)

Lastly, before building and deploying the application, you can configure any build arguments, environment variables, or Just-In-Time File Mounts. Once again, we don't need to make any changes here, so click **Start Build & Deploy**.

![](https://585411240-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M1neGLLQ0sDXeK6ooSo%2Fuploads%2Fgit-blob-c9b3098f39dca3f5153e180d04af2ef82525ea9b%2F6-build-conf-go-nginx.png?alt=media\&token=9f449f42-9200-4273-86b0-be20eab2753f)

Release will kick off a build and deployment for your application. This shouldn’t take more than a few minutes for this particular example.

Once our application has been successfully deployed into an environment, we can hover over the `proxy` badge on the environments tab. This badge indicates that the `proxy` service configured in our `docker-compose.yaml` file has a host name URL that we can access.

![](https://585411240-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M1neGLLQ0sDXeK6ooSo%2Fuploads%2Fgit-blob-0afac4f9b3a2c145c6603120cf612d10f5f71f35%2F7-deployed-go-nginx.png?alt=media\&token=7e6c91cc-1a7b-44f6-b2b2-3d80856dcd9d)

Clicking on this badge will take us to our live application, where we should see the response from our backend being routed via Nginx.

![](https://585411240-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M1neGLLQ0sDXeK6ooSo%2Fuploads%2Fgit-blob-8be7a6f0504d78c57219003cf73e324cdfa692ea%2F8-result-go-nginx.png?alt=media\&token=247a5ff6-749d-443c-9d8c-7ede1e8836af)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.release.com/guides-and-examples/common-setup-examples/nginx-postgres-go-docker.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
