Docker Tutorial for Java Developers
Let’s see this Docker Tutorial for Java Developers.
You can also check this tutorial in the following video:
1. Introduction
So in this article, we will explain what is Docker and why do we use Docker. Going further we will learn some concepts of Docker and later, we will see how to create Dockerfile.
We will then create Docker Image from Dockerfile. And finally, we will see how to run Docker images in the container using java code.
2. What is Docker?
According to the official documentation of Docker, Docker is a platform for developers and sysadmins to develop, deploy, and run applications with containers. The use of Linux containers to deploy applications is called containerization. Containers are not new, but their use for easily deploying applications is.
2.1 What is a Docker container?
A container image is a lightweight, stand-alone, executable package of a piece of software that includes everything needed to run it: code, runtime, system tools, system libraries, settings.
Containers isolate software from its surroundings, for example, differences between development and staging environments. Also, containers help reduce conflicts between teams running different software on the same infrastructure.
3. Docker Concepts
Let’s have a brief overview of the terms and concepts used in Docker.
3.1 Docker Image
An image is an executable package that includes everything needed to run an application–the code, a runtime, libraries, environment variables, and configuration files.
And when you have a Docker image, you run the container by using the image. So in nutshell, a container is a runtime instance of an image–what the image becomes in memory when executed (that is, an image with the state, or a user process).
3.2 Docker Hub
Docker Hub is a cloud-based registry service which allows you to link to code repositories, build your images and test them, stores manually pushed images, and links to Docker Cloud so you can deploy images to your hosts. It provides a centralized resource for container image discovery, distribution and change management, user and team collaboration, and workflow automation throughout the development pipeline.
So we use Docker hub to push our images and then pulling those images when required.
3.3 Dockerfile
Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using Docker build users can create an automated build that executes several command-line instructions in succession.
So while creating images you have to first create a Dockerfile. In Dockerfile, we write the instructions to create the image i.e what our image will consist of the libraries and the code that our application requires.
Base Image is an image in Docker from which you can create another image. By doing this we can use the infra of the base image, inside the child image.
Dockerfile Example
FROM java:8 #using Java 8 as the base image COPY . /home/user/hello #copying the code directory to /home/user/hello WORKDIR /home/user/hello #setting /home/user/hello as our work directory. Means code will reside here RUN javac printHelloImage.java #compiling the java class CMD ["java","printHelloImage"] #this command will be used to run application.
Now save this file and build the image by running the command below:
Docker build -t printhello
Here printhello
is the name of the image we want to build. Then, run this image by below command:
Docker run printhello
This was a short and simple example to create Dockerfiles. You can read more about writing Dockerfiles from here: Dockerfiles
4. Installing Docker
So this was the overview of Docker. Let’s install Docker now. Docker Community Edition (CE) is ideal for developers and small teams looking to get started with Docker and experimenting with container-based apps. So if you are not sure or just trying out things then you should install CE.
4.1 Installing Docker on Ubuntu using the repository
- Update the
apt
package index.sudo apt-get update
- Install packages to allow
apt
to use a repository over HTTPS:sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ software-properties-common
- Add Docker’s official GPG key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
- Use the following command to set up the stable repository.
sudo add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) \ stable"
4.2 Install Docker CE
- Update the apt package index.
sudo apt-get update
- Install the latest version of Docker CE, or go to the next step to install a specific version. Any existing installation of Docker is replaced.
sudo apt-get install docker-ce
- Verify that Docker CE is installed correctly by running the hello-world image
sudo docker run hello-world
For installing Docker on other Operating systems you can follow instructions from here depending on the operating system you are using.
5. Running java application with Docker
As I explained in creating Docker file, we will create a similar Docker file and we will see how we can run java application using Docker images.
5.1 Create Dockerfile
Create directory tutorial and in this directory create a file Dockerfile. Here is the Dockerfile:
FROM java:8 COPY . /home/user/hello WORKDIR /home/user/hello RUN javac PrintHelloDocker.java CMD ["java","PrintHelloDocker"]
So in this Dockerfile, I am using Java 8 as the base image. Then copy the current directory into /home/vikas/hello
(you can choose your own). Next, its setting /home/vikas/hello
as work directory. This means that Docker will look here for the code and then it will compile java file using RUN (its used to run the command inside containers). Then run the final command to run your application.
5.2 Create Java Class
Let’s create a simple java class to print “Hello Docker”
PrintHelloDocker.java
class PrintHelloDocker{ public static void main(String arg[]){ System.out.println("Hello Docker"); } }
5.3 Building Docker Image
Now we will build our first Docker image. So the command to build a Docker image is:
docker build -t <image name/tag>
So in the image tag, you can give a name so that you can clearly understand what this image does. So let’s give our image a name hello
as it is printing Hello Docker
when you run below command from the terminal:
sudo docker build -t hello
This will be the output:
Sending build context to Docker daemon 3.072kB Step 1/5 : FROM java:8 ---> d23bdf5b1b1b Step 2/5 : COPY . /home/user/hello ---> 63123154eb00 Removing intermediate container ee9310912e7d Step 3/5 : WORKDIR /home/user/hello ---> 61b27b711c36 Removing intermediate container 1680d2cd81fd Step 4/5 : RUN javac PrintHelloDocker.java ---> Running in d7dd4bc79723 ---> d3d467723a8b Removing intermediate container d7dd4bc79723 Step 5/5 : CMD java PrintHelloDocker ---> Running in 9a16a6513d3d ---> 3d9a2dcbe644 Removing intermediate container 9a16a6513d3d Successfully built 3d9a2dcbe644 Successfully tagged hello:latest
As you can see in the output, it is using java8 as our base image. As the application needs JDK to run, this is the reason we are using it as our base image.
Next, it’s copying our current directory to /home/vikas/hello
. Then it is setting work directory i.e code location to /home/vikas/hello
and finally it compiles PrintHelloDocker
by using:
RUN javac PrintHelloDocker.java
So our image is ready for us. Now we can run a container by using this image and let’s see if it does the task.
5.4 Running image in the container
The command to run a Docker image has the syntax:
sudo docker run <image name/tag>
Here the image name is the name of image or tag of the image you have. As we have given the name hello
to our image while building the image, we will be using hello
while running our image. Run the below command:
sudo docker run hello
Here is the output:
Hello Docker
As we want to print Hello Docker
, this is how you can run Docker images.
6. Docker Compose
According to Docker compose official documentation Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.
Using Compose is basically a three-step process:
- Define your app’s environment with a
Dockerfile
so it can be reproduced anywhere. - Define the services that make up your app in
docker-compose.yml
so they can be run together in an isolated environment. - Run
docker-compose up
and Compose starts and runs your entire app
6.1 Creating Docker-Compose file
Suppose your application needs MYSQL and nginx. Let’s create a Docker-Compose file to run these two as services.
docker-compose.yml
version: '2' services: mysql: image: mysql container_name: mysqlcontainer ports: - 3306:3306 environment: MYSQL_ROOT_PASSWORD: "javageeks" volumes: - /storage/docker/mysql-datadir:/var/lib/mysql web: image: nginx
In this yml we are creating two services mysql
and web
and starting mysqlcontainer
. image
is used to fetch image from the registry of Docker images. Let’s build and run our application services by running below command. Option -d is used to start in detached mode. This will start two containers. Let’s see our running containers:
sudo docker-compose up -d
sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4b4e2c4cadbd mysql "docker-entrypoint..." 4 minutes ago Up 3 minutes 0.0.0.0:3306->3306/tcp mysqlcontainer 69758a806d13 nginx "nginx -g 'daemon ..." 41 minutes ago Up 40 minutes 80/tcp gitclones_web_1
As we can see in the output mysqlcontainer
and gitclones_web_1
are running fine. So this is how you can set up services your application needs, by using Docker-Compose.
7. Docker Commands
Let’s have a brief overview of the commands use in Docker.
docker run
Run command in a new container.docker start
Starts one or more Docker container.docker stop
Stop one or more running Docker container.docker build
Build Docker image from Dockerfile.docker push
Push Docker image to registery.docker exec
Run a comand in a runtime container.docker search
Search for a Docker image in Docker hub.docker commit
Create new Image from a container’s changes.
8. Docker Tutorial – Conclusion
So in a typical java application, you create a Dockerfile at the root directory of your code. You have to make a Dockerfile and write commands to assemble all the things your application needs. Build Docker image using this Dockerfile using:
docker build -t <image name/tag>
And then run your image:
docker run <image name/tag>
Also, we were running Docker images from our local machine. You can push these images to a Docker repository like Docker Hub which is a central repository for storing images. Other people can pull images from this repository and can use as the base image.
For example in our Dockerfile, we were using java:8 as the base image so here java8 was pulled from the Docker hub because we didn’t have this image locally.
That’s it, hope you enjoyed this post. Thank you!
9. Download the Source Code
You can download the full source code of this example here: Docker Tutorial for Java Developers
Last updated on Jan. 31st, 2022