How to run Java Spring Boot Microservice Application using Docker Compose Container with Postgres Database? How to deploy on Ubuntu?
Explained “How to run Java Spring Boot Microservice Application using Docker Compose Container with Postgres Database. How to deploy on Ubuntu”.
Implementing Docker involves several steps, from installation to running containers. Here’s a step-by-step guide to help you get started
Step 1:-
1. Install Docker from your server :
Ensure your system meets the prerequisites for Docker installation (OS compatibility, hardware requirements).
Download and install( Docker Desktop )for your operating system from the (official Docker website)
2. Open a terminal or command prompt.
sudo apt update
3. Verify Installation:
Run docker — version to check the installed Docker version.
4. Postgres installation Pull PostgreSQL Image (Official Document)
docker pull postgres:latest
5. Create a Docker Volume:
docker volume create postgres_data(<-name)
6. Run PostgreSQL Container:
docker run -d — name my_postgres_db -e POSTGRES_USER=myuser -e POSTGRES_PASSWORD=mypassword -e POSTGRES_DB=mydatabase -p 5432:5432 (pg image name) -v postgres_data:/var/lib/postgresql/data postgres
Eg: docker run — name postgres-con -e POSTGRES_PASSWORD=1234-p 5434:5432 -d postgres -v postgres_data:/var/lib/postgresql/data postgres
7. Access PostgreSQL Container:
docker exec -it postgres_con bash
Psql –U postgres
8. Create Database inside PostgreSQL Container:
CREATE DATABASE <mynewdatabase>;
9. Check the available Databases
\l
10. After exit from Postgres user.
/exit
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
Create Docker File:
· Go to your project folders'
· Build your own images using a Dockerfile.
- Create Dockerfile in your Java project directory And paste below lines.
Note:-
Should be present Docker file in every microservice project inside folder.👇
Copy and Paste here your docker file.👇
FROM openjdk:17-oracle
EXPOSE 8002
COPY ./target/docz-app-services-0.0.1-SNAPSHOT.jar /app/
ENTRYPOINT [“java”,”-jar”,”docz-app-services-0.0.1-SNAPSHOT.jar”]
2. Create docker-compose.yml in your outside project directory And past below lines.
Copy and Paste here your docker-compose.yaml file.👇
version: “3.7”
services:
app1:
container_name: admin-con
image: yourapp/admin_app-img
environment:
POSTGRES_URL: postgres://postgres_user:postgres_password@postgres_host:5432/postgres_db
ports:
- “8001:8001”
depends_on:
- postgres
restart: on-failure
restart_policy:
max_attempts: 2
window: 120s
app2:
container_name: app-con
image: yourapp/app-img
environment:
POSTGRES_URL: postgres://postgres_user:postgres_password@postgres_host:5432/postgres_db
ports:
- “8002:8002”
depends_on:
- postgres
restart: on-failure
restart_policy:
max_attempts: 2
window: 120s
postgres:
container_name: postgres-con
image: postgres-img
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: 1234
POSTGRES_DB: your_table
ports:
- “5432:5432”
Like below👇.
Open the Command prompt in project directory and run the below commands.
3. Run below command for Containers Compose up.
docker-compose docker-compose.yaml up –d
4. Access the service via URL:- http://host_ip:exposed_port
Accessing your Applications If you have a application running in one of your Compose services, you can access it via the exposed port on your host machine.
— — — — — — — — — — — — — — — — — — — — — — — — — — —
Check additional commands
5. Stop or down cmd :
docker-compose docker-compose.yaml down
6. Restart compose:-
docker-compose restart
7. Removed particular image using id:
docker image rm 3cab9684f2b5
8. Remove image without postgres this cmd used.
docker image prune -a — force
docker-compose build — no-cache
9. List the services defined in the docker-compose.yml.
docker-compose ps
This way, you can easily manage your Docker Compose services and access your web applications running within those services.
— — — — — — — — — — — — — — — — — — — — — — — — — —
Author by
Sasiclinton. R (Profile)