|
| 1 | +# Go |
| 2 | + |
| 3 | +## Summary |
| 4 | + |
| 5 | +*Use and develop Go + Postgres applications. Includes appropriate runtime args, Go, common tools, extensions, and dependencies.* |
| 6 | + |
| 7 | +| Metadata | Value | |
| 8 | +|----------|-------| |
| 9 | +| *Contributors* | The VS Code Team | |
| 10 | +| *Categories* | Core, Languages | |
| 11 | +| *Definition type* | Docker Compose | |
| 12 | +| *Available image variants* | [See GO definition](../go) | |
| 13 | +| *Supported architecture(s)* | x86-64, arm64/aarch64 for `bullseye` variants | |
| 14 | +| *Works in Codespaces* | Yes | |
| 15 | +| *Container host OS support* | Linux, macOS, Windows | |
| 16 | +| *Container OS* | Debian | |
| 17 | +| *Languages, platforms* | Go | |
| 18 | + |
| 19 | +## Table of Contents |
| 20 | + |
| 21 | +- [Go](#go) |
| 22 | + - [Summary](#summary) |
| 23 | + - [Using this definition](#using-this-definition) |
| 24 | + - [Installing Node.js](#installing-nodejs) |
| 25 | + - [Adding the definition to a project or codespace](#adding-the-definition-to-a-project-or-codespace) |
| 26 | + - [Using the PostgreSQL Database](#using-the-postgresql-database) |
| 27 | + - [Adding another service](#adding-another-service) |
| 28 | + - [Installing GO Dependencies](#installing-go-dependencies) |
| 29 | + - [Testing the definition](#testing-the-definition) |
| 30 | + - [Debugging Security](#debugging-security) |
| 31 | + - [License](#license) |
| 32 | + |
| 33 | +## Using this definition |
| 34 | +This definition creates two containers, one for Go and one for PostgreSQL. VS Code will attach to the Go container, and from within that container the PostgreSQL container will be available on **`localhost`** port 5432. The `.env` file sets the default credentials for the Postgres Database. The database is named `postgres` with a user of `postgres` whose password is `postgres`, and if desired this may be changed in `.env`. Data is stored in a volume named `postgres-data`. |
| 35 | + |
| 36 | +While the definition itself works unmodified, you can select the version of Go the container uses by updating the `VARIANT` arg in the included `.devcontainer/docker-compose.yml` (and rebuilding if you've already created the container). |
| 37 | + |
| 38 | +```yaml |
| 39 | +build: |
| 40 | + context: . |
| 41 | + dockerfile: Dockerfile |
| 42 | + args: |
| 43 | + # [Choice] Go version 1, 1.16, 1.17 |
| 44 | + # Append -bullseye or -buster to pin to an OS version. |
| 45 | + # Use -bullseye variants on local arm64/Apple Silicon. |
| 46 | + VARIANT: 1.17 |
| 47 | +``` |
| 48 | +
|
| 49 | +### Installing Node.js |
| 50 | +
|
| 51 | +Given JavaScript front-end web client code written for use in conjunction with a Go back-end often requires the use of Node.js-based utilities to build, this container also includes `nvm` so that you can easily install Node.js. You can change the version of Node.js installed or disable its installation by updating the `args` property in `.devcontainer/docker-compose.yml`. |
| 52 | + |
| 53 | +```yaml |
| 54 | +args: |
| 55 | + VARIANT: 1 |
| 56 | + # Options |
| 57 | + NODE_VERSION: "14" # Set to "none" to skip Node.js installation |
| 58 | +``` |
| 59 | + |
| 60 | +### Adding the definition to a project or codespace |
| 61 | + |
| 62 | +1. If this is your first time using a development container, please see getting started information on [setting up](https://aka.ms/vscode-remote/containers/getting-started) Remote-Containers or [creating a codespace](https://aka.ms/ghcs-open-codespace) using GitHub Codespaces. |
| 63 | + |
| 64 | +2. Start VS Code and open your project folder or connect to a codespace. |
| 65 | + |
| 66 | +3. Press <kbd>F1</kbd> select and **Add Development Container Configuration Files...** command for **Remote-Containers** or **Codespaces**. |
| 67 | + |
| 68 | +> **Note:** If needed, you can drag-and-drop the `.devcontainer` folder from this sub-folder in a locally cloned copy of this repository into the VS Code file explorer instead of using the command. |
| 69 | + |
| 70 | +4. Select this definition. You may also need to select **Show All Definitions...** for it to appear. |
| 71 | + |
| 72 | +5. Finally, press <kbd>F1</kbd> and run **Remote-Containers: Reopen Folder in Container** or **Codespaces: Rebuild Container** to start using the definition. |
| 73 | + |
| 74 | +## Using the PostgreSQL Database |
| 75 | +You can connect to PostgreSQL from an external tool when using VS Code by updating `.devcontainer/devcontainer.json` as follows: |
| 76 | + |
| 77 | +```json |
| 78 | +"forwardPorts": [ "5432" ] |
| 79 | +``` |
| 80 | + |
| 81 | +Once the PostgreSQL container has port forwarding enabled, it will be accessible from the Host machine at `localhost:5432`. The [PostgreSQL Documentation](https://www.postgresql.org/docs/14/index.html) has: |
| 82 | + |
| 83 | +1. [An Installation Guide for PSQL](https://www.postgresql.org/docs/14/installation.html), a CLI tool to work with a PostgreSQL database. |
| 84 | +2. [Tips on populating data](https://www.postgresql.org/docs/14/populate.html) in the database. |
| 85 | + |
| 86 | +If needed, you can use `postCreateCommand` to run commands after the container is created, by updating `.devcontainer/devcontainer.json` similar to what follows: |
| 87 | + |
| 88 | +```json |
| 89 | +"postCreateCommand": "go version && git --version && node --version" |
| 90 | +``` |
| 91 | + |
| 92 | +### Adding another service |
| 93 | + |
| 94 | +You can add other services to your `docker-compose.yml` file [as described in Docker's documentation](https://docs.docker.com/compose/compose-file/#service-configuration-reference). However, if you want anything running in this service to be available in the container on localhost, or want to forward the service locally, be sure to add this line to the service config: |
| 95 | + |
| 96 | +```yaml |
| 97 | +# Runs the service on the same network as the database container, allows "forwardPorts" in devcontainer.json function. |
| 98 | +network_mode: service:[$SERVICENAME] |
| 99 | +``` |
| 100 | + |
| 101 | +### Installing GO Dependencies |
| 102 | + |
| 103 | +This definition includes the popular [PostGres Driver Library for Go](github.com/lib/pq). This is the recommended driver for use with Go, as per [GoLang Documentation](https://golangdocs.com/golang-postgresql-example). |
| 104 | + |
| 105 | +If you wish to change this, you may add additional `RUN` commands in the [Go Dockerfile](.devcontainer/Dockerfile). For example: |
| 106 | + |
| 107 | +```yaml |
| 108 | +# This line can be modified to add any needed additional packages |
| 109 | +RUN go get -x <github-link-for-package> |
| 110 | +``` |
| 111 | + |
| 112 | +## Testing the definition |
| 113 | + |
| 114 | +This definition includes some test code that will help you verify it is working as expected on your system. Follow these steps: |
| 115 | + |
| 116 | +1. If this is your first time using a development container, please follow the [getting started steps](https://aka.ms/vscode-remote/containers/getting-started) to set up your machine. |
| 117 | +2. Clone this repository. |
| 118 | +3. Start VS Code, press <kbd>F1</kbd>, and select **Remote-Containers: Open Folder in Container...** |
| 119 | +4. Select the `containers/go-postgres` folder. |
| 120 | +5. After the folder has opened in the container, press <kbd>F5</kbd> to start the project. |
| 121 | +6. From here the following should print out in the Debug Console: |
| 122 | + ``` |
| 123 | + Connected! |
| 124 | + Sending Query to Database |
| 125 | + One database in this cluster is: postgres |
| 126 | + ``` |
| 127 | +7. From here, you can add breakpoints or edit the contents of the `test-project` folder to do further testing. |
| 128 | +8. The Application can also be tested by running `./test.sh` from the Terminal. This will test the `hello.go` application. |
| 129 | + |
| 130 | +### Debugging Security |
| 131 | +To allow C++ based debuggers to run within the Docker Containers, the [docker-compose.yml](.devcontainer/docker-compose.yml) contains the following lines which can be uncommented: |
| 132 | + |
| 133 | +```yaml |
| 134 | + security_opt: |
| 135 | + - seccomp:unconfined |
| 136 | + cap_add: |
| 137 | + - SYS_PTRACE |
| 138 | +``` |
| 139 | + |
| 140 | +As these can create security vulnerabilities, it is advisable to not use this unless needed. This should only be used in a Debug or Dev container, not in Production. |
| 141 | + |
| 142 | +## License |
| 143 | + |
| 144 | +Copyright (c) Microsoft Corporation. All rights reserved. |
| 145 | + |
| 146 | +Licensed under the MIT License. See [LICENSE](https://github.com/microsoft/vscode-dev-containers/blob/main/LICENSE). |
0 commit comments