Spring Boot and Docker
Docker is a platform for developing, shipping, and running applications inside containers. This guide covers the key concepts and steps for using Docker with Spring Boot, including setting up Docker, creating a Dockerfile, building and running Docker images, and using Docker Compose.
Key Concepts of Spring Boot and Docker
- Docker: A platform for developing, shipping, and running applications inside containers.
- Dockerfile: A text file that contains instructions for building a Docker image.
- Docker Image: A lightweight, stand-alone, executable package that includes everything needed to run a piece of software.
- Docker Container: A runtime instance of a Docker image.
- Docker Compose: A tool for defining and running multi-container Docker applications.
Setting Up Docker
Follow these steps to set up Docker:
- Download and install Docker from the [Docker website](https://www.docker.com/products/docker-desktop).
- Verify the installation by running the following command:
$ docker --version
Creating a Dockerfile
Create a Dockerfile in the root directory of your Spring Boot project:
Example: Dockerfile
# Use an official OpenJDK runtime as a parent image
FROM openjdk:11-jre-slim
# Set the working directory in the container
WORKDIR /app
# Copy the JAR file into the container
COPY target/myapp.jar /app/myapp.jar
# Expose the port the app runs on
EXPOSE 8080
# Run the JAR file
ENTRYPOINT ["java", "-jar", "myapp.jar"]
Building and Running Docker Images
Build and run your Docker image using the following commands:
Building the Docker Image
$ docker build -t myapp:latest .
Running the Docker Container
$ docker run -p 8080:8080 myapp:latest
Using Docker Compose
Docker Compose is a tool for defining and running multi-container Docker applications. Create a docker-compose.yml
file in the root directory of your project:
Example: docker-compose.yml
version: '3.8'
services:
app:
image: myapp:latest
build: .
ports:
- "8080:8080"
environment:
SPRING_PROFILES_ACTIVE: docker
db:
image: postgres:latest
environment:
POSTGRES_DB: mydb
POSTGRES_USER: user
POSTGRES_PASSWORD: password
ports:
- "5432:5432"
Building and Running with Docker Compose
$ docker-compose up --build
Example: Using Environment Variables in Docker
You can pass environment variables to your Spring Boot application using Docker:
Example: application.properties
spring.datasource.url=jdbc:postgresql://db:5432/mydb
spring.datasource.username=${POSTGRES_USER}
spring.datasource.password=${POSTGRES_PASSWORD}
Running the Docker Container with Environment Variables
$ docker run -p 8080:8080 -e POSTGRES_USER=user -e POSTGRES_PASSWORD=password myapp:latest
Key Points
- Docker: A platform for developing, shipping, and running applications inside containers.
- Dockerfile: A text file that contains instructions for building a Docker image.
- Docker Image: A lightweight, stand-alone, executable package that includes everything needed to run a piece of software.
- Docker Container: A runtime instance of a Docker image.
- Docker Compose: A tool for defining and running multi-container Docker applications.
- Set up Docker by downloading and installing it from the Docker website.
- Create a Dockerfile in the root directory of your Spring Boot project to define how to build the Docker image.
- Build and run your Docker image using the
docker build
anddocker run
commands. - Use Docker Compose to define and run multi-container Docker applications.
- Pass environment variables to your Spring Boot application using Docker.
Conclusion
Docker and Spring Boot provide a powerful combination for developing, shipping, and running applications. By understanding and using the capabilities of Docker with Spring Boot, developers can create scalable, portable, and maintainable applications efficiently. Happy coding!