Tuesday, January 12, 2016

Docker - Creating simple java image

This is my "Hello World" of creating my own docker image for running a Java program in Java 7 environment.

You can find information about Docker here. Developers need to understand concepts like Docker, docker-machine and how to write a Dockerfile to be able to create own docker images.


Create Dockerfile 


This is how a Dockerfile for our project would look like:

FROM java:7
MAINTAINER tuhin.gupta@gmail.com
COPY ./src/main/java /usr/src/myjavaapp
WORKDIR /usr/src/myjavaapp
RUN javac com/tuhin/example/Main.java 
CMD ["java", "com.tuhin.example.Main"]

see details of Dockerfile syntax here

In our project we are using following instructions:
FROM - tell docker to pull java 7 image
MAINTAINER - instruction allows to set the author field
COPY - instruction is to copy the java source code to docker image file system
WORKDIR - sets the work directory in docker image
RUN - runs javac at the specified location
CMD - this instruction can run any command that can be run from command prompt.


Java code in Main.java is:

package com.tuhin.example;

/**
 * @author Tuhin Gupta
 *
 */
public class Main {

public static void main(String[] args) {
System.out.println("This is the main class.");

}

}

Build Docker Image

Once the Dockerfile is created, now we need to build the docker image for java application

$ docker build -f <path-to-docker-context>/docker/Dockerfile -t myjavaapp <path-to-docker-context>

Above command will build the docker image.
-f : is used to specify the path to docker file within docker context. Dockerfile must be in docker context.
-t : Repository name (and optionally a tag) for the image

This command will build the docker image and you can view your recently created image using following command:

$ docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
myjavaapp                 latest              45a3aacd5bd0          1 hours ago                590.1 MB


Run your Docker Image

To run the newly created image and the java program inside the image, use following command:

$ docker run -t --rm --name test-myjavaapp myjavaapp 

This will run myjavaapp image and Main.java class within it.


Save your Docker Image as tar file for sharing

Using following command, you can save a docker image as tar file and share between developers.

$ docker save -o <path-to-save>/myjavaapp.tar <image id>

<image id>: image id from docker images command

Refer to my Github site - https://github.com/tuhingupta/docker-simple-java and download the Dockerfile.



Monday, January 11, 2016

Docker inside corporate VPN Network


If you are trying to create a docker machine and use docker within a corporate VPN network it might not work straight out of an example. 
Following steps would help. When we are creating docker machine, we would need to specify proxy information so that docker can then connect to Docker Hub.

Create docker machine as follows:
$docker-machine create --driver=virtualbox --virtualbox-memory=4096 --virtualbox-cpu-count=2 \
--engine-env HTTP_PROXY=http://proxy.yourorg.com:8989 \
--engine-env HTTPS_PROXY=http://proxy.yourorg.com:8989 \
--engine-env NO_PROXY=localhost,127.0.0.1,localaddress,.localdomain.com,.local,127.0.0.0/8 \
office


Then go to the docker machine VM on VirtualBox and you would need to do port forwarding.
  1. Go to the VM on virtual box machine and go to Settings>Network
  2. For Adapter1, open Port Forwarding and add port forwarding information. (You can get your machine IP and port information using ` $docker-machine env office` command )

















Once port forwarding information is saved, on terminal run following commands

$ eval "$(docker-machine env office)”
$ export DOCKER_HOST=tcp://127.0.0.1:2376
--disable TLS security if you get certificate error when you do $ docker ps
$ alias docker="docker --tlsverify=false"

Now commands like $ docker ps and $ docker search <image> would work.



If you are working outside of office network, offline at home, then you can create a docker machine without proxy information. Use this machine when offline.

$docker-machine create --driver=virtualbox --virtualbox-memory=4096 --virtualbox-cpu-count=2 \
default