What is Docker and How is it Used with Payara Server and Payara Micro?
Originally published on 19 Feb 2019
Last updated on 18 May 2020
What is Docker?
Docker is a platform which makes it easier to create, deploy and run your applications using containers. A container bundles all the software needed to run it. By packaging the required dependencies, it makes it easy to run it on any machine, regardless of small configuration differences.
If you're familiar with Virtual Machines (VM), the concept is similar in principle, but more lightweight and utilizes the host operating system. A VM has an entire operating system in it whereas containers are just isolated processes which run on the host operating system.
Docker is primarily built for Linux machines, but there is also a Windows and Mac solution available.
You can start a container from a Docker image. A Docker image is a layered structure where you define the process which needs to be run and the files needed to run it, like the Payara Server installation and your application.
Since the Docker image has a layered structure, the files which are defined in a layer can be shared by different images and containers as they are read-only. Each container keeps track of the changes to these files as long as the container exists. If you need to persist data so that it survives a container recreation, a database for example, you need to use volumes.
Docker Architecture
On one side you have the Docker Engine which actually runs your container. It has a daemon process that hosts images, containers, networks and storage volumes. It is responsible for running your process in an isolated fashion.
On the other side, you have the Docker Client which can be used to control the Docker Engine Daemon process. You send commands via the Client to start or stop containers but also to build images. The Client and the daemon process communicate through REST. There are also other ways to communicate to the Docker Engine daemon, like a Java implementation of the docker API.
Basic Docker Commands
So let's take a deeper look into the Docker Client, the Command Line tool in this case.
If you do not have Docker installed yet, please have a look at the official Docker installation page. It gives you more information on how to install the Community Edition on your Linux machine or how you can use Docker Desktop if you are running on Windows or Mac.
In order to verify everything is installed correctly and up and running, you can issue the following command on the command line.
docker info
It will give you info about your installation. Here, info is a command we send to the Docker Engine daemon. Some other important commands are:
- run Download the Docker Image if needed and create and start a new container from the Image.
- ls Get a list of all the containers maintained by Docker Engine.
- stop S top a container, this will keep the changes to the file system.
- start Start a container.
- rm Remove a stopped container. You will loose all changes to the Container file system when executing this command.
Each command has an extensive set of options you can specify. Have look at the official documentation or our Docker guide for more info on these and other commands.
Payara Docker Images
We have Docker images for Payara Server and Payara Micro available for download on Docker hub. Docker hub is a repository where you can store and share your Docker images.
You can use these images to start a container directly from it. Or you can extend these images, customizing them to your needs and use those instead.
In the next sections, we will give you some examples on how you can use these images. While Docker images are optimized for use in production scenarios, you can also use them for development purposes.
How Do I Use Docker Images with Payara Server?
So let's startup Payara Server using the official Docker Image.
docker run -d -p 4848:4848 -p 8080:8080 --name payara-test payara/server-full:5.184
The options we have specified are:
- -d Run the container in the background. (detached)
- -p Make a port mapping between your host and the process which is running the Payara Server process.
This Docker process runs in a separate network segment (part of the isolation aspects of Docker). In this case, we just map the default endpoints for the Admin Console and the Http listener serving our application to the same ports on our local machine.
--name We give the container a specific name. A Docker Container always gets an id (a longer number which is difficult to use) and a name. If we do not specify this name, Docker will generate a random (nickname) for us like furious_heisenberg, agitated_darwin, ...
payara/server-full:5.184 . This is the image name we use to create our container from in the form of <community>/<repository>:<tag>. Here we use the Payara Server 5.184 Image.
After you have started the container, you can access the Payara Web Administration Console in your browser using the well-known URL http://localhost:4848.
If you want to start the container and immediately deploy an application to the server, you can use following command:
docker run -p 8080:8080 -v ~/payara/apps:/opt/payara/deployments --name payara-test payara/server-full:5.184
-v This options indicates a Docker Volume usage. In this example, the /opt/payara/deployments within the Docker Container is mapped to the ~/payara/apps directory on the host.
The Docker image is configured in such way that applications (war and ear files) within the /opt/payara/deployments are deployed automatically. In this case, it means that applications within the ~/payara/apps of your machine will be deployed automatically because these directories are mapped.
How do I Use Docker with Payara Micro?
The Payara Micro Image can be used in a similar way as the Payara Server image. As an example, we will show you the steps to extend the official Image to include your application within a new image.
When you create a file called Dockerfile with the following content:
FROM payara/micro COPY myapplication.war $DEPLOY_DIR
We create a Docker Image based on the official Payara Micro one where we add our application into a directory inside the Docker image. This directory will be scanned for applications which needs to be deployed.
From within the directory containing the Dockerfile and the myapplication.war files, we can launch the following command to generate the build.
docker build -t myapplication .
- -t The value after this option specifies the name we give to our Image.
- . The dot at the end is important as we indicate where the required files can be found (in this case the current directory)
Once we have the Image, we can execute it in a similar way as the Payara Docker Image.
docker run -d -p 8080:8080 --name myapplication myapplication
Conclusion
With Docker you can run a process in an isolated fashion, supplying it with all the dependencies it needs.
This makes it possible to run a program in a consistent way on different environments in a much more lightweight way than when you are using Virtual Machines.
On this blog, we also went over the basic commands that you can use to run the official Docker Payara Images and to create your custom Images based on the official ones.
Related Posts
What’s New In The Payara Platform December 2024 Release?
Published on 18 Dec 2024
by Luqman Saeed
0 Comments
Payara Platform Community 7 Alpha 2: Your Gateway to Jakarta EE 11
Published on 03 Dec 2024
by Luqman Saeed
0 Comments