Containers

Docker

Docker is a container engine: it allows you to pack and run applications, in a sandboxed layeredlightweight environment, with its own isolated filesystem, operating system, and resources.

You usually have to create a Dockerfile for monolithic services, and a docker-compose.yml when your container needs to interact with other services, like for example a database or a redis.

First you have to create a fat-jar file with your application. And a Dockerfile, which looks like this:

Dockerfile

  1. FROM openjdk:8-jre-alpine
  2. ENV APPLICATION_USER ktor
  3. RUN adduser -D -g '' $APPLICATION_USER
  4. RUN mkdir /app
  5. RUN chown -R $APPLICATION_USER /app
  6. USER $APPLICATION_USER
  7. COPY ./build/libs/my-application.jar /app/my-application.jar
  8. WORKDIR /app
  9. CMD ["java", "-server", "-XX:+UnlockExperimentalVMOptions", "-XX:+UseCGroupMemoryLimitForHeap", "-XX:InitialRAMFraction=2", "-XX:MinRAMFraction=2", "-XX:MaxRAMFraction=2", "-XX:+UseG1GC", "-XX:MaxGCPauseMillis=100", "-XX:+UseStringDeduplication", "-jar", "my-application.jar"]

For deploying to Docker simply you can check out the docker quickstart page for full details.

Nginx

When using Docker with multiple domains, you might want to use the nginx-proxy image and the letsencrypt-nginx-proxy-companion imageto serve multiple domains/subdomains in a single machine/ip and to automatically provide HTTPS,using let’s encrypt.

After configuring the nginx-proxy and letsencrypt-nginx-proxy-companion, your docker-compose.yml file(without additional services) might look like this:

docker-compose.yml

  1. version: '2'
  2. services:
  3. web:
  4. build:
  5. context: ./
  6. dockerfile: Dockerfile
  7. expose:
  8. - 8080
  9. environment:
  10. - VIRTUAL_HOST=mydomain.com
  11. - VIRTUAL_PORT=8080
  12. - LETSENCRYPT_HOST=mydomain.com
  13. - LETSENCRYPT_EMAIL=myemail@mydomain.com
  14. networks:
  15. - reverse-proxy
  16. restart: always
  17. networks:
  18. backend:
  19. reverse-proxy:
  20. external:
  21. name: reverse-proxy

You can start it with docker-compose up -d and it will be restarted if the service fails orafter a system reboot.

If the DNS for the specified domain is pointing to your server and you have configured the nginx-proxy and its companion correctly,the letsencrypt companion will contact with letsencrypt and will grab and configure the certificate automaticallyfor you. So you will be able to access your http-only service via: https://mydomain.com/ nginx will handle the SSL certificatesand will contact your server via plain HTTP.

Tomcat

You have to generate a war file and put it in the Tomcat webapps folder.

For a complete example, check:https://github.com/ktorio/ktor-samples/tree/master/deployment/tomcat-war

Jetty

You have to generate a war file and put it in the Jetty webapps folder.

For a complete example, check:https://github.com/ktorio/ktor-samples/tree/master/deployment/jetty-war