docker installation in ubuntu

The below steps are for installing the specific version of docker-ce in ubuntu

1. Run the below command to download the package.


2. Run the below command to install the same.
sudo dpkg -i docker-engine_17.05.0~ce~rc3-0~ubuntu-trusty_amd64.deb

The below steps are for installing the docker engine in ubuntu

1. Run the below command to find out the available version
sudo apt-cache showpkg docker-engine

2. Then select the specific version and run below to install the docker-engine. The below one is used to install 1.13.1 version of docker-engine. 
sudo apt-get install docker-engine=1.13.1-0~ubuntu-trusty

Docker container – health check

HEALTHCHECK instruction is used to tell the Docker how to test a container to see if its still working or not.  This is used to detect cases such as a web server that is stuck in an infinite loop and unable to handle new connections, even though the server is still running.

In this post, I am going to show an example how we can add this instruction in the Dockerfile and test this.

Refer my code @java-rest-docker

Here is my Dockerfile

FROM java:8


ADD hello-rest-${project.version}-jar-with-dependencies.jar /opt/helloRest.jar
ADD bin /opt/bin

RUN chmod +x /opt/helloRest.jar
RUN chmod +x /opt/bin/

CMD ["/opt/bin/"]


HEALTHCHECK --interval=1m --timeout=3s CMD curl -f http://localhost:8080/api/greeting || exit 1

Check the last line of my Dockerfile where I instruct the docker to access localhost:8080/api/greeting in every 1 min and do the health check and fail it if there are any issues. If the exit status 0 when the container is healthy. If it’s 1, then it’s not in healthy status.

My docker CE version is 17.06.2-ce.

Now Build the image and the run the docker container.Then run this command and check the health check output. Here replace the container name “elegant_bartik” with your local container name.

  docker inspect --format "{{json .State.Health }}" elegant_bartik

{“Status”:”healthy”,”FailingStreak”:0,”Log”:[{“Start”:”2017-09-19T18:23:54.612256172-04:00″,”End”:”2017-09-19T18:23:54.652678939-04:00″,”ExitCode”:0,”Output”:” % Total % Received % Xferd Average Speed Time Time Time Current\n Dload Upload Total Spent Left Speed\n\r 0 0 0 0 0 0 0 0 –:–:– –:–:– –:–:– 0\r100 11 100 11 0 0 1022 0 –:–:– –:–:– –:–:– 1100\nHello World”},{“Start”:”2017-09-19T18:24:54.652833383-04:00″,”End”:”2017-09-19T18:24:54.696685407-04:00″,”ExitCode”:0,”Output”:” % Total % Received % Xferd Average Speed Time Time Time Current\n Dload Upload Total Spent Left Speed\n\r 0 0 0 0 0 0 0 0 –:–:– –:–:– –:–:– 0\r100 11 100 11 0 0 842 0 –:–:– –:–:– –:–:– 916\nHello World”},{“Start”:”2017-09-19T18:25:54.696867514-04:00″,”End”:”2017-09-19T18:25:54.748451647-04:00″,”ExitCode”:0,”Output”:” % Total % Received % Xferd Average Speed Time Time Time Current\n Dload Upload Total Spent Left Speed\n\r 0 0 0 0 0 0 0 0 –:–:– –:–:– –:–:– 0\r100 11 100 11 0 0 814 0 –:–:– –:–:– –:–:– 846\nHello World”},{“Start”:”2017-09-19T18:26:54.74862166-04:00″,”End”:”2017-09-19T18:26:54.798137953-04:00″,”ExitCode”:0,”Output”:” % Total % Received % Xferd Average Speed Time Time Time Current\n Dload Upload Total Spent Left Speed\n\r 0 0 0 0 0 0 0 0 –:–:– –:–:– –:–:– 0\r100 11 100 11 0 0 663 0 –:–:– –:–:– –:–:– 687\nHello World”},{“Start”:”2017-09-19T18:27:54.798446904-04:00″,”End”:”2017-09-19T18:27:54.845142125-04:00″,”ExitCode”:0,”Output”:” % Total % Received % Xferd Average Speed Time Time Time Current\n Dload Upload Total Spent Left Speed\n\r 0 0 0 0 0 0 0 0 –:–:– –:–:– –:–:– 0Hello World\r100 11 100 11 0 0 991 0 –:–:– –:–:– –:–:– 1100\n”}]}

Testing the mail functionality with Fake Smtp Server

In this post, I am going to show how we can integrate the FakeSmtp server with a java application.

Fake SMTP is a dummy SMTP server which is mainly used for testing the emails. It’s useful for the developers as it emits the email content into a local file directory. So we can check the local file content and do the validations.

The Dockerfile for the Fake Smtp is given below,


FROM java:8

RUN mkdir -p /opt && \
  wget -q && \ 
   unzip -d /opt && \

VOLUME /output

RUN chmod +x /opt/fakeSMTP-2.0.jar


CMD java -jar /opt/fakeSMTP-2.0.jar --start-server --background --output-dir /output --port 25

If you refer the last line of the above file, you can understand that the email content would be written under the /output folder. So we have to mount the local directory accordingly in the docker compose file.

Next one is the REST application code to send out the email to Fake SMTP server.


package com.resource;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class MailResource {

    Properties properties = System.getProperties();

    public MailResource() {
        properties.put("", System.getenv("SMTP_HOST"));
        properties.put("mail.smtp.port", "25");

    @Produces(MediaType.TEXT_HTML) public Response sendSimpleHelloMail() throws WebApplicationException {

        String to = "";
        String from = "";
        Session session = Session.getDefaultInstance(properties);
        try {
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setContent("<h1>Hello</h1>", "text/html");
            System.out.println("Sent message successfully....");
        catch (Exception ex) {
            throw new WebApplicationException(ex.getMessage());
        return Response.ok().entity("Mail has been sent successfully").build();

The SMTP_HOST enviornmental variable should hold the Fake SMTP server host. In this case, we have to link the Fake SMTP service with the REST container and give that service. Refer the below docker-compose.yml file to know how to do that.


    image: mail-rest:latest
        - 8080:8080
       - SMTP_HOST=mail
      - mail:mail
    image: fake-smtp:latest
     - ./output:/output
     - 25

Here we have mounted the local output directory to output folder. so the mail content will be available under the output folder and linked the fake-smtp service and giving that in the SMTP_HOST env variable.

Follow the below steps to run this application,

1. Clone this repository
2. Package the project by running mvn Package
3. Run ‘docker images’ and confirm that the ‘mail-rest’ docker images is available.
3. Then go into fake-smtp folder and build the image by running ‘docker build -t fake-smtp:latest . ” and confirm that the ‘fake-smtp’ docker images is available.

4. Then go to project root folder(java-mail-rest) and run “docker-compose up -d”
5. Access the REST endpoint with http://localhost:8080/api/sendEmail
6. Check the output folder and confirm that there is an eml file created which has the email content

Here is the sample file content


        Sat, 22 Jul 2017 19:27:10 +0000 (UTC)
Message-ID: <840194110.01500751630345.JavaMail.root@2c7d28e1a4a2>
Subject: Subject
MIME-Version: 1.0
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit


Refer the code @

docker-gc: Utility to do garbage collection

The “docker-gc” is used to clean up the unused containers and images. By defualt, It removes all the containers that exited more than an hour ago and also the images that don’t belong to any remaining containers.

Refer the GitHub(docker-gc)

We can use this utility as a script and container. To run this utility as container, then use the below command. The container will start up, run a garbage collection process, then shutdown.

docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v /etc:/etc:ro spotify/docker-gc

There is an another cron utility docker-gc-cron which internally uses the docker-gc utilty for cleaning up the unused docker containers and images. This utility is very useful for scheduling the cleanup process on Jenkins CI nodes.

docker-slim: Utility to reduce the fat size docker images

The “docker-slim” is a magic diet pill for our containers. It uses the static and dynamic analysis to create skinny image variants of our fat images.

To use docker-slim, we have to download its binary from docker-slim. Binaries are available for Linux and Mac. Once we download the binary, then add it to PATH.

I have used the “helloworld-docker” image for this exercise.  Refer my GitHub project (java-rest-docker).

Assume that, I have already built the docker image. Let’s run the “docker images” command to check the size of it. The below is the response.

Command: docker images

REPOSITORY               TAG                 IMAGE ID            CREATED          SIZE
hello-rest               latest              81a33e78995c        3 minutes ago       651.4 MB
java                     8                   d23bdf5b1b1b        4 months ago        643.2 MB

Let’s run the docker-slim utility now. The utility will run and create a new slim image.

  sudo docker-slim build --http-probe hello-rest

Then run the “docker images” command and check the output. In the output, I see a new image named “hello-rest.slim” and its size is 193.6 MB which is better than the original image size. Make sure to run the newly created image and check if its working or not.

Command: docker images

REPOSITORY               TAG                 IMAGE ID            CREATED          SIZE
hello-rest.slim          latest              1507f864ebbc        9 seconds ago       193.6 MB
hello-rest-docker        latest              81a33e78995c        3 minutes ago       651.4 MB
java                     8                   d23bdf5b1b1b        4 months ago        643.2 MB

Create Docker image with Maven build

There are lots of maven Docker plugin available to integrate the docker with maven.

In this example, I am going to show how to build the Docker image while building a maven project.

Copy the below snippet and put into your pom.xml file and then create a maven property “” with the appropriate docker image name and also make sure that the Dockerfile available in the correct location.

Then run the ‘mvn install’ and once its done, run ‘docker images’ and check that the docker image is available in the list of images.



Rest API to produce message to Kafka using Docker Maven Plugin

I have developed a simple REST API to send the incoming message to Apache Kafka.

I have used Docker Kafka ( and the Docker Maven Plugin( to do this.

So before going through this post be familiarize yourself with Docker and Docker Compose

Docker Maven Plugin[Docker Maven Plugin] provides us a nice way to specify multiple images in POM.xml and link it as necessary. We can also use Docker compose for doing this. But I have used this plugin here.

    1. Clone the project (
    2. Then go into kafka-message-sender folder
    3. Then enter ‘mvn clean install’
    4. Then enter  ‘mvn docker:start’. Then enter ‘docker ps’ and make sure that there are two containers are running. The name of those containers are kafka, kafka-rest
    5. Then access http://localhost:8080/api/kafka/send/test (POST) and confirm that you see message has been sent on the browser
    6. Then enter the below command and make sure that whatever message which you sent is available at Kafka[Kafka Command Line Consumer] or you can also consume via a Flume agent[Kafka Flume Agent Consumer]
docker exec -it kafka /opt/kafka_2.11- --zookeeper localhost:2181 --topic test --from-beginning