1. Developping a Dockerfile
  2. Creating a docker image
  3. Running a container based on the docker image
  4. (Bonus) Publish the docker image

In this Session we will see how the example application developed in the Session 3 can be packaged in a docker image and run by any host that has Docker installed.

Developping a Dockerfile

To package the program we developed in Session 3, let’s create a file called Dockerfile that has the following content:

FROM openjdk:15-jdk-slim

COPY bin bin

ENTRYPOINT java -classpath "bin/" -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:8000 Helloworld

In this Dockerfile we do the following steps:

  • First we indicate which base image may be used via the FROM instruction.
  • Second we copy the local bin folder of the virtual machine on the file system on the container : the container now has the last compiled version of the application.
  • Finally, we define the command line that should at the startup of the container.

Creating a docker image

To create the image, run the following command

docker build . -t lowcomote

The process of creating the image should start as follow:

the image should now be available on the list of images:

root@lowcomote-VirtualBox:/home/lowcomote/workspace# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
lowcomote           latest              d4178dde65ba        2 minutes ago       413MB
openjdk             15-jdk-slim         7850bfdb733e        2 days ago          413MB

Running a container based on the docker image

Now that the lowcomote is created, we can now a container based on the lowcomote image. To do so, run the following command:

docker run --detach -p 8000:8000 --name lowcomote lowcomote

And check that the container is running:

root@lowcomote-VirtualBox:/home/lowcomote/workspace# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
2507834ab420        lowcomote           "/bin/sh -c 'java -c…"   8 seconds ago       Up 4 seconds>8000/tcp   lowcomote

The output above confirms that the container has been created!

(Bonus) Publish the docker image

Run a local registry on the virtual machine:

docker run -d -p 5000:5000 --restart=always --name registry registry:2

To upload the lowcomote image to the private repository, we will now tag it with its hostname and port:

docker tag lowcomote localhost:5000/lowcomote

Once the tagging is done, we can push the image to the private repository:

docker push localhost:5000/lowcomote

You can try to pull the image from the local repository as follow:

docker pull localhost:5000/lowcomote

Now, aks your neighbour for his IP address. Let’s call it MY_NEIGHBOUR_IP, let’s grab its version of the lowcomote image:

docker pull $MY_NEIGHBOUR_IP:5000/lowcomote

The image should have been downloaded correctly:

root@lowcomote-VirtualBox:/home/lowcomote# docker image ls
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE   latest              d4178dde65ba        29 hours ago        413MB

You can run your neighbour’s image as follow:

docker run --detach -p 8000:8000 --name lowcomote $MY_NEIGHBOUR_IP:5000/lowcomote