Skip to content
This repository was archived by the owner on Nov 30, 2023. It is now read-only.

Commit e8d4408

Browse files
authored
Adding PostGreSQL + Java DB and Language Container pair (#1206)
1 parent 6a1aee0 commit e8d4408

File tree

18 files changed

+771
-0
lines changed

18 files changed

+771
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# [Choice] Java version (use -bullseye variants on local arm64/Apple Silicon): 11, 17, 11-bullseye, 17-bullseye, 11-buster, 17-buster
2+
ARG VARIANT=11-bullseye
3+
FROM mcr.microsoft.com/vscode/devcontainers/java:0-${VARIANT}
4+
5+
# [Option] Install Maven
6+
ARG INSTALL_MAVEN="false"
7+
ARG MAVEN_VERSION=""
8+
# [Option] Install Gradle
9+
ARG INSTALL_GRADLE="false"
10+
ARG GRADLE_VERSION=""
11+
RUN if [ "${INSTALL_MAVEN}" = "true" ]; then su vscode -c "umask 0002 && . /usr/local/sdkman/bin/sdkman-init.sh && sdk install maven \"${MAVEN_VERSION}\""; fi \
12+
&& if [ "${INSTALL_GRADLE}" = "true" ]; then su vscode -c "umask 0002 && . /usr/local/sdkman/bin/sdkman-init.sh && sdk install gradle \"${GRADLE_VERSION}\""; fi
13+
14+
# [Choice] Node.js version: none, lts/*, 16, 14, 12, 10
15+
ARG NODE_VERSION="none"
16+
RUN if [ "${NODE_VERSION}" != "none" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi
17+
18+
# [Optional] Uncomment this section to install additional OS packages.
19+
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
20+
# && apt-get -y install --no-install-recommends <your-package-list-here>
21+
22+
# [Optional] Uncomment this line to install global node packages.
23+
# RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g <your-package-here>" 2>&1
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "Java & PostgreSQL",
3+
"dockerComposeFile": "docker-compose.yml",
4+
"service": "app",
5+
"workspaceFolder": "/workspace",
6+
7+
// Set *default* container specific settings.json values on container create.
8+
"settings": {
9+
"sqltools.connections": [
10+
{
11+
"name": "Container database",
12+
"driver": "PostgreSQL",
13+
"previewLimit": 50,
14+
"server": "localhost",
15+
"port": 5432,
16+
// NOTE: database/username/password should match values in docker.compose.yml
17+
"database": "postgres",
18+
"username": "postgres",
19+
"password": "postgres"
20+
}
21+
],
22+
"java.home": "/docker-java-home"
23+
},
24+
25+
// Add the IDs of extensions you want installed when the container is created.
26+
"extensions": [
27+
"vscjava.vscode-java-pack",
28+
"mtxr.sqltools",
29+
"mtxr.sqltools-driver-pg"
30+
],
31+
32+
// Use 'forwardPorts' to make a list of ports inside the container available locally.
33+
// "forwardPorts": [5432],
34+
35+
// Use 'postCreateCommand' to run commands after the container is created.
36+
// "postCreateCommand": "java -version",
37+
38+
// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
39+
"remoteUser": "vscode"
40+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
version: '3.8'
2+
3+
volumes:
4+
postgres-data:
5+
6+
services:
7+
app:
8+
container_name: javadev
9+
build:
10+
context: .
11+
dockerfile: Dockerfile
12+
args:
13+
# Update 'VARIANT' to pick an version of Java: 11, 17.
14+
# Append -bullseye or -buster to pin to an OS version.
15+
# Use -bullseye variants on local arm64/Apple Silicon.
16+
VARIANT: 11-bullseye
17+
# Options
18+
INSTALL_MAVEN: "false"
19+
MAVEN_VERSION: ""
20+
INSTALL_GRADLE: "false"
21+
GRADLE_VERSION: ""
22+
NODE_VERSION: "lts/*"
23+
environment:
24+
# NOTE: POSTGRES_DB/USER/PASSWORD should match values in devcontainer.json and in db container
25+
POSTGRES_PASSWORD: postgres
26+
POSTGRES_USER: postgres
27+
POSTGRES_DB: postgres
28+
POSTGRES_HOSTNAME: postgresdb
29+
30+
volumes:
31+
- ..:/workspace:cached
32+
33+
# Overrides default command so things don't shut down after the process ends.
34+
command: sleep infinity
35+
36+
# Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function.
37+
network_mode: service:db
38+
39+
# Uncomment the next line to use a non-root user for all processes.
40+
# user: vscode
41+
42+
# Use "forwardPorts" in **devcontainer.json** to forward an app port locally.
43+
# (Adding the "ports" property to this file will not forward from a Codespace.)
44+
45+
db:
46+
container_name: postgresdb
47+
image: postgres:latest
48+
restart: unless-stopped
49+
volumes:
50+
- postgres-data:/var/lib/postgresql/data
51+
environment:
52+
# NOTE: POSTGRES_DB/USER/PASSWORD should match values in devcontainer.json and in app container
53+
POSTGRES_PASSWORD: postgres
54+
POSTGRES_USER: postgres
55+
POSTGRES_DB: postgres
56+
57+
# Add "forwardPorts": ["5432"] to **devcontainer.json** to forward PostgreSQL locally.
58+
# (Adding the "ports" property to this file will not forward from a Codespace.)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
README.md
2+
test-project
3+
definition-manifest.json
4+
.vscode
5+
.npmignore
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"configurations": [
3+
{
4+
"type": "java",
5+
"name": "Launch App",
6+
"request": "launch",
7+
"cwd": "${workspaceFolder}/test-project",
8+
"console": "internalConsole",
9+
"stopOnEntry": false,
10+
"mainClass": "mymodule/com.mycompany.app.App",
11+
"args": "",
12+
"projectName": "my-app"
13+
}
14+
]
15+
}

containers/java-postgres/README.md

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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).
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
10+
<attributes>
11+
<attribute name="optional" value="true"/>
12+
<attribute name="maven.pomderived" value="true"/>
13+
<attribute name="test" value="true"/>
14+
</attributes>
15+
</classpathentry>
16+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
17+
<attributes>
18+
<attribute name="maven.pomderived" value="true"/>
19+
</attributes>
20+
</classpathentry>
21+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
22+
<attributes>
23+
<attribute name="maven.pomderived" value="true"/>
24+
</attributes>
25+
</classpathentry>
26+
<classpathentry kind="src" path="target/generated-sources/annotations">
27+
<attributes>
28+
<attribute name="optional" value="true"/>
29+
<attribute name="maven.pomderived" value="true"/>
30+
<attribute name="ignore_optional_problems" value="true"/>
31+
<attribute name="m2e-apt" value="true"/>
32+
</attributes>
33+
</classpathentry>
34+
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
35+
<attributes>
36+
<attribute name="optional" value="true"/>
37+
<attribute name="maven.pomderived" value="true"/>
38+
<attribute name="ignore_optional_problems" value="true"/>
39+
<attribute name="m2e-apt" value="true"/>
40+
<attribute name="test" value="true"/>
41+
</attributes>
42+
</classpathentry>
43+
<classpathentry kind="output" path="target/classes"/>
44+
</classpath>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
target
2+
3+
# Compiled class file
4+
*.class
5+
6+
# Log file
7+
*.log
8+
9+
# BlueJ files
10+
*.ctxt
11+
12+
# Mobile Tools for Java (J2ME)
13+
.mtj.tmp/
14+
15+
# Package Files #
16+
*.jar
17+
*.war
18+
*.nar
19+
*.ear
20+
*.zip
21+
*.tar.gz
22+
*.rar
23+
24+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
25+
hs_err_pid*
26+
replay_pid*

0 commit comments

Comments
 (0)