Ktor Application Configuration with Docker and PostgreSQL
In this article, you will learn how to set up a Ktor application with Docker, PostgreSQL, and PgAdmin using Docker Compose. This will allow you to manage your database and deploy your application efficiently.
π Prerequisites
Before getting started, make sure you have the following installed:
ποΈ Setting Up the Dockerfile
The following Dockerfile defines two stages: a build stage and a run
# Build stage: Using Gradle with JDK 21
FROM gradle:8.6-jdk21 AS build
# Set working directory
WORKDIR /app
# Copy Gradle configuration files
COPY gradle ./gradle
# This is done before copying the source code to leverage Docker's layer caching
COPY build.gradle.kts settings.gradle.kts gradle.properties ./
# Copy the source code
COPY src ./src
# Build the application
# The --no-daemon flag ensures Gradle doesn't start a background process
RUN gradle installDist --no-daemon
# Runtime stage: Using a lightweight JDK 21 image
FROM openjdk:21-slim
# Set working directory
WORKDIR /app
# Copy the built application from the build stage
COPY --from=build /app/build/install/ ./
# Expose the port that Ktor will use (adjust according to your configuration)
EXPOSE 8080
# Define the port environment variable (can be used in your Ktor configuration)
ENV PORT=8080
# Command to run the application
CMD ["app/bin/app"]
π Dockerfile Explanation
- First stage (build): Uses
gradle:8.6-jdk21
to build the application. - Copy files: Copies Gradle configuration files and source code.
- Builds the application: Runs
gradle installDist
to generate distribution files. - Second stage (runtime): Uses
openjdk:21-slim
as a lightweight runtime image. - Copies built files: From
/app/build/install/
. - Exposes port 8080 and defines the
PORT
environment variable. - Runs the application using
CMD
.
π οΈ Configuring Docker Compose
The following docker-compose.yml
file defines three services:
app
: Our Ktor application.postgres-db
: A PostgreSQL database.pgadmin
: A web tool for managing PostgreSQL.
services:
app:
build: .
container_name: ktor_app
depends_on:
- postgres-db
environment:
DATABASE_URL: jdbc:postgresql://postgres-db:5432/ktor_db
DATABASE_USER: ktor_user
DATABASE_PASSWORD: ktor_password
PORT: 8080
ports:
- "8080:8080"
postgres-db:
image: postgres:15
container_name: postgres_db
environment:
POSTGRES_DB: ktor_db
POSTGRES_USER: ktor_user
POSTGRES_PASSWORD: ktor_password
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
pgadmin:
image: dpage/pgadmin4:9.1.0
restart: always
ports:
- "5050:80"
environment:
PGADMIN_DEFAULT_EMAIL: admin@example.com
PGADMIN_DEFAULT_PASSWORD: admin
depends_on:
- postgres-db
volumes:
postgres_data:
π Docker Compose Explanation
app
: Builds the image from the Dockerfile and sets environment variables.postgres-db
: Uses the official PostgreSQL image and configures the database.pgadmin
: Uses the PgAdmin image to manage the database graphically.volumes
: Ensures PostgreSQL data persists between restarts.
π Running the Containers
Run the following command to start the services:
docker compose up -d
Then, you can access:
http://localhost:8080
β Your Ktor application.http://localhost:5050
β PgAdmin (user:admin@example.com
, password:admin
).
To stop the services, use:
docker compose down
π― Conclusion
This setup allows you to develop and test Ktor applications with PostgreSQL and PgAdmin in a Dockerized environment. You can extend it by adding security configurations, database migrations, and additional services.
Happy coding! π
If you like my content and want to support my work, you can give me a cup of coffee βοΈ π₯°
Follow me in
- X: @devjcastro
- Linkedin: devjcastro