|
| 1 | +# Java & PostgreSQL |
| 2 | + |
| 3 | +## Summary |
| 4 | + |
| 5 | +*Develop applications with Java and PostgreSQL. Includes a Java application container and PostgreSQL server.* |
| 6 | + |
| 7 | +| Metadata | Value | |
| 8 | +|----------|-------| |
| 9 | +| *Contributors* | The VS Code Java Team | |
| 10 | +| *Categories* | Core, Languages | |
| 11 | +| *Definition type* | Docker Compose | |
| 12 | +| *Available image variants* | [See Java definition](../java). | |
| 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* | Java | |
| 18 | + |
| 19 | +## Table of Contents |
| 20 | + |
| 21 | +- [Java & PostgreSQL](#java--postgresql) |
| 22 | + - [Summary](#summary) |
| 23 | + - [Using this definition](#using-this-definition) |
| 24 | + - [Adding another service](#adding-another-service) |
| 25 | + - [Debug Configuration](#debug-configuration) |
| 26 | + - [Installing Maven or Gradle](#installing-maven-or-gradle) |
| 27 | + - [Installing Node.js](#installing-nodejs) |
| 28 | + - [Adding the definition to your folder](#adding-the-definition-to-your-folder) |
| 29 | + - [Testing the definition](#testing-the-definition) |
| 30 | + - [Testing the PostgreSQL container](#testing-the-postgresql-container) |
| 31 | + - [License](#license) |
| 32 | + |
| 33 | + |
| 34 | +## Using this definition |
| 35 | + |
| 36 | +This definition creates two containers, one for Java and one for PostgreSQL. VS Code will attach to the Java container, and from within that container the PostgreSQL container will be available on **`localhost`** port 5432. The default database is named `postgres` with a user of `postgres` whose password is `postgres`, and if desired this may be changed in `docker-compose.yml`. Data is stored in a volume named `postgres-data`. |
| 37 | + |
| 38 | +While the definition itself works unmodified, it uses the `mcr.microsoft.com/vscode/devcontainers/java` image which includes `git`, a non-root `vscode` user with `sudo` access, and a set of common dependencies and Java tools for development. You can pick a different version of this image by updating the `VARIANT` arg in `.devcontainer/docker-compose.yml` to pick a Java version. |
| 39 | + |
| 40 | +```yaml |
| 41 | +build: |
| 42 | + context: .. |
| 43 | + dockerfile: .devcontainer/Dockerfile |
| 44 | + args: |
| 45 | + # Update 'VARIANT' to pick an version of Java: 11, 17. |
| 46 | + # Append -bullseye or -buster to pin to an OS version. |
| 47 | + # Use -bullseye variants on local arm64/Apple Silicon. |
| 48 | + VARIANT: 11-bullseye |
| 49 | +``` |
| 50 | +
|
| 51 | +You also can connect to PostgreSQL from an external tool when using VS Code by updating `.devcontainer/devcontainer.json` as follows: |
| 52 | + |
| 53 | +```json |
| 54 | +"forwardPorts": [ "5432" ] |
| 55 | +``` |
| 56 | + |
| 57 | +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: |
| 58 | + |
| 59 | +1. [An Installation Guide for PSQL](https://www.postgresql.org/docs/14/installation.html) a CLI tool to work with a PostgreSQL database. |
| 60 | +2. [Tips on populating data](https://www.postgresql.org/docs/14/populate.html) in the database. |
| 61 | + |
| 62 | +If needed, you can use `postCreateCommand` to run commands after the container is created, by updating `.devcontainer/devcontainer.json` similar to what follows: |
| 63 | + |
| 64 | +```json |
| 65 | +"postCreateCommand": "java -version && git --version && node --version" |
| 66 | +``` |
| 67 | + |
| 68 | +### Adding another service |
| 69 | + |
| 70 | +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: |
| 71 | + |
| 72 | +```yaml |
| 73 | +# Runs the service on the same network as the database container, allows "forwardPorts" in devcontainer.json function. |
| 74 | +network_mode: service:db |
| 75 | +``` |
| 76 | + |
| 77 | +### Debug Configuration |
| 78 | + |
| 79 | +Note that only the integrated terminal is supported by the Remote - Containers extension. You may need to modify `launch.json` configurations to include the following value if an external console is used. |
| 80 | + |
| 81 | +```json |
| 82 | +"console": "integratedTerminal" |
| 83 | +``` |
| 84 | + |
| 85 | +### Installing Maven or Gradle |
| 86 | + |
| 87 | +You can opt to install a version of Maven or Gradle by adding `INSTALL_MAVEN: "true"` or `INSTALL_GRADLE: "true"` to build args in `.devcontainer/docker-compose.yml`. Both of these are set by default. For example: |
| 88 | + |
| 89 | +```yaml |
| 90 | +args: |
| 91 | + VARIANT: 11 |
| 92 | + INSTALL_GRADLE: "true" |
| 93 | + INSTALL_MAVEN: "true" |
| 94 | +``` |
| 95 | + |
| 96 | +Remove the appropriate arg or set its value to `"false"` to skip installing the specified tool. |
| 97 | + |
| 98 | +You can also specify the version of Gradle or Maven if needed. |
| 99 | + |
| 100 | +```yaml |
| 101 | +args: |
| 102 | + VARIANT: 11 |
| 103 | + INSTALL_GRADLE: "true" |
| 104 | + MAVEN_VERSION: "3.8.3" |
| 105 | + INSTALL_MAVEN: "true" |
| 106 | + GRADLE_VERSION: "7.2" |
| 107 | +``` |
| 108 | + |
| 109 | +### Installing Node.js |
| 110 | + |
| 111 | +Given JavaScript front-end web client code written for use in conjunction with a Java 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 enable installation and change the version of Node.js installed or disable its installation by updating the `args` property in `.devcontainer/docker-compose.yml`. |
| 112 | + |
| 113 | +```yaml |
| 114 | +args: |
| 115 | + VARIANT: 11 |
| 116 | + NODE_VERSION: "10" # Set to "none" to skip Node.js installation, or "lts/*" for latest |
| 117 | +``` |
| 118 | + |
| 119 | +### Adding the definition to your folder |
| 120 | + |
| 121 | +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. |
| 122 | + |
| 123 | +2. Start VS Code and open your project folder or connect to a codespace. |
| 124 | + |
| 125 | +3. Press <kbd>F1</kbd> select and **Add Development Container Configuration Files...** command for **Remote-Containers** or **Codespaces**. |
| 126 | + |
| 127 | + > **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. |
| 128 | + |
| 129 | +4. Select this definition. You may also need to select **Show All Definitions...** for it to appear. |
| 130 | + |
| 131 | +5. Finally, press <kbd>F1</kbd> and run **Remote-Containers: Reopen Folder in Container** or **Codespaces: Rebuild Container** to start using the definition. |
| 132 | + |
| 133 | +## Testing the definition |
| 134 | + |
| 135 | +This definition includes some test code that will help you verify it is working as expected on your system. Follow these steps: |
| 136 | + |
| 137 | +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. |
| 138 | +2. Clone this repository. |
| 139 | +3. Start VS Code, press <kbd>F1</kbd>, and select **Remote-Containers: Open Folder in Container...** |
| 140 | +4. Select the `containers/java-postgres` folder. |
| 141 | +5. After the folder has opened in the container, press <kbd>F5</kbd> to start the project. |
| 142 | +6. You should see "Hello Remote World!" in the a Debug Console after the program executes. |
| 143 | +7. From here, you can add breakpoints or edit the contents of the `test-project` folder to do further testing. |
| 144 | + |
| 145 | +## Testing the PostgreSQL container |
| 146 | + |
| 147 | +The `docker-compose` file sets up a networked PostgreSQL database that is accessible from the Java Dev Container. The port is forwarded to `localhost:5432` by default, but can be changed in the [devcontainer.json](.devcontainer\devcontainer.json). |
| 148 | + |
| 149 | +1. After starting the Dev Container as above, you can run the individual tests, which will output in the debug console. |
| 150 | +2. The [AppTest.java](test-project\src\test\java\com\mycompany\app\AppTest.java) contains a Test Method, `testIP` which will ping the Postgres Database using it's default container name, `postgresdb`. |
| 151 | +3. Running this test will let you know that the PostgreSQL DB is accessible. **This does not make or authorize a connection to the database, only checks for connectivity between containers.** You can run the tests either by: |
| 152 | + a. Hovering over individual tests and pressing the Green Play Button. This will compile the class and run a single test. |
| 153 | + b. Finding `AppTest.java`, right-clicking and hitting "Run Java". This will compile the class and run all tests. |
| 154 | +5. Alternatively, running [./test.sh](test-project/test-utils.sh) will also run all the connectivity tests and verify that the PostgresDB is actually accessible. |
| 155 | + |
| 156 | +## License |
| 157 | + |
| 158 | +Copyright (c) Microsoft Corporation. All rights reserved. |
| 159 | + |
| 160 | +Licensed under the MIT License. See [LICENSE](https://github.com/microsoft/vscode-dev-containers/blob/main/LICENSE). |
0 commit comments