Skip to content

theEngMansour/vps-docker-config-for-spring-boot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 

Repository files navigation

Docker and Spring Boot Setup Guide

  by: @theengmansour

1. Create a Docker Network

Create a new Docker network to allow your containers to communicate:

docker network create {network-name}

Example:

docker network create test-net

2. Run MySQL Container

Run a MySQL container attached to the created network:

docker run --name mysql --network test-net -e MYSQL_ROOT_PASSWORD=12345678 -p 3306:3306 -d mysql:{tag}
  • Replace {tag} with the desired MySQL version tag.
  • Example:
docker run --name mysql --network test-net -e MYSQL_ROOT_PASSWORD=12345678 -p 3306:3306 -d mysql:8.0.43-oraclelinux9

3. Create a New Database in MySQL

Access the MySQL container and create a new database:

docker exec -it mysql mysql -u root -p

Enter the password (12345678 in this example), then run:

CREATE DATABASE {database_name};
SHOW DATABASES;
EXIT;

4. Connect to MySQL Using phpMyAdmin

You can also use phpMyAdmin to manage your MySQL container easily.

Run the MySQL container (if not already running):

docker run --name mysql --network test-net -e MYSQL_ROOT_PASSWORD=12345678 -p 3306:3306 -d mysql:{tag}

Run phpMyAdmin container connected to the same network:

docker run --name phpmyadmin --network test-net -d -e PMA_HOST=mysql -p 8080:80 phpmyadmin
  • Open your browser at http://localhost:8080 to access phpMyAdmin.
  • Use root and your password (12345678) to log in.

5. Build Spring Boot Application Package

In your pom.xml add:

<packaging>jar</packaging>

6. Create Dockerfile for Spring Boot Application

Create a Dockerfile with the following content:

FROM openjdk:24
LABEL authors="theengmansour"
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java", "-jar", "-Dspring.profiles.active=cloud", "app.jar"]

7. Configure Spring Boot application-cloud.yaml

Example configuration:

server:
  port: 2000

spring:
  application:
    name: connections
  datasource:
    url: jdbc:mysql://host.docker.internal:3306/patients_db?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
    username: root
    password: 12345678

  jpa:
    show-sql: true
    properties:
      hibernate:
        format_sql: true
        dialect: org.hibernate.dialect.MySQL8Dialect

8. Build Docker Image for Spring Boot

Run this command in the folder containing your Dockerfile:

docker build -t connection:v1.6 .

9. Run Spring Boot Container

Run the Spring Boot container attached to your Docker network:

docker run --name spring-app --network test-net -p 8080:2000 connection:v1.6

10. Upload Docker Image to GitHub Container Registry (GHCR)

You can push your Docker image to GitHub Container Registry to share and deploy it easily.

Step 1: Login to GitHub Container Registry

docker login ghcr.io

To login with a different account :

docker logout ghcr.io

Step 2: Tag your Docker image

docker tag connection:v1.6 ghcr.io/yourusername/vps-docker-config-spring-boot:v1.6

Step 3: Tag your Docker image

docker push ghcr.io/yourusername/vps-docker-config-spring-boot:v1.6

Step 4: Use your image

docker pull ghcr.io/yourusername/vps-docker-config-spring-boot:v1.6

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published