CODEWITH_RAJESH
RAJESH KUMAR
STEP-BY-STEP GUIDE: RUNNING A
SPRING BOOT APPLICATION IN
DOCKER WITH DATABASE
CONNECTION AND ENVIRONMENT
VARIABLES
CODEWITH_RAJESH
RAJESH KUMAR
STEP 1:
SET UP YOUR SPRING BOOT
APPLICATION
Ensure that your Spring Boot application is
properly configured to connect to the database.
This may involve configuring the data source,
such as MySQL, PostgreSQL, or H2, in the
application.properties or application.yml file.
CODEWITH_RAJESH
RAJESH KUMAR
STEP 2:
USE ENVIRONMENT
VARIABLES FOR DATABASE
CONFIGURATION
Modify your application.properties or
application.yml file to reference environment
variables for the database configuration:
CODEWITH_RAJESH
RAJESH KUMAR
STEP 3:
CREATE A .env FILE
In the root directory of your Spring Boot project,
create a .env file with the following content
Replace your-database-host, your-database-
name, your-database-username, and your-
database-password with the actual values for
your database.
CODEWITH_RAJESH
RAJESH KUMAR
STEP 4:
CREATE A DOCKER FILE
Create a Dockerfile in the root directory of your
Spring Boot application with the following
content
CODEWITH_RAJESH
RAJESH KUMAR
STEP 5:
CREATE A .dockerignore FILE
Create a .dockerignore file in the same directory
as the Dockerfile to exclude unnecessary files
and directories from being copied into the Docker
image. This helps to reduce the image size and
build time.
CODEWITH_RAJESH
RAJESH KUMAR
STEP 7:
RUN THE DOCKER CONTANIER
After the Docker image is successfully built, you
can run a container using the following command
CODEWITH_RAJESH
RAJESH KUMAR
STEP 8:
ACCESS YOUR APPLICATION
Your Spring Boot application with the database
connection details read from the .env file should
now be running inside a Docker container. You
can access it by opening a web browser and
visiting http://localhost:8080 (or the custom port
you specified in the application if different)
CODEWITH_RAJESH
RAJESH KUMAR
By following these steps, you can run your Spring
Boot application in Docker with environment
variables for database configuration, while
keeping sensitive information secure and not
exposing it in the Dockerfile or application
properties.
Thanks for reading!
Stay up-to-date with the latest advancements in Java full stack
development by following me on the handles below, where I'll be
sharing my expertise and experience in the field.
codewith_rajesh
Rajesh Kumar
Like | Save | Share