How to deploy a java docker app with a database into the cloud

Posted by Java developer blog on May 10, 2020

Overview

There are lots of popular cloud services nowadays. We are going to review one of them. It’s Heroku.

In the post, we are going to discuss how to deploy a java docker app with a postgres database into the Heroku cloud and locally with the help of the docker-compose.

Create a java docker application

We are going to take java-getting-started application and create a docker image

Firstly, we need to compile the application with the help of the following command:

1
mvn clean install

Secondly, we have to create a Dockerfile:

1
2
3
4
5
6
7
8
9
10
11
FROM openjdk:8-jdk-alpine

COPY ./target/java-getting-started-1.0.jar java-getting-started-1.0.jar

# Run the image as a non-root user
RUN adduser -D myuser
USER myuser

EXPOSE 8080

CMD java $JAVA_OPTS -jar java-getting-started-1.0.jar

openjdk:8-jdk-alpine - you use any other base java 8 and higher image as well.

COPY ./target/java-getting-started-1.0.jar java-getting-started-1.0.jar - we copy jar from the target folder into the image.

RUN adduser -D myuser USER myuser - we use it because Heroku images are run as a non-root user.

EXPOSE 8080 - we use it for local deploy. However, Heroku doesn’t recommend exposing any port in docker image.

CMD java $JAVA_OPTS -jar java-getting-started-1.0.jar - Heroku forces to use CMD.

Build and deploy a java docker application into the cloud

We are going to use a Heroku Postgres as a database.

Firstly, we have to use the following commands to deploy the application.

1
2
3
4
5
6
7
8
# Log in to Container Registry
heroku container:login

# Create a Heroku app
heroku create

# Build the image and push to Container Registry
heroku container:push web

Secondly, we have to get the credentials for the database with the help of the following command:

1
heroku pg:credentials:url

You also could get the information about the database in the data dashboard.

Thirdly, we need to pass database settings as an environment variables with the help of Heroku Config Vars.

Simply add database settings into the Config Var section in the application dashboard.

Look at the example below:

Fourthly, we are going to deploy the docker image into the cloud with the help of the following command:

1
heroku container:release web

Lastly, we are going to check the application with the help of the following command:

1
heroku open

Build and deploy locally with the help of docker-compose

I recommend using docker-compose for local deploy.

Simply, put in the root folder docker-compose.yml file with the following content:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
version: '3'
services:
  db:
    image: postgres:9.6
    environment:
      POSTGRES_PASSWORD: postgres
    ports:
      - 5432:5432
  app:
    build: .
    ports:
      - 8080:8080
    environment:
      - JDBC_DATABASE_URL=jdbc:postgresql://db:5432/postgres
      - DATABASE_USERNAME=postgres
      - DATABASE_PASSWORD=postgres
      - PORT=8080

In the docker-compose.yml file you could add a local database as db and define all required environment variables for the app.

To run the app simply use the following command:

1
docker-compose up -d

Conclusion

We have discussed how to deploy a java docker app with a postgres database into the Heroku cloud and locally with the help of the docker-compose. You could check out the source code here.