Skip to content

Commit 828218c

Browse files
CopilotKaniska244
andcommitted
fix: replace network_mode with bridge network in java-postgres docker-compose and update test.sh
Co-authored-by: Kaniska244 <[email protected]>
1 parent 8af970f commit 828218c

3 files changed

Lines changed: 37 additions & 22 deletions

File tree

src/java-postgres/.devcontainer/docker-compose.yml

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
version: '3.8'
2-
31
volumes:
42
postgres-data:
53

@@ -14,16 +12,21 @@ services:
1412
POSTGRES_PASSWORD: postgres
1513
POSTGRES_USER: postgres
1614
POSTGRES_DB: postgres
17-
POSTGRES_HOSTNAME: postgresdb
15+
POSTGRES_HOSTNAME: db
1816

1917
volumes:
2018
- ../..:/workspaces:cached
2119

2220
# Overrides default command so things don't shut down after the process ends.
2321
command: sleep infinity
2422

25-
# Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function.
26-
network_mode: service:db
23+
# Use proper Docker networking instead of network_mode: service:db
24+
# to ensure reliable DNS resolution in all environments
25+
depends_on:
26+
db:
27+
condition: service_healthy
28+
networks:
29+
- app-network
2730

2831
# Use "forwardPorts" in **devcontainer.json** to forward an app port locally.
2932
# (Adding the "ports" property to this file will not forward from a Codespace.)
@@ -32,6 +35,14 @@ services:
3235
container_name: postgresdb
3336
image: postgres:latest
3437
restart: unless-stopped
38+
networks:
39+
- app-network
40+
healthcheck:
41+
test: ["CMD-SHELL", "pg_isready -U postgres"]
42+
interval: 10s
43+
timeout: 5s
44+
retries: 5
45+
start_period: 30s
3546
volumes:
3647
- postgres-data:/var/lib/postgresql/data
3748
environment:
@@ -41,4 +52,8 @@ services:
4152
POSTGRES_DB: postgres
4253

4354
# Add "forwardPorts": ["5432"] to **devcontainer.json** to forward PostgreSQL locally.
44-
# (Adding the "ports" property to this file will not forward from a Codespace.)
55+
# (Adding the "ports" property to this file will not forward from a Codespace.)
56+
57+
networks:
58+
app-network:
59+
driver: bridge

src/java-postgres/NOTES.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ This template references an image that was [pre-built](https://containers.dev/im
55

66
## Using this template
77

8-
This template creates two containers, one for Java and one for PostgreSQL. You will be connected 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 `.devcontainer/docker-compose.yml`. Data is stored in a volume named `postgres-data`.
8+
This template creates two containers, one for Java and one for PostgreSQL. You will be connected to the Java container, and from within that container the PostgreSQL container will be available on the hostname **`db`** 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 `.devcontainer/docker-compose.yml`. Data is stored in a volume named `postgres-data`.
99

1010
While the template itself works unmodified, it uses the `mcr.microsoft.com/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.
1111

@@ -28,12 +28,7 @@ If needed, you can use `postCreateCommand` to run commands after the container i
2828

2929
### Adding another service
3030

31-
You can add other services to your `.devcontainer/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:
32-
33-
```yaml
34-
# Runs the service on the same network as the database container, allows "forwardPorts" in devcontainer.json function.
35-
network_mode: service:db
36-
```
31+
You can add other services to your `.devcontainer/docker-compose.yml` file [as described in Docker's documentation](https://docs.docker.com/compose/compose-file/#service-configuration-reference). For inter-service communication, attach new services to the `app-network` bridge network configured in the compose file.
3732

3833
### Using the forwardPorts property
3934

test/java-postgres/test.sh

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,27 @@ source test-utils.sh vscode
66
# Run common tests
77
checkCommon
88

9-
# Prep
10-
echo -e "\nGetting Maven wrapper..."
11-
curl -sSL https://github.com/takari/maven-wrapper/archive/maven-wrapper-0.5.5.tar.gz| tar -xzf -
12-
mv maven-wrapper-maven-wrapper-0.5.5/mvnw mvnw
13-
mv maven-wrapper-maven-wrapper-0.5.5/.mvn .mvn
14-
rm -rf mv maven-wrapper-maven-wrapper-0.5.5
9+
# Prep - Download Maven for building (network_mode fix enables outbound access)
10+
echo -e "\nResolving Maven..."
11+
MAVEN_VERSION="3.9.9"
12+
MAVEN_DIR="apache-maven-${MAVEN_VERSION}"
13+
14+
if command -v mvn >/dev/null 2>&1; then
15+
MVN_CMD="mvn"
16+
else
17+
echo "mvn not found, downloading Apache Maven ${MAVEN_VERSION}..."
18+
curl -fsSL "https://archive.apache.org/dist/maven/maven-3/${MAVEN_VERSION}/binaries/${MAVEN_DIR}-bin.tar.gz" | tar -xzf -
19+
MVN_CMD="$(pwd)/${MAVEN_DIR}/bin/mvn"
20+
fi
1521

1622
# template specific tests
1723
checkExtension "vscjava.vscode-java-pack"
1824
check "java" java -version
19-
check "build-and-test-jar" ./mvnw -q package
25+
check "build-and-test-jar" "$MVN_CMD" -q package
2026
check "test-project" java -jar target/my-app-1.0-SNAPSHOT.jar
2127

2228
# Clean up
23-
rm -f mvnw
24-
rm -rf .mvn
29+
rm -rf "${MAVEN_DIR}"
2530

2631
# Report result
2732
reportResults

0 commit comments

Comments
 (0)