Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

Deploying a Spring Boot App with MySQL on OpenShift

March 27, 2018
Chandra Shekhar Pandey
Related topics:
KubernetesJavaSpring Boot
Related products:
Red Hat OpenShift Container Platform

    This article shows how to take an existing Spring Boot standalone project that uses MySQL and deploy it on Red Hat OpenShift,  In the process, we'll create docker images which can be deployed to most container/cloud platforms. I'll discuss creating a Dockerfile, pushing the container image to an OpenShift registry, and finally creating running pods with the Spring Boot app deployed.

    To develop and test using OpenShift on my local machine, I used Red Hat Container Development Kit (CDK), which provides a single-node OpenShift cluster running in a Red Hat Enterprise Linux VM, based on minishift. You can run CDK on top of Windows, macOS, or Red Hat Enterprise Linux.   For testing, I used Red Hat Enterprise Linux Workstation release 7.3. It should work on macOS too.

    To create the Spring Boot app I used this article as a guide. I'm using an existing openshift/mysql-56-centos7 docker image to deploy MySQL to  OpenShift.

    You can download the code used in this article from my personal github repo.   In this article, I'll be building container images locally, so you'll need to be able to build the project locally with Maven.  This example exposes a rest service using: com.sample.app.MainController.java.

    In the repository, you'll find a Dockerfile in src/main/docker-files/. The dockerfile_springboot_mysql file creates a docker image, having the Spring Boot application based on java8 docker image as a base . While this is ok for testing, for production deployment you'd want to use images that are based on Red Hat Enterprise Linux.

    Building the application:

    1. Use mvn clean install to build the project.

    2. Copy the generated jar in the target folder to src/main/docker-files. When creating the docker image, the application jar can be found at the same location.

    3. Set the database username, password, and URL in src/main/resources/application.properties.  Note: For OpenShift,  it is recommended to pass these parameters into the container as environment variables.

    Now start the CDK VM to get your local OpenShift cluster running.

    1. Start the CDK VM using minishift start:

    $ minishift start

    2.  Set your local environment for docker and the oc CLI:

    $ eval $(minishift oc-env) 
    $ eval $(minishift docker-env)

    Note: the above eval commands will not work on Windows.  See the CDK documentation for more information.

    3. Login to OpenShift and Docker using the developer account:

    $ oc login
    $ docker login -u developer -p $(oc whoami -t) $(minishift openshift registry)

     

    Now we'll build the container images.

    1. Change the directory location to src/main/docker-files within the project.  Then, execute the following commands to build the container images.   Note:  The period (.) is required at the end of the docker build command to indicate the current directory:

    $ docker build -t springboot_mysql -f ./dockerfile_springboot_mysql .

    Use the following command to view the container images that were created:

    $ docker images

    2.  Add the tag springboot_mysql to the image, and push it to the OpenShift registry:

    $ docker tag springboot_mysql $(minishift openshift registry)/myproject/springboot_mysql
    $ docker push $(minishift openshift registry)/myproject/springboot_mysql

    3. Next, pull the OpenShift MySQL image, and create it as an OpenShift application which will initialize and run it. Refer to the documentation for more information:

    docker pull openshift/mysql-56-centos7
    oc new-app -e MYSQL_USER=root -e MYSQL_PASSWORD=root -e MYSQL_DATABASE=test openshift/mysql-56-centos7

    4. Wait for the pod running MySQL to be ready. You can check the status with oc get pods:

    $ oc get pods
    NAME READY STATUS RESTARTS AGE 
    mysql-56-centos7-1-nvth9 1/1 Running 0 3m

    5. Next, ssh to the mysql pod and create a MySQL root user with full privileges:

    $ oc rsh mysql-56-centos7-1-nvth9
    sh-4.2$ mysql -u root
    CREATE USER 'root'@'%' IDENTIFIED BY 'root';
    Query OK, 0 rows affected (0.00 sec)
    
    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
    Query OK, 0 rows affected (0.00 sec)
    
    FLUSH PRIVILEGES;
    Query OK, 0 rows affected (0.00 sec)
    
    exit

    6. Finally, initialize the Spring Boot app using imagestream springboot_mysql:

    $ oc get svc
    NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    mysql-56-centos7 172.30.145.88 none 3306/TCP 8m
    
    $ oc new-app -e spring_datasource_url=jdbc:mysql://172.30.145.88:3306/test springboot_mysql
    $ oc get pods
    NAME READY STATUS RESTARTS AGE
    mysql-56-centos7-1-nvth9 1/1 Running 0 12m
    springbootmysql-1-5ngv4 1/1 Running 0 9m

    7. Check the pod logs:

    oc logs -f springbootmysql-1-5ngv4

    8. Next, expose the service as route:

    $ oc get svc
    NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    mysql-56-centos7 172.30.242.225 none 3306/TCP 14m
    springbootmysql 172.30.207.116 none 8080/TCP 1m
    
    $ oc expose svc springbootmysql
    route "springbootmysql" exposed
    
    $ oc get route
    NAME HOST/PORT PATH SERVICES PORT TERMINATION WILDCARD
    springbootmysql springbootmysql-myproject.192.168.42.182.nip.io springbootmysql 8080-tcp None

    9. Test the application using curl. You should see a list of all entries in the database table:

    $ curl -v http://springbootmysql-myproject.192.168.42.182.nip.io/demo/all

    10. Next, use curl to create an entry in the db:

    $ curl http://springbootmysql-myproject.192.168.42.182.nip.io/demo/add?name=SpringBootMysqlTest
    Saved

    11. View the updated list of entries in the database:

    $ curl http://springbootmysql-myproject.192.168.42.182.nip.io/demo/all
    
    [{"name":"UBUNTU 17.10 LTS","lastaudit":1502409600000,"id":1},{"name":"RHEL 7","lastaudit":1500595200000,"id":2},{"name":"Solaris 11","lastaudit":1502582400000,"id":3},{"name":"SpringBootTest","lastaudit":1519603200000,"id":4},{"name":"SpringBootMysqlTest","lastaudit":1519603200000,"id":5}

    That's it!

    I hope this article is helpful to you for migrating an existing spring-boot application to OpenShift. Just a note, in production environments, one should use Red Hat Supportable Images. This document is intended for development purposes only. It should assist you in creating spring-boot applications that run in containers; and in how to set up MySQL connectivity for spring-boot in OpenShift.

    Last updated: January 29, 2019

    Recent Posts

    • Every layer counts: Defense in depth for AI agents with Red Hat AI

    • Fun in the RUN instruction: Why container builds with distroless images can surprise you

    • Trusted software factory: Building trust in the agentic AI era

    • Build a zero trust AI pipeline with OpenShift and RHEL CVMs

    • Red Hat Hardened Images: Top 5 benefits for software developers

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.