Introduction to Docker

Docker is a lightweight, open and secure container platform. Docker simplifies the process of building, shipping and running applications. It is always a challenge when it comes to deploying your application stack together with its dependencies and getting it to run without issues. Docker offers complete set of tools to deploy everything that forms an application across systems and machines.

Main Docker elements are:

  • Docker Images
  • Docker Containers
  • Dockerfile

An Image is used to build a container. Image contains the application and the  necessary files to run the application in a minimal Linux/Unix environment. So for example, if you’re working in PHP then you’d have PHP as well as your application code built into your image. And based on this image we can create a running instance called a container. Image is a read-only template composed of layered filesystems. Common files are usually shared using a base image. New layers are added on top of this base image to include more files and applications. This final image is used to create Docker container instances.



Containers are actually where the live application runs, or the database or whatever it may be that you need to run on an OS.

Container is an isolated and secure shipping container created from an image that can be run, started, stopped, moved and deleted.

Difference between Docker containers and virtual machines

Virtual machines always run on top of a host operating system. So you would always have a host running OS and then you can run a guest OS on top of a something called a hypervisor. So in general you have a copy of OS for every virtual machine and it’s quite expensive to start multiple virtual machines.

Docker containers are running in Docker engine which is integrating containers with the host OS. So we have OS running (host) and containers which are sitting right on top of OS. And this image is much smaller then running a guest OS and of course a lot faster.

Docker depends on some special functionality present in newer versions of Linux/Osx machines. Unfortunately this is not part of windows as of today. Also earlier version of macs have this problem. So when we use a windows machine for development, we usually use an actual virtual machine to run docker. And as we usually deploy the live version in a linux based server, there we don’t need this. Docker Toolbox provides images and container tools and required virtual machine for windows (and earlier mac) users. By default it uses Virtual Box and the docker toolbox will provide that automatically for you. Docker Toolbox contains following:

  • Docker client – interaction with containers
  • Docker machine – allow you to work with virtual machine and set up environment variables
  • Docker compose – allow to run multiple containers and interaction between them
  • Docker kitematic – allow you to  find and use other images available in docker hub using GUI
  • VirtualBox

Simple example how to run your Java application in a docker container

Let’s assume you have a Java application which is using PostgreSQL database. You need one docker container to run the java application. You can either run PostgreSQL in another container, or you can simply connect to an existing on. In this example we will run the PostgreSQL in separate Docker container. Simplest way to run PostgreSQL database is to use one of the existing Docker images from Docker repository. Docker images for Postgres are available here:

So for example you could pull Docker image for PostgreSQL 9.4 by running the following command:

After successfully pulling you can run:

Which will list all your docker images and you should see postgres image.

To create docker network use the following command:

And we are ready to start the docker container:

If you run command:

 you will see your container is running.

You could also run the following command to enter in psql and for example create database:

(hint: to exit psql run: \q)

But the problem with the following setup is that once you stop your container all the data will be lost. To avoid this we will mount volume to save data on host. Firstly we remove the container which is already running by executing command:

and then we mount the volume like this:

And directory should be created inside the path you specified with the following content:

Also very useful command is : 

which will give you all the info about the container. And if you run that on the container we just created you could see a the following part:

Now postgresql is up and running, and we could create docker image for our java app. We start by creating Dockerfile.

Let’s create a directory called: /app/

Inside of this directory create a file named Dockerfile and put the following content inside:

But let’s stop here and explain Dockerfile a bit. Dockerfile has to be named “Dockerfile” anything else it won’t work. It’s a plain text file. Dockerfile contains instructions how to build an image. Also very important to mention here is that any files or directories in the same location where Dockerfile is are included in the build! So that means it’s not good to have Dockerfile in root unless of course we want to include all the files from root. Comments in Dockerfile are marked with #.

# This is comment

FROM has to be first instruction in Dockerfile, it tells us on what image this image is based on!

So in example above ‘FROM openjdk:8’ means that our image will be based on openjdk:8 image.

MAINTAINER instruction can go anywhere in the file but it’s standard practice to keep it at the top as well as it’s standard practice to capitalize instructions.

RUN instruction is important in Dockerfile and:

  • It’s used to run commands against our images that we’re building
  • Every RUN instruction adds a new layer to our image
  • Build time instruction

CMD instruction is executing command. It only executes at runtime which means it runs once the container is launched. Also it’s important to note that it could only be one CMD instruction per Dockerfile.

ARG instruction allows us to pass argument during image build process

ENV instruction allows us to set environment variable, in our use case we pass virtual name of postgres

Now let’s continue with creating our docker container for our Java app. Inside the same directory create also file with the following content:

And move also YOUR_JAR.jar to this directory.

To create an image, we could run the following command:

If you run docker images:


Start docker image by running:

And your app should be running and available on port 8080. Flag “-p” publish a container’s port or a range of ports to the host. In the example command above -p 8080:8080 this means that port 8080 from docker container is exposed and bound to 8080 on the host, so our application will be available on port 8080.


Introduction to Docker with ExampleAdnan KukuljacProgrammingTutorialsIntroduction to Docker Docker is a lightweight, open and secure container platform. Docker simplifies the process of building, shipping and running applications. It is always a challenge when it comes to deploying your application stack together with its dependencies and getting it to run without issues. Docker offers complete set...The point is to understand