In this article we are going to cover Build Docker Image using GitLab CI CD.
How to Install Docker on Ubuntu 20.04 LTS
Update System packages:
sudo apt update
Install Docker on Ubuntu 20.04 LTS
sudo apt install docker.io
How to Create a Docker file
Step 1: Create Dockerfile in the root directory of GitLab Maven Project Branch.
Step 2: Paste below code into it:
FROM openjdk
COPY target/*.jar /
EXPOSE 8080
ENTRYPOINT ["java","-jar","/my-app-1.0-SNAPSHOT.jar"]

How to Build Docker image using GitLab CI/CD Pipeline
Step 1: Create .gitlab-ci.yml file with below code:
stages:
- build
- build-docker-image
build-java-project:
stage: build
script:
mvn package
artifacts:
paths:
- /home/gitlab-runner/
build-docker-image:
stage: build-docker-image
script:
- docker info
- docker build -t devopshint/java-maven .

If your second job docker image failed then run below command:
sudo chmod 666 /var/run/docker.sock

So our job succeeded

Now lets check our docker image
You can see in below image our docker image java-maven

Conclusion:
In this article we have covered Build Docker Image using GitLab CI CD.
Related Articles:
Reference: