Skip to the content.

Run Java in Docker

In this exercise, we will create a java container that will insert data into a MySql container.

Create a Docker Network

In order for the java container to find the mysql container they will both need to be run in the same docker network.

docker network create mynet

To view the network you created, run the following

docker network ls
NETWORK ID     NAME                         DRIVER    SCOPE
e6b0914eb41c   mynet                        bridge    local

Run the mysql container we built in the prior exercise within the docker network

docker run --rm --name mydb -d --network mynet my-mysql

To confirm that the database has started properly, run the following

docker logs -f mydb

Once you see /usr/sbin/mysqld: ready for connections., press Cntl-C to exit the database logs.

2021-01-03T00:48:06.136441Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.22'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.

Sample Test Data

The following CSV file contains sample data that will be inserted into our database.

Sample data notes

Sample Java Program Bundled into a Container

The following is a very simple program that parses a CSV file and loads data into our database schema.

The following maven pom file defines the mysql dependency.

The following Dockerfile uses maven to build a directory containing the compiled java application (as a jar) along with all jar file dependencies.

This Dockerfile is a multi-stage Dockerfile.

Build the java docker image

docker build -t my-java examples/session1/java

Run the java container to insert data into the database

This container will be run in the same network that was used to run the database container.

By default the program looks for a file named test.csv.

A local file (the test.csv file) will be bound into the file system of the running container.

docker run --rm --name java --network mynet -v "$(pwd)/examples/session1/java/test.csv:/tmp/test.csv" my-java 

Any additional parameters added to the end of the docker command are passed as parameters to the java program.

    public static final void main(String[] argv) {
        String file = argv.length > 0 ? argv[0] : "test.csv";
        System.out.println(String.format("Import the contents of [%s]", file));
        IngestCLI cli = new IngestCLI();
        cli.processInput(file);
    }

Try the following

docker run --rm --name java --network mynet -v "$(pwd)/examples/session1/java/test.csv:/tmp/test.csv" my-java simpsons.csv

Note the following output

Import the contents of [simpsons.csv]
java.io.FileNotFoundException: simpsons.csv (No such file or directory)

If we change the name of the bind mount target, the file will be read as simpsons.csv.

docker run --rm --name java --network mynet -v "$(pwd)/examples/session1/java/test.csv:/tmp/simpsons.csv" my-java simpsons.csv