Docker is a developer tool to package applications along with their runtime environment, so anybody can deploy and run them in any other machine without facing runtime environment conflicts. It is very similar to virtual machine concept (virtualization), where you can get a VM image and run it on any supporting hardware. All internal programs in VM will function as they were packaged originally.
Difference between a VM and a docker image is that docker image does not package whole virtual operating system. It uses the OS resources just like other process in developer’s machine, only application and it’s runtime specific dependencies are packaged (Containerization).
Docker allows users to publish docker images and consume those published by others in repositories like Docker Hub.
In this tutorial, learn to install Docker container in windows, to create docker image and to deploy Docker image(which as one simple spring boot based microservice) in developer machine.
Docker Installation
To install docker on Windows 7 machine, follow the bellow steps:
Choose the appropriate Docker installer for your System
Before starting with the installation process we need to understand the exact Docker version that is suitable for the Windows that you are using. Docker has provided two versions of Windows distribution as bellow
In order to Docker toolbox works properly we need to make sure your Windows system supports Hardware Virtualization Technology and that virtualization is enabled. Docker has provided detailed step on this here: https://docs.docker.com/toolbox/toolbox_install_windows/#step-1-check-your-version. If we don’t have this enabled then we need to go to BIOS option and enable Hardware Virtualization. The BIOS is bit different for different models of Computer, so please follow official guideline for enabling that.
Run Docker installer
Once we have the Installer downloaded and we have enabled the Hardware Virtualization, we can start the installer. It’s just like a simple another windows based installation process guided by a installation wizard.
Verify your installation
To verify docker installation, open Docker Quickstart Terminal shortcut from either Desktop or Start menu. Verify that Docker prompt is coming and then need to test few basic commands. Docker prompt and sample docker command will look like below.
Note Down the Docker IP
We need to now note down the Docker IP assigned to this Container. We will access this IP to access the Applications installed inside Docker. To know the IP from the command prompt use command docker-machine ip. Here is the sample output of the command. Please note that this IP will be different for different M/Cs.
docker-machine ip output
Create docker Image
We will first create a spring boot based REST API, add docker specific configuration and then we will create docker image.
Create Spring REST Project
Develop one simple hello world Microservice for testing. We have used spring boot and Maven and Eclipse as IDE. Add a REST endpoints so that once this application is deployed in to Docker, we can test this by accessing the rest endpoint.
This is used by Docker while creating the image. It is basically declaring the Java runtime information and target distributions. For more details, follow docker builder reference.
Add Maven Docker Plugins
Add two maven plugins in the pom.xml file so that we can use the Docker related maven commands while creating the instance. Those plugins are dockerfile-maven-plugin and maven-dependency-plugin.
We have used the minimal configurations required to build the project.
So we have created the Docker Image (i.e. hello-docker-0.0.1-SNAPSHOT-docker-info.jar). We also have a installed docker container running in our local machine.
Now, to run the docker image inside installed docker container, we will use below command.
docker run -p 8080:9080 -t hello-howtodoinjava/hello-docker --name hello-docker-image
Here the option -p 8080:9080 is important. It says that expose port 8080 for internal port 9080. Remember our application is running in port 9080 inside docker image and we will access that in port 8080 from outside Docker container.
Now access the application with URL http://192.168.99.100:8080/hello/sajal. Notice that the browser output is same as output of standalone REST API on localhost.
Docker Localhost Output
Stop Docker Container
We can list down all docker containers by command docker ps in the terminal and we can use command docker stop <name>
Stop Docker Container
Summary
We learned to install Docker in Windows OS. We also learned to create a spring boot project with one REST endpoint and build Docker image for it. Then we learned to run the docker image inside docker container and tested REST endpoint inside docker image.
Docker is very cool tool to solve very old developer problem that “it works in my local machine”. Now if something works in your machine, you can surely run that on other machine as well.
Install Redis on Oracle Linux 7+ Install Redis Permalink In this section you’ll add the EPEL repository, and then use it to install Redis. Add the EPEL repository, and update YUM to confirm your change: sudo yum install epel-release sudo yum update Install Redis: sudo yum install redis Start Redis: sudo systemctl start redis Optional : To automatically start Redis on boot: sudo systemctl enable redis Verify the Installation Permalink Verify that Redis is running with redis-cli : redis-cli ping If Redis is running, it will return: PONG Configure Redis Permalink In this section, you’ll configure some basic persistence and tuning options for Redis. Persistence Options Permalink Redis provides two options for disk persistence: Point-in-time snapshots of the dataset, made at specified intervals (RDB). Append-only logs of all the write operations performed by the server (AOF). Each option has its own pros and cons whic...
Microservices Using Spring Boot and Spring Cloud Microservices is the hot buzzword in software development and many organizations prefer building their enterprise applications using microservices architecture. In the Java community, Spring Boot is the most widely used framework for building both monoliths and microservices. I am planning to write a series of articles covering how to build microservices using Spring Boot and Spring Cloud . In this article, we are going to learn about the following: Monoliths What are microservices? Advantages of microservices Challenges with microservices Why Spring Boot and Spring Cloud are a good choice for microservices Introducing the application Traditionally, we are building large enterprise applications in a modularized fashion (?!) but finally deploy them together as a single deployment unit (EAR or WAR). These are called monolithic applications. There are some issues with the monolithic architecture, s...
Comments
Post a Comment