Introduction to Docker and Docker containers in Java

In short, Docker is a tool that allows you to build, deploy and run applications easily by the usage of so-called containers. These containers let us package all the essentials such as libraries and dependencies.

java-featured-image

In addition, the containers run on the host operation system.

There are many benefits that come when we use Docker. It

  • enhances the portability of the application and test applications can be created very easily and won’t be dependent on the environment
  • optimizes infrastructure
  • packages the application with its dependencies into a standardized unit

Containers

No, I wasn’t talking about containers in the real world. But now that you are thinking about it, I might as well make an analogy. The purpose of the real-life containers is to store goods or items which need transportation to another place. Now, these goods and items have different storing requirements, for example there may be milk which expiry date is way shorter than for example tomatoes. That’s why containers in real life are a very useful thing – they preserve the inner environment like temperature for sensitive goods and/or items. With that said, the transportation industry does not have to worry about the items and focus on getting them from A to B.

Now, if you’ve paid attention to the paragraph above, containers in Docker are the same thing. Thanks to Docker’s containers we can easily move containerized application components from one environment to another with hopefully no issues.

Just like containers in real life, containers in Docker provide us with an isolated and secure environment for the application components. Compared to the real life containers analogy, the application components are the goods.




 

Docker containers vs Virtual Machine

Container structure in Docker

 

Virtual machine structure Docker

Based on the images, the problem with the VM approach is that it comes with a lot of things – binaries and libraries (whcih is essential) but the main thing – an entire guest operating system which amount to several  GBs.

On the other hand, the Docker container includes all the dependencies of an application and shares the kernel with other containers. In addition, the Docker containers can run on any computer, infrastructure and cloud. Now, you can read the real-life containers analogy again and see the similarities.

Installing Docker

If you are using Windows or Mac, click here.

After you’ve installed Docker you have the following things:

  1. Docker Machine for running docker-machine commands
  2. Container Engine for running the docker commands
  3. Docker Compose for running the docker-compose commands
  4. Kitematic, the Docker GUI
  5. A shell preconfigured for a Docker command-line environment
  6. Oracle VirtualBox

To check that you have successfully installed docker, type the following:

docker --version

To view more information such as containers, how many are running, paused or stopped, images and so on, you can type:

docker info

And that’s it! You have Docker on your machine. To write a simple hello world “program”, you can type the following:

docker run hello-world

The response:

This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.
0 0 votes
Article Rating
guest
0 Comments
Inline Feedbacks
View all comments