Why Docker needed ?
In today’s software/application development, DevOps team’s biggest challenge for teams is to manage applications dependencies across various development environments. As part of their routine tasks, they must keep the application operational and stable—regardless of the underlying platform that it runs on.
However, for developers, primary focus is on to release new features and updates for the application. It’s highly possible that developers sometimes ignore or compromise application’s stability by deploying code which can cause issue in different environments.
To avoid, such instances, companies are adapting containerized framework which allows designing stable framework without adding additional complexities. One of containerized platform is Docker. We will see docker details now
What is Docker?
Docker is a Linux-based, open-source containerization platform designed to simplify the process of developing, shipping, and running applications.
It achieves this using containerization, a technology that packages an application and its dependencies together into a single unit called a container. These containers are lightweight and efficient and ensure that an application runs seamlessly across different computing environments.
Unlike virtual machines, docker containers offer:
OS-level abstraction with optimum resource utilization
Interoperability
Efficient build and test
Faster application execution
Fundamentally, Docker containers modularize an application’s functionality into multiple components that allow deploying, testing, or scaling them independently when needed.
What is Docker Engine?
The Docker engine is the core of the Docker platform. It manages containers, including their creation, running and shipping and the entire lifecycle. When you install Docker, the Docker engine gets installed as well. This engine is the primary client-server technology that manages containers using all Dockers services and components.
Docker Engine acts as a client-server application with:
Docker Daemon - A server with a long-running daemon process dockerd.
Docker API - APIs which specify interfaces that programs can use to talk to and instruct the Docker daemon.
Docker Client - A command line interface (CLI) client docker
Docker Daemon
Docker daemon is a fundamental Docker component. It is a background process that listens to requests from the Docker client and manages the creation and running of Docker containers. The Docker daemon can be considered as the engine that powers the Docker environment. This allows developers to run, build and manage containerized applications.
The Docker daemon pulls Docker images from registries and manages the resources needed to run Docker containers. Docker daemon functions include image management, volume management, network management and container management.
Docker API
The Docker API is a programmatic interface that communicates with the Docker daemon. With the Docker API, you can tell the Docker daemon to perform tasks like starting, stopping and deleting containers or downloading or uploading Docker images. Docker API makes networks and volumes possible and manages user permissions and access.
All Docker daemon tasks are possible due to the Docker API. Without the Docker API, communicating with the Docker daemon programmatically wouldn't be possible
Docker Client
This is the primary mode of communicating with the Docker daemon. The Docker client is a command line interface (CLI) developers use to interact with the Docker daemon from their computers. When a user uses a command such as a docker run, the Docker client sends this request to the Docker daemon.
With the Docker client, developers can manage Docker containers, images, networks, and volumes from the command line.
Docker mechanism flow for standard image from registry
The docker client, docker daemon and docker API work together for a complete docker mechanism flow
Using the Docker client, you can pull an image from a registry (such as Docker Hub) or build an image from a Dockerfile
$ docker pull nginx:latest
//The above will pull the nginx image from Docker Hub
$ docker build -t <image_tag>.
// When you run the above in a directory with a Dockerfile, it builds and tags it
Once you have an image, create a container using the Docker client
docker run --name mycontainer -d nginx:latest
The above command requests the Docker API to create a container
The Docker API communicates with the Docker daemon to create the container. The Docker daemon sets up a network interface that allows the container to communicate with other containers or the host system. It also sets up volumes that the container can use to persist data.
You can interact with the container once it's running using the Docker client. You can use the following commands to do this.
$ docker exec -it mycontainer bash# Execute a command inside a running container
$ docker logs mycontainer # View the logs of a container
$ docker stop mycontainer # Stop a container
$ docker start mycontainer # Start a container
Docker client can be used to stop or delete the container
$ docker stop mycontainer # Stop a container
$ dockerrm mycontainer # Start a container
The Docker client sends commands or requests to the Docker API, which communicates with the Docker daemon to create and manage containers and their resources.
Containerizing Java application
Sample application code
public class HelloWorld {
public static void main(String[] args){
System.out.println("Welcome to Java application");
}
}
Write a Dockerfile
First step is to create empty file named Dockerfile. A Dockerfile is a plain-text file that specifies instructions for building a Docker image. You can create this in your project’s root directory:
FROM openjdk:11
MAINTAINER test.com
COPY target/docker-java-jar-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
Here,
FROM –we’re importing the OpenJDK Java version 11 image as our base image from their official repository.
MAINTAINER - we specify the maintainer for our image. This step doesn’t create any additional layers
COPY – we create a new layer by copying the generated jar, docker-java-jar-0.0.1-SNAPSHOT.jar, from the target folder of the build context into the root folder of our container with the name app.jar
ENTRYPOINT – we specify the main application with the unified command that gets executed for this image. In this case, we tell the container to run the app.jar using the java -jar command. Also, this line does not introduce any additional layer.
Build docker image
Docker images are instructive templates for creating Docker containers. You’ll build your Docker image at your project’s root directory, and entering the following command:
docker image build -t docker-java-jar:latest .
Here, we use the -t flag to specify a name and tag in <name>:<tag> format. In this case, docker-java-jar is our image name, and the tag is latest. The “.” signifies the path where our Dockerfile resides. In this example, it’s simply the current directory
Note: We can build different Docker images with the same name and different tags.
Run application as docker container
docker run docker-java-jar:latest
The above command runs our Docker image, identifying it by the name and the tag in the <name>:<tag> format
Conclusion
The importance of virtualization technologies grows with an increasing speed. Containerization gains more and more attention because companies want to develop software faster, cheaper and better. Considering continuous integration, delivery or deployment, DevOps need to choose the right way for their own use case.
Docker is a great tool and supports the continuous deployment pipeline. It’s got a good integration in already existing configuration management tools. With its big and growing ecosystem it offers many use cases and we can be excited for more things to come!
Comments