Docker is an software platform for building applications based on something called – containers.
Why do we need Docker ?
As we move towards microservice architecture, each application may be in different language like Python, Go, Java etc.. In that case the deployment process would be different for different application. How we can make it easy and generic ? Containers (Docker) is the answer.
Create a docker image for each of the microservice. Each image will not only be having the application code but also the Runtime environments (JDK, NodeJS) along with any other dependencies.
Docker Architecture
Docker Client and Docker Daemon:
Whenever we install docker desktop in our local machine, two things got installed- Docker Client and Docker Daemon.
It works on the principle of Client-Server architecture. Docker Daemon acts like the server.
Docker CLI is an example of Docker Client. The Docker Client talks to the Docker Daemon which does all the work like building, running and distributing your containers.
Registry / Repository/ Tag / Image / Container:
Docker Registry is the place where all docker images are kept. Below is an example of a public registry: https://hub.docker.com/
On the above registry we create repository. This repository contain a Image which has a Tag number. This Image is then downloaded to local machine and when this Image runs its called container.
In the below picture you can see the repository heapsteep having an image named spring-docker whose tag no. is 0.0.1-SNAPSHOT:
If you want to know the container solutions provided by AWS, here is the link.
How to install Docker ?
Go to the official site and click and download Docker Desktop for Windows installer.
Before installing the Docker Desktop, uninstall the previous version of Docker called Docker Toolbox for Windows. And delete all Docker related environment variables from the system.
Then follow the installation steps while installing the Docker Desktop for Windows.
After docker is installed successfully you can check it by opening a DOS command window and calling below command:
docker version
How to run Docker in local machine ?
Start -> Docker Desktop -> Open
How to pull and run the above image from dockerhub ?
Open a DOS command window and call below command:
docker run -p 8080:8080 heapsteep/spring-docker:0.0.1-SNAPSHOT
It will pull the image from docker hub and run the application as well. Isn’t it Magic !!..
In the above command the 1st port is the host port – the port on which the pulled image will run now.
The 2nd port is the container port – the port given to the image while uploading it to the Docker registry. This port is given in the Dockerfile.
If you want to run the above image in detached mode – even if you close the docker CLI, application will not stop:
docker run -p 8080:8080 -d heapsteep/spring-docker:0.0.1-SNAPSHOT
To run the above application you can call the below URL in a browser:
http://localhost:8080
Other docker commands:
Commands | Meaning |
---|---|
docker image ls | show all images |
docker image rm <image-id/name> | remove a particular image |
docker images | prints all images |
docker container ls | lists current running container |
docker container ls -a | lists all containers |
docker container stop c9b5 | initials of the container-id |
docker container rm c9b5 | to remove container |
docker container rm c9b5 -f | forcefully remove |
docker system prune -a | Will delete: all stopped containers, all networks not used by at least one container, all unused images, all build cache |
docker image remove c9b5 | remove an docker image |
How to create an Docker enabled Springboot application ?
Below are the steps for the same:
- Create a Springboot application.(don’t give the application name as –deleteIt or similar. Don’t know, the build never gets successful, its seems some kind of bug with Docker)
2. Add in pom.xml :
<!-- Docker --> <plugin> <groupId>com.spotify</groupId> <artifactId>dockerfile-maven-plugin</artifactId> <version>1.4.6</version> <executions> <execution> <id>default</id> <goals> <goal>build</goal> <!-- <goal>push</goal> --> </goals> </execution> </executions> <configuration> <repository>heapsteep/${project.name}</repository> <tag>${project.version}</tag> <skipDockerInfo>true</skipDockerInfo> </configuration> </plugin>
3. Add a file called “Dockerfile” at the same level of pom.xml having below contents:
FROM openjdk:8-jdk-alpine VOLUME /tmp EXPOSE 8080 ADD target/*.jar app.jar ENTRYPOINT [ "sh", "-c", "java -jar /app.jar" ]
Below is the meaning of each of the attribute:
FROM => Specifies which version of software to add. Basically here we were not using openjdk8 rather the openjdk8-alpine one which is very light weight in compare to the former.
ADD => Will copy the content from source to destination.
COPY => Will copy the content from source to destination, it will create the destination if its not there.
That’s it !!…. Your application is now docker enabled.
How to generate the docker image of springboot application ?
First start the local docker environment.
Do a maven build by calling the below command (here spring-boot is the maven plugin and build-image is the goal) :
spring-boot:build-image
An image will be created in your local docker environment.
P.S.: for spring boot 2.3 and beyond below are the steps for creating a docker image, instead of the above one:
Add below things inside the <plugin> of <spring-boot-maven-plugin>:
<!-- Docker --> <configuration> <image> <name>heapsteep/v2-${project.name}:${project.version}</name> </image> <pullPolicy>IF_NOT_PRESENT</pullPolicy> </configuration>