Install the Ktor plugin
To install the Ktor plugin, add it to the
plugins block of your
After installing the plugin, the following tasks are available for packaging, running, and deploying applications:
buildImage: builds a project's Docker image to a tarball. This task generates a
jib-image.tarfile in the
builddirectory. You can load this image to a Docker daemon using the docker load command:docker load < build/jib-image.tar
publishImageToLocalRegistry: builds and publishes a project's Docker image to a local registry.
runDocker: builds a project's image to a Docker daemon and runs it. Executing this task will launch the Ktor server, responding on
http://0.0.0.0:8080by default. If your server is configured to use another port, you can adjust port mapping.
publishImage: builds and publishes a project's Docker image to an external registry such as Docker Hub or Google Container Registry. Note that you need to configure the external registry using the ktor.docker.externalRegistry property for this task.
Note that by default, these tasks build the image with the
ktor-docker-image name and
latest tag. You can customize these values in the plugin configuration.
Configure the Ktor plugin
To configure the Ktor plugin settings related to Docker tasks, use the
ktor.docker extension in your
jreVersion property specifies the JRE version to use in the image:
Image name and tag
If you need to customize the image name and tag, use the
imageTag properties, respectively:
By default, the runDocker task publishes the
8080 container port to the
8080 Docker host port. If required, you can change these ports using the
portMappings property. This might be useful if your server is configured to use another port.
The example below shows how to map the
8080 container port to the
80 Docker host port.
In this case, you can access the server on
Before publishing a project's Docker image to an external registry using the publishImage task, you need to configure the external registry using the
ktor.docker.externalRegistry property. This property accepts the
DockerImageRegistry instance, which provides configuration for the required registry type:
DockerImageRegistry.dockerHub: creates a
DockerImageRegistryfor Docker Hub.
DockerImageRegistry.googleContainerRegistry: creates a
DockerImageRegistryfor Google Container Registry.
The example below shows how to configure the Docker Hub registry:
Note that the Docker Hub name and password are fetched from the environment variables, so you need to set these values before running the
Manual image configuration
If required, you can provide your own
Dockerfile to assemble an image with a Ktor application.
Package the application
Prepare Docker image
To dockerize the application, we'll use multi-stage builds:
First, we'll use the
mavenimage to generate a fat JAR with the application.
Then, the generated distribution will be run in the environment created based on the
In the root folder of the project, create a file named
Dockerfile with the following contents:
The second stage of the build works in the following way:
Indicates what image is going to be used (
openjdkin this case).
Specifies the exposed port (this does not automatically expose the port, which is done when running the container).
Copies the contents from the build output to the folder.
Runs the application (
You can find the resulting project in the docker branch of the ktor-get-started-sample repository.
Build and run the Docker image
The next step is to build and tag the Docker image:
Finally, start the image:
This will launch the Ktor server, responding on