Create and deploy Java + PostGres Docker App for free from 0 using Okteto-Stacks

Mateusz Popielarz
3 min readDec 12, 2020

Docker

Pss, buddy, want a candy?

PreReq

https://docs.microsoft.com/en-us/windows/wsl/install-win10

https://docs.docker.com/docker-for-windows/install/

Java

We gonna useMaven

Undersrc\ we can place java files

Given pom.xml in solution root, basing on https://stackoverflow.com/a/27768965/5381370we can create Dockerfile, which we will be using as a recipe how to set up application

Dockerfile

./Dockerfile

Building

Now we can check if project is building correctly

docker build . -t java-service:latest

t is for naming image

. we gonna use current working directory as a context, so make sure its same that one which contains dockerfile

After few mins we should have build docker image

Launching

Finally! We can run our image now.

docker run --publish 8080:8080 --detach --name java java-service:latest

--publish 8080:8080 — we’re redirecting container port 8080 to host 8080

--detach— do not use current console

When that one runs successfully we should be able to connect to app

We can stop instance by usingdocker stop java

But still we’re lacking of db :(

PostGres

Postgres does have existing image, so we can just use it

But to make things a little more complicated, and to use custom init script which would set up db we do a little magic ;)

Dockerfile

./Dockerfile

Building

docker build . -f Dockerfile-northwind -t pg-service:latest

-f Dockerfile-northwind switch is used when we have multiple dockerfiles in dir and we want to use the one not named ‘Dockerfile’

Running

docker run --publish 5432:5432 --detach --name pg pg-service:latest

Communication between components — docker-compose

Docker by its own does not have any build-in way to setup constelation of containers — we gonna use docker-compose to deal with that.

docker-compose.yaml

We need to setup networks to enable communication between containers

in each service

and we’re reusing previously created dockerfile

after invoking (powershell)

Our app should be up and running http://localhost:8080

swagger ui with data — db connection is ok

Finally — deploy app using Okteto-stacks

To get everything in cloud we can use free Okteto-stacks service

https://okteto.com/

First, we need an account — i tried github login, went smooth

After mail confirmation we need to add permissions to repo

And we can continue

In git root we need to add fileokteto-stack.ymland we need to replicate

docker-compse with okteto syntax

name: myapp
services:
northwind:
environment:
- POSTGRES_HOST_AUTH_METHOD=trust
image: okteto.dev/northwind
build:
context: .
dockerfile: Dockerfile-northwind
ports:
- 5432
northwind-java:
public: true
image: okteto.dev/northwind-java
build: .
ports:
- 8080

Click redeploy

And our app is up, running, hosted in cloud, and free ;)

--

--

Mateusz Popielarz

Initially .net developer that is exploring different lands