by: @theengmansourCreate a new Docker network to allow your containers to communicate:
docker network create {network-name}Example:
docker network create test-netRun 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-oraclelinux9Access the MySQL container and create a new database:
docker exec -it mysql mysql -u root -pEnter the password (12345678 in this example), then run:
CREATE DATABASE {database_name};
SHOW DATABASES;
EXIT;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:8080to access phpMyAdmin. - Use
rootand your password (12345678) to log in.
In your pom.xml add:
<packaging>jar</packaging>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"]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.MySQL8DialectRun this command in the folder containing your Dockerfile:
docker build -t connection:v1.6 .Run the Spring Boot container attached to your Docker network:
docker run --name spring-app --network test-net -p 8080:2000 connection:v1.6You can push your Docker image to GitHub Container Registry to share and deploy it easily.
docker login ghcr.ioTo login with a different account :
docker logout ghcr.iodocker tag connection:v1.6 ghcr.io/yourusername/vps-docker-config-spring-boot:v1.6docker push ghcr.io/yourusername/vps-docker-config-spring-boot:v1.6docker pull ghcr.io/yourusername/vps-docker-config-spring-boot:v1.6