awesome-compose/spring-postgres
Atomist Bot 195e8994b2 Pin Docker image maven:3.8.5-eclipse-temurin-17
--platform=$BUILDPLATFORM
->
maven:3.8.5-eclipse-temurin-17@sha256:a554849fd74b733acb76379bd7df0ea26635a48ab9b54ede1e94bbeaaf448cf6

 [atomist:generated]
 [atomist-skill:atomist/docker-base-image-policy]

Signed-off-by: Atomist Bot <bot@atomist.com>
2022-10-07 01:36:45 +00:00
..
.docker add configuration to use spring-postgres sample with Docker Dev Environments feature (#258) 2022-07-09 00:51:47 +02:00
backend Pin Docker image maven:3.8.5-eclipse-temurin-17 2022-10-07 01:36:45 +00:00
db Move all samples to the root dir 2020-03-16 17:23:59 +01:00
compose.yaml Adopt Compose v2 (#240) 2022-05-10 11:59:25 +02:00
README.md Add "Open in Docker Dev Environments" links (#280) 2022-09-01 11:39:33 -05:00

Compose sample application

Use with Docker Development Environments

You can open this sample in the Dev Environments feature of Docker Desktop version 4.12 or later.

Open in Docker Dev Environments Open in Docker Dev Environments

Java application with Spring framework and a Postgres database

Project structure:

.
├── backend
│   ├── Dockerfile
│   └── ...
├── db
│   └── password.txt
├── compose.yaml
└── README.md

compose.yaml

services:
  backend:
    build: backend
    ports:
    - 8080:8080
  db:
    image: postgres
    ...

The compose file defines an application with two services backend and db. When deploying the application, docker compose maps port 8080 of the backend service container to port 8080 of the host as specified in the file. Make sure port 8080 on the host is not already being in use.

Deploy with docker compose

$ docker compose up -d
Creating network "spring-postgres_default" with the default driver
Building backend
Step 1/11 : FROM maven:3.5-jdk-9 AS build
3.5-jdk-9: Pulling from library/maven
...
Successfully tagged spring-postgres_backend:latest
WARNING: Image for service backend was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating spring-postgres_backend_1 ... done
Creating spring-postgres_db_1      ... done

Expected result

Listing containers must show two containers running and the port mapping as below:

$ docker ps
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS              PORTS                  NAMES
56236f640eaa        postgres                  "docker-entrypoint.s…"   29 seconds ago      Up 28 seconds       5432/tcp               spring-postgres_db_1
6e69472dc2c0        spring-postgres_backend   "java -Djava.securit…"   29 seconds ago      Up 28 seconds       0.0.0.0:8080->8080/tcp   spring-postgres_backend_1

After the application starts, navigate to http://localhost:8080 in your web browse or run:

$ curl localhost:8080
<!DOCTYPE HTML>
<html>
<head>
  <title>Getting Started: Serving Web Content</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
	<p>Hello from Docker!</p>
</body>

Stop and remove the containers

$ docker compose down
Stopping spring-postgres_db_1      ... done
Stopping spring-postgres_backend_1 ... done
Removing spring-postgres_db_1      ... done
Removing spring-postgres_backend_1 ... done
Removing network spring-postgres_default