Build Docker Image using GitLab CI CD

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"]
Build Docker Image using GitLab CI CD 1
docker file

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 .
Build Docker Image using GitLab CI CD 2
gitlab pipeline

If your second job docker image failed then run below command:

sudo chmod 666 /var/run/docker.sock
Build Docker Image using GitLab CI CD 3

So our job succeeded

Build Docker Image using GitLab CI CD 4
job succeeded

Now lets check our docker image

You can see in below image our docker image java-maven

Build Docker Image using GitLab CI CD 5
docker image

Conclusion:

In this article we have covered Build Docker Image using GitLab CI CD.

Related Articles:

Reference:

Build Docker Image using GitLab CI official page

Shweta Mamidwar

I am Shweta Mamidwar working as a Intern in Product Company. Likes to share knowledge.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap